chia7712 commented on code in PR #18891:
URL: https://github.com/apache/kafka/pull/18891#discussion_r1957734129


##########
clients/src/test/java/org/apache/kafka/test/TestUtils.java:
##########
@@ -580,21 +583,28 @@ public static Set<TopicPartition> 
generateRandomTopicPartitions(int numTopic, in
     }
 
     /**
-     * Assert that a future raises an expected exception cause type. Return 
the exception cause
-     * if the assertion succeeds; otherwise raise AssertionError.
+     * Assert that a future raises an expected exception cause type.
+     * This method will wait for the future to complete or timeout(15000 
milliseconds) after a default period.
      *
      * @param future The future to await
      * @param exceptionCauseClass Class of the expected exception cause
      * @param <T> Exception cause type parameter
      * @return The caught exception cause
      */
     public static <T extends Throwable> T assertFutureThrows(Future<?> future, 
Class<T> exceptionCauseClass) {
-        ExecutionException exception = assertThrows(ExecutionException.class, 
future::get);
-        Throwable cause = exception.getCause();
-        assertEquals(exceptionCauseClass, cause.getClass(),
-            "Expected a " + exceptionCauseClass.getSimpleName() + " exception, 
but got " +
-                        cause.getClass().getSimpleName());
-        return exceptionCauseClass.cast(exception.getCause());
+        try {
+            future.get(DEFAULT_MAX_WAIT_MS, TimeUnit.MILLISECONDS);
+            fail("Should throw expected exception " + 
exceptionCauseClass.getSimpleName() + " but nothing was thrown.");
+        } catch (InterruptedException | ExecutionException | 
CancellationException e) {
+            Throwable cause = e instanceof ExecutionException ? e.getCause() : 
e;
+            return assertInstanceOf(
+                exceptionCauseClass, cause,
+                "Expected " + exceptionCauseClass.getSimpleName() + ", but got 
" + cause
+            );
+        } catch (TimeoutException e) {
+            fail("Future is not completed within " + DEFAULT_MAX_WAIT_MS + " 
millisecond.");
+        }
+        throw new RuntimeException("Future should throw expected exception but 
unexpected error happened.");

Review Comment:
   Could you please include the "unexpected error"?



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