You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "abhishekrb19 (via GitHub)" <gi...@apache.org> on 2026/01/02 18:12:43 UTC

Re: [PR] Increase code coverage for `DirectDruidClientTest` (druid)

abhishekrb19 commented on code in PR #18861:
URL: https://github.com/apache/druid/pull/18861#discussion_r2658183970


##########
server/src/test/java/org/apache/druid/client/DirectDruidClientTest.java:
##########
@@ -241,244 +191,199 @@ public void testRun() throws Exception
     List<Result> results = s1.toList();
     Assert.assertEquals(1, results.size());
     Assert.assertEquals(DateTimes.of("2014-01-01T01:02:03Z"), results.get(0).getTimestamp());
-    Assert.assertEquals(3, client.getNumOpenConnections());
-
-    client2.run(QueryPlus.wrap(query));
-    client2.run(QueryPlus.wrap(query));
+    Assert.assertEquals(3, client1.getNumOpenConnections());
 
+    client2.run(queryPlus, responseContext);
+    client2.run(queryPlus, responseContext);
     Assert.assertEquals(2, client2.getNumOpenConnections());
-
-    Assert.assertEquals(serverSelector.pick(null, CloneQueryMode.EXCLUDECLONES), queryableDruidServer2);
-
-    EasyMock.verify(httpClient);
   }
 
   @Test
   public void testCancel()
   {
-    Capture<Request> capturedRequest = EasyMock.newCapture();
-    ListenableFuture<Object> cancelledFuture = Futures.immediateCancelledFuture();
-    SettableFuture<Object> cancellationFuture = SettableFuture.create();
-
-    EasyMock.expect(
-        httpClient.go(
-            EasyMock.capture(capturedRequest),
-            EasyMock.<HttpResponseHandler>anyObject(),
-            EasyMock.anyObject(Duration.class)
-        )
-    )
-            .andReturn(cancelledFuture)
-            .once();
-
-    EasyMock.expect(
-        httpClient.go(
-            EasyMock.capture(capturedRequest),
-            EasyMock.<HttpResponseHandler>anyObject(),
-            EasyMock.anyObject(Duration.class)
-        )
-    )
-            .andReturn(cancellationFuture)
-            .anyTimes();
+    DirectDruidClient client = makeDirectDruidClient(initHttpClientWithFuture(Futures.immediateCancelledFuture()));
+    Sequence results = client.run(getQueryPlus(), responseContext);
 
-    EasyMock.replay(httpClient);
-
-
-    TimeBoundaryQuery query = Druids.newTimeBoundaryQueryBuilder().dataSource("test").build();
-    query = query.withOverriddenContext(ImmutableMap.of(DirectDruidClient.QUERY_FAIL_TIME, Long.MAX_VALUE));
-    cancellationFuture.set(new StatusResponseHolder(HttpResponseStatus.OK, new StringBuilder("cancelled")));
-    Sequence results = client.run(QueryPlus.wrap(query));
-    Assert.assertEquals(HttpMethod.POST, capturedRequest.getValue().getMethod());
     Assert.assertEquals(0, client.getNumOpenConnections());
-
-
-    QueryInterruptedException exception = null;
-    try {
-      results.toList();
-    }
-    catch (QueryInterruptedException e) {
-      exception = e;
-    }
-    Assert.assertNotNull(exception);
-
-    EasyMock.verify(httpClient);
+    QueryInterruptedException actualException =
+        Assert.assertThrows(QueryInterruptedException.class, () -> results.toList());
+    Assert.assertEquals(hostName, actualException.getHost());
+    Assert.assertEquals("Query cancelled", actualException.getErrorCode());
+    Assert.assertEquals("Task was cancelled.", actualException.getCause().getMessage());
   }
 
   @Test
   public void testQueryInterruptionExceptionLogMessage()
   {
     SettableFuture<Object> interruptionFuture = SettableFuture.create();
-    Capture<Request> capturedRequest = EasyMock.newCapture();
-    final String hostName = "localhost:8080";
-    EasyMock
-        .expect(
-            httpClient.go(
-                EasyMock.capture(capturedRequest),
-                EasyMock.<HttpResponseHandler>anyObject(),
-                EasyMock.anyObject(Duration.class)
-            )
-        )
-        .andReturn(interruptionFuture)
-        .anyTimes();
-
-    EasyMock.replay(httpClient);
-
-    // test error
-    TimeBoundaryQuery query = Druids.newTimeBoundaryQueryBuilder().dataSource("test").build();
-    query = query.withOverriddenContext(ImmutableMap.of(DirectDruidClient.QUERY_FAIL_TIME, Long.MAX_VALUE));
     interruptionFuture.set(
         new ByteArrayInputStream(
             StringUtils.toUtf8("{\"error\":\"testing1\",\"errorMessage\":\"testing2\"}")
         )
     );
-    Sequence results = client.run(QueryPlus.wrap(query));
+    final DirectDruidClient client = makeDirectDruidClient(initHttpClientWithFuture(interruptionFuture));
 
-    QueryInterruptedException actualException = null;
-    try {
-      results.toList();
-    }
-    catch (QueryInterruptedException e) {
-      actualException = e;
-    }
-    Assert.assertNotNull(actualException);
+    interruptionFuture.set(
+        new ByteArrayInputStream(StringUtils.toUtf8("{\"error\":\"testing1\",\"errorMessage\":\"testing2\"}"))
+    );
+    Sequence results = client.run(getQueryPlus(), responseContext);
+
+    QueryInterruptedException actualException =
+        Assert.assertThrows(QueryInterruptedException.class, () -> results.toList());
     Assert.assertEquals("testing1", actualException.getErrorCode());
     Assert.assertEquals("testing2", actualException.getMessage());
     Assert.assertEquals(hostName, actualException.getHost());
-    EasyMock.verify(httpClient);
   }
 
   @Test
   public void testQueryTimeoutBeforeFuture() throws IOException, InterruptedException

Review Comment:
   Removed `InterruptedException` from this test method.  
   
   >Come to think of it, I'm not quite sure why a linter wouldn't catch this. We should add this as a rule.
   
   hmm, I vaguely recall that we did have IntelliJ checks to catch this kind of issue, but they were removed at some point. I'm not sure if pmc/checkstyle can be configured to catch this.



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org