lianetm commented on code in PR #16140:
URL: https://github.com/apache/kafka/pull/16140#discussion_r1631259791
##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerNetworkThreadTest.java:
##########
@@ -258,155 +252,66 @@ void testPollResultTimer() {
NetworkClientDelegate.PollResult success = new
NetworkClientDelegate.PollResult(
10,
Collections.singletonList(req));
- assertEquals(10, networkClient.addAll(success));
+ assertEquals(10, networkClientDelegate.addAll(success));
NetworkClientDelegate.PollResult failure = new
NetworkClientDelegate.PollResult(
10,
new ArrayList<>());
- assertEquals(10, networkClient.addAll(failure));
+ assertEquals(10, networkClientDelegate.addAll(failure));
}
@Test
- void testMaximumTimeToWait() {
+ public void testMaximumTimeToWait() {
// Initial value before runOnce has been called
assertEquals(ConsumerNetworkThread.MAX_POLL_TIMEOUT_MS,
consumerNetworkThread.maximumTimeToWait());
- consumerNetworkThread.runOnce();
- // After runOnce has been called, it takes the default heartbeat
interval from the heartbeat request manager
- assertEquals(DEFAULT_HEARTBEAT_INTERVAL_MS,
consumerNetworkThread.maximumTimeToWait());
- }
- @Test
- void testRequestManagersArePolledOnce() {
- consumerNetworkThread.runOnce();
- testBuilder.requestManagers.entries().forEach(rmo -> rmo.ifPresent(rm
-> verify(rm, times(1)).poll(anyLong())));
- testBuilder.requestManagers.entries().forEach(rmo -> rmo.ifPresent(rm
-> verify(rm, times(1)).maximumTimeToWait(anyLong())));
- verify(networkClient, times(1)).poll(anyLong(), anyLong());
- }
+
when(requestManagers.entries()).thenReturn(Collections.singletonList(Optional.of(heartbeatRequestManager)));
+
when(heartbeatRequestManager.maximumTimeToWait(time.milliseconds())).thenReturn((long)
DEFAULT_HEARTBEAT_INTERVAL_MS);
- @Test
- void testEnsureMetadataUpdateOnPoll() {
- MetadataResponse metadataResponse =
RequestTestUtils.metadataUpdateWith(2, Collections.emptyMap());
- client.prepareMetadataUpdate(metadataResponse);
- metadata.requestUpdate(false);
consumerNetworkThread.runOnce();
- verify(metadata,
times(1)).updateWithCurrentRequestVersion(eq(metadataResponse), eq(false),
anyLong());
- }
-
- @Test
- void testEnsureEventsAreCompleted() {
- // Mimic the logic of CompletableEventReaper.reap(Collection):
- doAnswer(__ -> {
- Iterator<ApplicationEvent> i = applicationEventsQueue.iterator();
-
- while (i.hasNext()) {
- ApplicationEvent event = i.next();
-
- if (event instanceof CompletableEvent)
- ((CompletableEvent<?>)
event).future().completeExceptionally(new TimeoutException());
-
- i.remove();
- }
-
- return null;
- }).when(applicationEventReaper).reap(any(Collection.class));
-
- Node node = metadata.fetch().nodes().get(0);
- coordinatorRequestManager.markCoordinatorUnknown("test",
time.milliseconds());
-
client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE,
"group-id", node));
- prepareOffsetCommitRequest(new HashMap<>(), Errors.NONE, false);
- CompletableApplicationEvent<Void> event1 = spy(new
AsyncCommitEvent(Collections.emptyMap()));
- ApplicationEvent event2 = new AsyncCommitEvent(Collections.emptyMap());
- CompletableFuture<Void> future = new CompletableFuture<>();
- when(event1.future()).thenReturn(future);
- applicationEventsQueue.add(event1);
- applicationEventsQueue.add(event2);
- assertFalse(future.isDone());
- assertFalse(applicationEventsQueue.isEmpty());
- consumerNetworkThread.cleanup();
- assertTrue(future.isCompletedExceptionally());
- assertTrue(applicationEventsQueue.isEmpty());
+ // After runOnce has been called, it takes the default heartbeat
interval from the heartbeat request manager
+ assertEquals(DEFAULT_HEARTBEAT_INTERVAL_MS,
consumerNetworkThread.maximumTimeToWait());
}
@Test
- void testCleanupInvokesReaper() {
+ public void testCleanupInvokesReaper() {
+ LinkedList<NetworkClientDelegate.UnsentRequest> queue = new
LinkedList<>();
+ when(networkClientDelegate.unsentRequests()).thenReturn(queue);
consumerNetworkThread.cleanup();
verify(applicationEventReaper).reap(applicationEventsQueue);
}
@Test
- void testRunOnceInvokesReaper() {
+ public void testRunOnceInvokesReaper() {
consumerNetworkThread.runOnce();
verify(applicationEventReaper).reap(any(Long.class));
}
@Test
- void testSendUnsentRequest() {
- String groupId = "group-id";
- NetworkClientDelegate.UnsentRequest request = new
NetworkClientDelegate.UnsentRequest(
- new FindCoordinatorRequest.Builder(
- new FindCoordinatorRequestData()
-
.setKeyType(FindCoordinatorRequest.CoordinatorType.TRANSACTION.id())
- .setKey(groupId)),
- Optional.empty());
-
- networkClient.add(request);
- assertTrue(networkClient.hasAnyPendingRequests());
- assertFalse(networkClient.unsentRequests().isEmpty());
- assertFalse(client.hasInFlightRequests());
+ public void testSendUnsentRequests() {
+ when(networkClientDelegate.unsentRequests()).thenReturn(new
LinkedList<>());
+
when(networkClientDelegate.hasAnyPendingRequests()).thenReturn(true).thenReturn(true).thenReturn(false);
+
consumerNetworkThread.cleanup();
- assertTrue(networkClient.unsentRequests().isEmpty());
+ verify(networkClientDelegate, times(2)).poll(anyLong(), anyLong());
+ assertTrue(networkClientDelegate.unsentRequests().isEmpty());
assertFalse(client.hasInFlightRequests());
Review Comment:
```suggestion
verify(networkClientDelegate, times(2)).poll(anyLong(), anyLong());
```
We don't need to assert on the mocked clients. As long as we can verify that
the consumer network thread is calling the client poll, the rest is the
responsibility of the networkClientDelegate
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]