Repository: apache/kafka
File: clients/src/test/java/org/apache/kafka/clients/consumer/internals/CommitRequestManagerTest.java (Java)

Code before:
        when(coordinatorRequestManager.coordinator()).thenReturn(Optional.of(mockedNode));

        commitRequestManager.fetchOffsets(Collections.singleton(new TopicPartition("test", 0)), 200);
        assertEquals(1, commitRequestManager.pendingRequests.unsentOffsetFetches.size());

        when(coordinatorRequestManager.coordinator()).thenReturn(Optional.empty());
        when(coordinatorRequestManager.fatalError())
                .thenReturn(Optional.of(new GroupAuthorizationException("Group authorization exception")));

        assertEquals(NetworkClientDelegate.PollResult.EMPTY, commitRequestManager.poll(200));

        assertEmptyPendingRequests(commitRequestManager);
    }

    private static void assertEmptyPendingRequests(CommitRequestManager commitRequestManager) {
        assertTrue(commitRequestManager.pendingRequests.inflightOffsetFetches.isEmpty());
        assertTrue(commitRequestManager.pendingRequests.unsentOffsetFetches.isEmpty());
        assertTrue(commitRequestManager.pendingRequests.unsentOffsetCommits.isEmpty());
    }

    @Test
    public void testPollWithFatalErrorDuringCoordinatorIsEmptyAndClosing() {
        CommitRequestManager commitRequestManager = create(true, 100);

        Map<TopicPartition, OffsetAndMetadata> offsets = Collections.singletonMap(new TopicPartition("topic", 1),
                new OffsetAndMetadata(0));

        var commitFuture = commitRequestManager.commitAsync(offsets);

        commitRequestManager.signalClose();
        when(coordinatorRequestManager.coordinator()).thenReturn(Optional.empty());
        when(coordinatorRequestManager.fatalError())
                .thenReturn(Optional.of(new GroupAuthorizationException("Fatal error")));

        assertEquals(NetworkClientDelegate.PollResult.EMPTY, commitRequestManager.poll(200));

        assertTrue(commitFuture.isCompletedExceptionally());

        TestUtils.assertFutureThrows(GroupAuthorizationException.class, commitFuture, "Fatal error");
    }

    // Supplies (error, isRetriable)
    private static Stream<Arguments> partitionDataErrorSupplier() {
        return Stream.of(
            Arguments.of(Errors.UNSTABLE_OFFSET_COMMIT, true),
            Arguments.of(Errors.UNKNOWN_TOPIC_OR_PARTITION, false),
            Arguments.of(Errors.TOPIC_AUTHORIZATION_FAILED, false),
            Arguments.of(Errors.UNKNOWN_SERVER_ERROR, false));
    }

    private List<CompletableFuture<Map<TopicPartition, OffsetAndMetadata>>> sendAndVerifyDuplicatedOffsetFetchRequests(

Review comment (suggestion):
```suggestion
        Map<TopicPartition, OffsetAndMetadata> offsets = Map.of(new TopicPartition("topic", 1),
                new OffsetAndMetadata(0));
```

Code after:
        when(coordinatorRequestManager.coordinator()).thenReturn(Optional.of(mockedNode));

        commitRequestManager.fetchOffsets(Collections.singleton(new TopicPartition("test", 0)), 200);
        assertEquals(1, commitRequestManager.pendingRequests.unsentOffsetFetches.size());

        when(coordinatorRequestManager.coordinator()).thenReturn(Optional.empty());
        when(coordinatorRequestManager.fatalError())
                .thenReturn(Optional.of(new GroupAuthorizationException("Group authorization exception")));

        assertEquals(NetworkClientDelegate.PollResult.EMPTY, commitRequestManager.poll(200));

        assertEmptyPendingRequests(commitRequestManager);
    }

    private static void assertEmptyPendingRequests(CommitRequestManager commitRequestManager) {
        assertTrue(commitRequestManager.pendingRequests.inflightOffsetFetches.isEmpty());
        assertTrue(commitRequestManager.pendingRequests.unsentOffsetFetches.isEmpty());
        assertTrue(commitRequestManager.pendingRequests.unsentOffsetCommits.isEmpty());
    }

    @Test
    public void testPollWithFatalErrorDuringCoordinatorIsEmptyAndClosing() {
        CommitRequestManager commitRequestManager = create(true, 100);

        Map<TopicPartition, OffsetAndMetadata> offsets = Map.of(new TopicPartition("topic", 1),
                new OffsetAndMetadata(0));

        var commitFuture = commitRequestManager.commitAsync(offsets);

        commitRequestManager.signalClose();
        when(coordinatorRequestManager.coordinator()).thenReturn(Optional.empty());
        when(coordinatorRequestManager.fatalError())
                .thenReturn(Optional.of(new GroupAuthorizationException("Fatal error")));

        assertEquals(NetworkClientDelegate.PollResult.EMPTY, commitRequestManager.poll(time.milliseconds()));

        assertTrue(commitFuture.isCompletedExceptionally());

        TestUtils.assertFutureThrows(GroupAuthorizationException.class, commitFuture, "Fatal error");
    }

    @Test
    public void testPollWithClosingAndPendingRequests() {
        CommitRequestManager commitRequestManager = create(true, 100);

        Map<TopicPartition, OffsetAndMetadata> offsets = Map.of(new TopicPartition("topic", 1),
                new OffsetAndMetadata(0));

        var commitFuture = commitRequestManager.commitAsync(offsets);

        commitRequestManager.signalClose();