Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ public void run() {
LOG.debug("Pause and skip consuming from Kafka topic due to an external condition: {}", pauseConsumePredicate);
paused = true;
consumer.pause(consumer.assignment());
doPoll();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doPoll() result is intentionally discarded. Add a one-line comment since a rebalance mid-poll could return records that are safely re-delivered later.

Thread.sleep(1000);
continue;
} else if(paused) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,57 @@ private ConsumerRecords createJsonRecords(String topic) throws Exception {
return new ConsumerRecords(records);
}

@Test
public void testPauseConsumingCallsPollToMaintainGroupMembership() throws Exception {
String topic = topicConfig.getName();
when(topicConfig.getMaxPollInterval()).thenReturn(Duration.ofMillis(4000));

when(pauseConsumePredicate.pauseConsuming()).thenReturn(true);
when(kafkaConsumer.poll(any(Duration.class))).thenReturn(ConsumerRecords.empty());
when(kafkaConsumer.assignment()).thenReturn(java.util.Collections.singleton(new TopicPartition(topic, testPartition)));

consumer = createObjectUnderTestWithMockBuffer("plaintext");
consumer.onPartitionsAssigned(List.of(new TopicPartition(topic, testPartition)));

// Run in a thread and shut down after a short delay
Thread consumerThread = new Thread(() -> consumer.run());
consumerThread.start();
Thread.sleep(100);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thread.sleep(100) before the verify. I prefer Awaitility over a fixed sleep.

shutdownInProgress.set(true);
consumerThread.join(5000);

// Verify poll() was called even though consuming was paused
verify(kafkaConsumer, org.mockito.Mockito.atLeastOnce()).pause(any());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline FQN org.mockito.Mockito.atLeastOnce() static-import atLeastOnce.

verify(kafkaConsumer, org.mockito.Mockito.atLeastOnce()).poll(any(Duration.class));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline FQN org.mockito.Mockito.atLeastOnce() static-import atLeastOnce.

}

@Test
public void testPauseConsumingResumesAfterPredicateReturnsFalse() throws Exception {
String topic = topicConfig.getName();
when(topicConfig.getMaxPollInterval()).thenReturn(Duration.ofMillis(4000));

// First call returns true (paused), subsequent calls return false (resumed)
when(pauseConsumePredicate.pauseConsuming())
.thenReturn(true)
.thenReturn(false);
when(kafkaConsumer.poll(any(Duration.class))).thenReturn(ConsumerRecords.empty());
when(kafkaConsumer.assignment()).thenReturn(java.util.Collections.singleton(new TopicPartition(topic, testPartition)));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline FQN java.util.Collections.singleton(...) Please import Collections.


consumer = createObjectUnderTestWithMockBuffer("plaintext");
consumer.onPartitionsAssigned(List.of(new TopicPartition(topic, testPartition)));

// Need to wait longer than the 1s sleep in the pause branch for the second iteration
Thread consumerThread = new Thread(() -> consumer.run());
consumerThread.start();
Thread.sleep(2500);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thread.sleep(2500) is slow and timing-dependent. Please use Awaitility.await().untilAsserted(() -> verify(...resume())).

shutdownInProgress.set(true);
consumerThread.join(5000);

// Verify that consumer was paused and then resumed
verify(kafkaConsumer, org.mockito.Mockito.atLeastOnce()).pause(any());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline FQN org.mockito.Mockito.atLeastOnce() static-import atLeastOnce.

verify(kafkaConsumer, org.mockito.Mockito.atLeastOnce()).resume(any());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline FQN org.mockito.Mockito.atLeastOnce() static-import atLeastOnce.

}

private static Stream<Arguments> provideExceptionsFromBufferWrite() {
return Stream.of(
Arguments.of(new SizeOverflowException("size overflow")),
Expand Down
Loading