FrankYang0529 commented on code in PR #16982:
URL: https://github.com/apache/kafka/pull/16982#discussion_r1734259073


##########
clients/src/test/java/org/apache/kafka/clients/consumer/KafkaConsumerTest.java:
##########
@@ -2481,23 +2489,31 @@ public void testCurrentLag(GroupProtocol groupProtocol) 
{
         consumer.seek(tp0, 50L);
         consumer.poll(Duration.ofMillis(0));
         // requests: list-offset, fetch
-        assertEquals(2, client.inFlightRequestCount());
+        TestUtils.waitForCondition(() -> {
+            boolean hasListOffsetRequest = requestGenerated(client, 
ApiKeys.LIST_OFFSETS);
+            boolean fetchRequest = requestGenerated(client, ApiKeys.FETCH);
+            return hasListOffsetRequest && fetchRequest;
+        }, "No list-offset & fetch request sent");
 
         // no error for no end offset (so unknown lag)
         assertEquals(OptionalLong.empty(), consumer.currentLag(tp0));
 
         // poll once again, which should return the list-offset response
         // and hence next call would return correct lag result
-        client.respond(listOffsetsResponse(singletonMap(tp0, 90L)));
+        Optional<ClientRequest> listOffsetRequest = 
client.requests().stream().filter(request -> 
request.requestBuilder().apiKey().equals(ApiKeys.LIST_OFFSETS)).findFirst();
+        assertTrue(listOffsetRequest.isPresent());
+        client.respondToRequest(listOffsetRequest.get(), 
listOffsetsResponse(singletonMap(tp0, 90L)));
         consumer.poll(Duration.ofMillis(0));
 
         assertEquals(OptionalLong.of(40L), consumer.currentLag(tp0));
         // requests: fetch
-        assertEquals(1, client.inFlightRequestCount());
+        TestUtils.waitForCondition(() -> requestGenerated(client, 
ApiKeys.FETCH), "No fetch request sent");
 
         // one successful fetch should update the log end offset and the 
position
+        Optional<ClientRequest> fetchRequest = 
client.requests().stream().filter(request -> 
request.requestBuilder().apiKey().equals(ApiKeys.FETCH)).findFirst();
         final FetchInfo fetchInfo = new FetchInfo(1L, 99L, 50L, 5);
-        client.respond(fetchResponse(singletonMap(tp0, fetchInfo)));
+        assertTrue(fetchRequest.isPresent());

Review Comment:
   Hi @lianetm, thanks for the review and suggestion. I addressed other 
comments. For this one, I would like to keep it. If we remove it, there may 
have warning message like `'Optional.get()' without 'isPresent()' check`.



-- 
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]

Reply via email to