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

Code before:
            topicId);
        futures.forEach(f -> {
            assertTrue(f.isDone());
            assertFalse(f.isCompletedExceptionally());
        });
        // expecting the buffers to be emptied after being completed successfully
        commitRequestManager.poll(0);
        assertEmptyPendingRequests(commitRequestManager);
    }

    @Test
    public void testOffsetFetchRequestShouldFailWithTopicIdWhenMetadataUnknownResponseTopicId() {
        CommitRequestManager commitRequestManager = create(true, 100);
        when(coordinatorRequestManager.coordinator()).thenReturn(Optional.of(mockedNode));
        Uuid topicId = Uuid.randomUuid();
        when(metadata.topicIds()).thenReturn(Map.of("t1", topicId));
        // Mock the scenario where the topicID from the response is not in the metadata.
        when(metadata.topicNames()).thenReturn(Map.of());
        Set<TopicPartition> partitions = new HashSet<>();
        partitions.add(new TopicPartition("t1", 0));

        List<CompletableFuture<Map<TopicPartition, OffsetAndMetadata>>> futures = sendAndVerifyDuplicatedOffsetFetchRequests(
            commitRequestManager,
            partitions,
            1,
            Errors.UNKNOWN_TOPIC_OR_PARTITION,
            true,
            topicId);
        futures.forEach(f -> testRetriable(commitRequestManager, futures, Errors.UNKNOWN_TOPIC_OR_PARTITION));
        // expecting the buffers to be emptied after being completed successfully
        commitRequestManager.poll(0);
        assertEmptyPendingRequests(commitRequestManager);
    }

    @ParameterizedTest
    @MethodSource("offsetFetchExceptionSupplier")
    public void testOffsetFetchRequestErroredRequests(final Errors error) {
        CommitRequestManager commitRequestManager = create(true, 100);
        when(coordinatorRequestManager.coordinator()).thenReturn(Optional.of(mockedNode));

        Set<TopicPartition> partitions = new HashSet<>();
        partitions.add(new TopicPartition("t1", 0));
        List<CompletableFuture<Map<TopicPartition, OffsetAndMetadata>>> futures = sendAndVerifyDuplicatedOffsetFetchRequests(
            commitRequestManager,
            partitions,
            1,
            error);
        // we only want to make sure to purge the outbound buffer for non-retriables, so retriable will be re-queued.
        if (error.exception() instanceof RetriableException)
            testRetriable(commitRequestManager, futures, error);
        else {

Review comment (bug):
this generates a response with an error at the group level, which is not exactly the scenario we want to test. 

We are after the case where we use topic IDs, the response has NONE error, but topic ID is not in metadata anymore. On fetch that is expected to fail with `KafkaException` (not retriable, sorry I mentioned testRetriable before, that's the case for commit but not for fetch). So we just need NONE and assert that the future completes exceptionally with `KafkaException` to cover our scenario I expect.
https://github.com/apache/kafka/blob/0491e1124eaa163a7ed7c139ab477c9f4a7b71c9/clients/src/main/java/org/apache/kafka/clients/consumer/internals/CommitRequestManager.java#L1142

Code after:
            true,
            topicId);
        futures.forEach(f -> {
            assertTrue(f.isDone());
            assertFalse(f.isCompletedExceptionally());
        });
        // expecting the buffers to be emptied after being completed successfully
        commitRequestManager.poll(0);
        assertEmptyPendingRequests(commitRequestManager);
    }

    @Test
    public void testOffsetFetchRequestShouldFailWithTopicIdWhenMetadataUnknownResponseTopicId() {
        CommitRequestManager commitRequestManager = create(true, 100);
        when(coordinatorRequestManager.coordinator()).thenReturn(Optional.of(mockedNode));
        Uuid topicId = Uuid.randomUuid();
        when(metadata.topicIds()).thenReturn(Map.of("t1", topicId));
        // Mock the scenario where the topicID from the response is not in the metadata.
        when(metadata.topicNames()).thenReturn(Map.of());
        Set<TopicPartition> partitions = new HashSet<>();
        partitions.add(new TopicPartition("t1", 0));

        List<CompletableFuture<Map<TopicPartition, OffsetAndMetadata>>> futures = sendAndVerifyDuplicatedOffsetFetchRequests(
            commitRequestManager,
            partitions,
            1,
            Errors.NONE,
            true,
            topicId);
        futures.forEach(f -> {
            assertTrue(f.isDone());
            assertTrue(f.isCompletedExceptionally());
            ExecutionException exception = assertThrows(ExecutionException.class, f::get);
            assertInstanceOf(KafkaException.class, exception.getCause());
        });
        // expecting the buffers to be emptied after being completed successfully
        commitRequestManager.poll(0);
        assertEmptyPendingRequests(commitRequestManager);
    }

    @ParameterizedTest
    @MethodSource("offsetFetchExceptionSupplier")
    public void testOffsetFetchRequestErroredRequests(final Errors error) {
        CommitRequestManager commitRequestManager = create(true, 100);
        when(coordinatorRequestManager.coordinator()).thenReturn(Optional.of(mockedNode));

        Set<TopicPartition> partitions = new HashSet<>();
        partitions.add(new TopicPartition("t1", 0));
        List<CompletableFuture<Map<TopicPartition, OffsetAndMetadata>>> futures = sendAndVerifyDuplicatedOffsetFetchRequests(
            commitRequestManager,
            partitions,