kirktrue commented on code in PR #15613: URL: https://github.com/apache/kafka/pull/15613#discussion_r1569232314
########## clients/src/test/java/org/apache/kafka/clients/consumer/internals/AsyncKafkaConsumerTest.java: ########## @@ -616,6 +620,80 @@ public void testCommitSyncTriggersFencedExceptionFromCommitAsync() { assertEquals("Get fenced exception for group.instance.id groupInstanceId1", e.getMessage()); } + @Test + public void testCommitSyncAwaitsCommitAsyncCompletionWithEmptyOffsets() { + time = new MockTime(1); + consumer = newConsumer(); + + // Commit async (incomplete) + doReturn(Fetch.empty()).when(fetchCollector).collectFetch(any(FetchBuffer.class)); + doReturn(LeaderAndEpoch.noLeaderOrEpoch()).when(metadata).currentLeader(any()); + final TopicPartition tp = new TopicPartition("foo", 0); + consumer.assign(Collections.singleton(tp)); + consumer.seek(tp, 20); + consumer.commitAsync(); + + // Commit async is not completed yet, so commit sync should wait for it to complete (time out) + assertThrows(TimeoutException.class, () -> consumer.commitSync(Collections.emptyMap(), Duration.ofMillis(100))); + + // Complete async commit event + final ArgumentCaptor<AsyncCommitEvent> commitEventCaptor = ArgumentCaptor.forClass(AsyncCommitEvent.class); + verify(applicationEventHandler).add(commitEventCaptor.capture()); + final AsyncCommitEvent commitEvent = commitEventCaptor.getValue(); + commitEvent.future().complete(null); + + // Commit async is completed, so commit sync completes immediately (since offsets are empty) + assertDoesNotThrow(() -> consumer.commitSync(Collections.emptyMap(), Duration.ofMillis(100))); + } + + @Test + public void testCommitSyncAwaitsCommitAsyncCompletionWithNonEmptyOffsets() { + final TopicPartition tp = new TopicPartition("foo", 0); + testSyncCommitTimesoutAfterIncompleteAsyncCommit(tp); + + // Complete async commit event and sync commit event + final ArgumentCaptor<AsyncCommitEvent> commitEventCaptor = ArgumentCaptor.forClass(AsyncCommitEvent.class); + verify(applicationEventHandler).add(commitEventCaptor.capture()); + final AsyncCommitEvent commitEvent = commitEventCaptor.getValue(); + commitEvent.future().complete(null); + + // Commit async is completed, so commit sync completes immediately (since offsets are empty) + assertDoesNotThrow(() -> consumer.commitSync(Collections.singletonMap(tp, new OffsetAndMetadata(20)), Duration.ofMillis(100))); + } + + @Test + public void testCommitSyncAwaitsCommitAsyncButDoesNotFail() { + final TopicPartition tp = new TopicPartition("foo", 0); + testSyncCommitTimesoutAfterIncompleteAsyncCommit(tp); + + // Complete exceptionally async commit event and sync commit event + final ArgumentCaptor<AsyncCommitEvent> commitEventCaptor = ArgumentCaptor.forClass(AsyncCommitEvent.class); + verify(applicationEventHandler).add(commitEventCaptor.capture()); + final AsyncCommitEvent commitEvent = commitEventCaptor.getValue(); + commitEvent.future().completeExceptionally(new KafkaException("Test exception")); + + // Commit async is completed exceptionally, but this will be handled by commit callback - commit sync should not fail. + assertDoesNotThrow(() -> consumer.commitSync(Collections.singletonMap(tp, new OffsetAndMetadata(20)), Duration.ofMillis(100))); + } + + private void testSyncCommitTimesoutAfterIncompleteAsyncCommit(TopicPartition tp) { + time = new MockTime(1); + consumer = newConsumer(); + + // Commit async (incomplete) + doReturn(Fetch.empty()).when(fetchCollector).collectFetch(any(FetchBuffer.class)); + doReturn(LeaderAndEpoch.noLeaderOrEpoch()).when(metadata).currentLeader(any()); + consumer.assign(Collections.singleton(tp)); + consumer.seek(tp, 20); Review Comment: Yes, that's what I meant 😉 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org