You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "chiacyu (via GitHub)" <gi...@apache.org> on 2024/06/10 09:40:56 UTC

[PR] Add time out in assertFutureThrows [kafka]

chiacyu opened a new pull request, #16264:
URL: https://github.com/apache/kafka/pull/16264

   Based on [KAFKA-16918](https://issues.apache.org/jira/browse/KAFKA-16918), this pr add `future.get()` with timeout in `assertFutureThrows`.
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


-- 
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: jira-unsubscribe@kafka.apache.org

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


Re: [PR] Add time out in assertFutureThrows [kafka]

Posted by "chiacyu (via GitHub)" <gi...@apache.org>.
chiacyu commented on code in PR #16264:
URL: https://github.com/apache/kafka/pull/16264#discussion_r1634968366


##########
clients/src/test/java/org/apache/kafka/test/TestUtils.java:
##########
@@ -556,10 +557,15 @@ public static Set<TopicPartition> generateRandomTopicPartitions(int numTopic, in
      * @return The caught exception cause
      */
     public static <T extends Throwable> T assertFutureThrows(Future<?> future, Class<T> exceptionCauseClass) {
-        ExecutionException exception = assertThrows(ExecutionException.class, future::get);
-        assertInstanceOf(exceptionCauseClass, exception.getCause(),
-            "Unexpected exception cause " + exception.getCause());
-        return exceptionCauseClass.cast(exception.getCause());
+        try {
+            future.get(5, TimeUnit.SECONDS);
+            ExecutionException exception = assertThrows(ExecutionException.class, future::get);
+            assertInstanceOf(exceptionCauseClass, exception.getCause(),
+                    "Unexpected exception cause " + exception.getCause());
+            return exceptionCauseClass.cast(exception.getCause());
+        } catch (Exception ignored) {
+            return null;
+        }

Review Comment:
   Thanks for the comments, would apply.



-- 
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: jira-unsubscribe@kafka.apache.org

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


Re: [PR] Add time out in assertFutureThrows [kafka]

Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #16264:
URL: https://github.com/apache/kafka/pull/16264#discussion_r1634016604


##########
clients/src/test/java/org/apache/kafka/test/TestUtils.java:
##########
@@ -556,10 +557,15 @@ public static Set<TopicPartition> generateRandomTopicPartitions(int numTopic, in
      * @return The caught exception cause
      */
     public static <T extends Throwable> T assertFutureThrows(Future<?> future, Class<T> exceptionCauseClass) {
-        ExecutionException exception = assertThrows(ExecutionException.class, future::get);
-        assertInstanceOf(exceptionCauseClass, exception.getCause(),
-            "Unexpected exception cause " + exception.getCause());
-        return exceptionCauseClass.cast(exception.getCause());
+        try {
+            future.get(5, TimeUnit.SECONDS);
+            ExecutionException exception = assertThrows(ExecutionException.class, future::get);
+            assertInstanceOf(exceptionCauseClass, exception.getCause(),
+                    "Unexpected exception cause " + exception.getCause());
+            return exceptionCauseClass.cast(exception.getCause());
+        } catch (Exception ignored) {
+            return null;
+        }

Review Comment:
   Before this change, when future.get throws "non-ExecutionException", it'll fail. But after the change, it'll pass. This changes the original semantics. I think we can use `catch` to assert what we expected. Ex:
   
   ```
   try {
     future.get(5, sec);
     fail("expected to throw ExecutionException...");
   } catch (TimeoutException e) {
     fail("timeout waiting....");
   } catch (ExecutionException e) {
     ...
   } catch ...
   
   
   ```
   
   WDYT? 
   ```



##########
clients/src/test/java/org/apache/kafka/test/TestUtils.java:
##########
@@ -556,10 +557,15 @@ public static Set<TopicPartition> generateRandomTopicPartitions(int numTopic, in
      * @return The caught exception cause
      */
     public static <T extends Throwable> T assertFutureThrows(Future<?> future, Class<T> exceptionCauseClass) {
-        ExecutionException exception = assertThrows(ExecutionException.class, future::get);
-        assertInstanceOf(exceptionCauseClass, exception.getCause(),
-            "Unexpected exception cause " + exception.getCause());
-        return exceptionCauseClass.cast(exception.getCause());
+        try {
+            future.get(5, TimeUnit.SECONDS);
+            ExecutionException exception = assertThrows(ExecutionException.class, future::get);
+            assertInstanceOf(exceptionCauseClass, exception.getCause(),
+                    "Unexpected exception cause " + exception.getCause());
+            return exceptionCauseClass.cast(exception.getCause());
+        } catch (Exception ignored) {
+            return null;
+        }

Review Comment:
   AI saw there's another `future.get` without timeout in L583. Please also fix it. Thanks.



-- 
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: jira-unsubscribe@kafka.apache.org

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


Re: [PR] Add time out in assertFutureThrows [kafka]

Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #16264:
URL: https://github.com/apache/kafka/pull/16264#discussion_r1634018574


##########
clients/src/test/java/org/apache/kafka/test/TestUtils.java:
##########
@@ -556,10 +557,15 @@ public static Set<TopicPartition> generateRandomTopicPartitions(int numTopic, in
      * @return The caught exception cause
      */
     public static <T extends Throwable> T assertFutureThrows(Future<?> future, Class<T> exceptionCauseClass) {
-        ExecutionException exception = assertThrows(ExecutionException.class, future::get);
-        assertInstanceOf(exceptionCauseClass, exception.getCause(),
-            "Unexpected exception cause " + exception.getCause());
-        return exceptionCauseClass.cast(exception.getCause());
+        try {
+            future.get(5, TimeUnit.SECONDS);
+            ExecutionException exception = assertThrows(ExecutionException.class, future::get);
+            assertInstanceOf(exceptionCauseClass, exception.getCause(),
+                    "Unexpected exception cause " + exception.getCause());
+            return exceptionCauseClass.cast(exception.getCause());
+        } catch (Exception ignored) {
+            return null;
+        }

Review Comment:
   I saw there's another `future.get` without timeout in L583. Please also fix it. Thanks.



-- 
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: jira-unsubscribe@kafka.apache.org

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


Re: [PR] Add time out in assertFutureThrows [kafka]

Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #16264:
URL: https://github.com/apache/kafka/pull/16264#discussion_r1635625857


##########
clients/src/test/java/org/apache/kafka/test/TestUtils.java:
##########
@@ -556,10 +558,22 @@ public static Set<TopicPartition> generateRandomTopicPartitions(int numTopic, in
      * @return The caught exception cause
      */
     public static <T extends Throwable> T assertFutureThrows(Future<?> future, Class<T> exceptionCauseClass) {
-        ExecutionException exception = assertThrows(ExecutionException.class, future::get);
-        assertInstanceOf(exceptionCauseClass, exception.getCause(),
-            "Unexpected exception cause " + exception.getCause());
-        return exceptionCauseClass.cast(exception.getCause());
+        try {
+            future.get(5, TimeUnit.SECONDS);
+            fail("expected to throw ExecutionException...");
+        } catch (TimeoutException e) {
+            fail("timeout waiting");
+            return null;
+        } catch (ExecutionException e) {
+            ExecutionException exception = assertThrows(ExecutionException.class, future::get);
+            assertInstanceOf(exceptionCauseClass, exception.getCause(),
+                    "Unexpected exception cause " + exception.getCause());
+            return exceptionCauseClass.cast(exception.getCause());
+        } catch (InterruptedException e) {
+            fail("Unexpected exception cause" + e.getCause());
+            return null;
+        }
+        return null;

Review Comment:
   1. Please put the error message clearly. Do you think users understand what this error message mean when reading it: `expected to throw ExecutionException...`, `timeout waiting`? I wrote it like that is to let you have a start. You can check other places to learn how to put a clear error message.
   2. when `fail()` method invoked, do you know what will happen? 
   3. The method is `assertFutureThrows`, so what should we do if no exception thrown? 



-- 
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: jira-unsubscribe@kafka.apache.org

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


Re: [PR] KAFKA-16918: TestUtils#assertFutureThrows should use future.get with timeout [kafka]

Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #16264:
URL: https://github.com/apache/kafka/pull/16264#discussion_r1639358954


##########
clients/src/test/java/org/apache/kafka/test/TestUtils.java:
##########
@@ -556,10 +558,22 @@ public static Set<TopicPartition> generateRandomTopicPartitions(int numTopic, in
      * @return The caught exception cause
      */
     public static <T extends Throwable> T assertFutureThrows(Future<?> future, Class<T> exceptionCauseClass) {
-        ExecutionException exception = assertThrows(ExecutionException.class, future::get);
-        assertInstanceOf(exceptionCauseClass, exception.getCause(),
-            "Unexpected exception cause " + exception.getCause());
-        return exceptionCauseClass.cast(exception.getCause());
+        try {
+            future.get(5, TimeUnit.SECONDS);
+            fail("expected to throw ExecutionException...");
+        } catch (TimeoutException e) {
+            fail("timeout waiting");
+            return null;
+        } catch (ExecutionException e) {
+            ExecutionException exception = assertThrows(ExecutionException.class, future::get);
+            assertInstanceOf(exceptionCauseClass, exception.getCause(),
+                    "Unexpected exception cause " + exception.getCause());
+            return exceptionCauseClass.cast(exception.getCause());
+        } catch (InterruptedException e) {
+            fail("Unexpected exception cause" + e.getCause());
+            return null;
+        }
+        return null;

Review Comment:
   For 1, yes, for 2, maybe check this commit: https://github.com/apache/kafka/pull/16264#discussion_r1639351349



-- 
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: jira-unsubscribe@kafka.apache.org

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


Re: [PR] KAFKA-16918: TestUtils#assertFutureThrows should use future.get with timeout [kafka]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #16264:
URL: https://github.com/apache/kafka/pull/16264#issuecomment-2571488860

   This PR is being marked as stale since it has not had any activity in 90 days. If you
   would like to keep this PR alive, please leave a comment asking for a review. If the PR has 
   merge conflicts, update it with the latest from the base branch.
   <p>
   If you are having difficulty finding a reviewer, please reach out on the 
   [mailing list](https://kafka.apache.org/contact).
   <p>
   If this PR is no longer valid or desired, please feel free to close it. If no activity
   occurs in the next 30 days, it will be automatically closed.


-- 
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: jira-unsubscribe@kafka.apache.org

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


Re: [PR] KAFKA-16918: TestUtils#assertFutureThrows should use future.get with timeout [kafka]

Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #16264:
URL: https://github.com/apache/kafka/pull/16264#discussion_r1663413976


##########
clients/src/test/java/org/apache/kafka/test/TestUtils.java:
##########
@@ -558,14 +558,13 @@ public static Set<TopicPartition> generateRandomTopicPartitions(int numTopic, in
      */
     public static <T extends Throwable> T assertFutureThrows(Future<?> future, Class<T> exceptionCauseClass) {
         try {
-            future.get(DEFAULT_MAX_WAIT_MS, TimeUnit.SECONDS);
+            future.get(DEFAULT_MAX_WAIT_MS, TimeUnit.MILLISECONDS);
             fail("Future should throw expected exception " + exceptionCauseClass.getSimpleName() + " but succeed.");
         } catch (TimeoutException | InterruptedException | ExecutionException e) {
-            assertInstanceOf(exceptionCauseClass, e.getCause(),
-                    "Unexpected exception cause " + e.getCause());
+            assertInstanceOf(exceptionCauseClass, e.getCause(), "Expected a" + exceptionCauseClass.getSimpleName() + "but got" + e.getCause());
             return exceptionCauseClass.cast(e.getCause());
         }
-        return null;
+        throw new RuntimeException("Future should throw expected exception but unexpected error happened.");

Review Comment:
   I saw you addressed 2 of my 3 comments:
   
   > 1. DEFAULT_MAX_WAIT_MS is a millisecond time unit, not seconds. Same comment applied to L583.
   
   It's fixed now. Thanks.
   > 2. Now, if the future throws TimeoutException or InterruptedException, it will fail as expected. But what error message will we get? In your opinion, do you think that's helpful for troubleshooting?
   
   Could you first let me know what error message we will get when `TimeoutException or InterruptedException` thrown?
       
   > 3. What happened if the future throws CancellationException? return null? Is there any better way to handle it?
   
   Now, it will throw RuntimeException, which works, too. But I think we can directly `fail` the test, like L 562 did.
   



-- 
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: jira-unsubscribe@kafka.apache.org

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


Re: [PR] KAFKA-16918: TestUtils#assertFutureThrows should use future.get with timeout [kafka]

Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #16264:
URL: https://github.com/apache/kafka/pull/16264#discussion_r1639351349


##########
clients/src/test/java/org/apache/kafka/test/TestUtils.java:
##########
@@ -556,10 +558,20 @@ public static Set<TopicPartition> generateRandomTopicPartitions(int numTopic, in
      * @return The caught exception cause
      */
     public static <T extends Throwable> T assertFutureThrows(Future<?> future, Class<T> exceptionCauseClass) {
-        ExecutionException exception = assertThrows(ExecutionException.class, future::get);
-        assertInstanceOf(exceptionCauseClass, exception.getCause(),
-            "Unexpected exception cause " + exception.getCause());
-        return exceptionCauseClass.cast(exception.getCause());

Review Comment:
   The goal of this PR, is to maintain the same semantics (the logic) as before, but make the future.get has time limit.
   So, we should not change the assert logic here.
   Maybe you should understand the original logic first, and then making other changes.
   Some questions you need to know first is:
   1. What will happen when future.get has no exception thrown?
   2. What will happen when future.get throws ExecutionException?
   3. What's exceptionCauseClass used for?
   4. What's the return value?
   



-- 
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: jira-unsubscribe@kafka.apache.org

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


Re: [PR] KAFKA-16918: TestUtils#assertFutureThrows should use future.get with timeout [kafka]

Posted by "chiacyu (via GitHub)" <gi...@apache.org>.
chiacyu commented on code in PR #16264:
URL: https://github.com/apache/kafka/pull/16264#discussion_r1636492176


##########
clients/src/test/java/org/apache/kafka/test/TestUtils.java:
##########
@@ -556,10 +558,22 @@ public static Set<TopicPartition> generateRandomTopicPartitions(int numTopic, in
      * @return The caught exception cause
      */
     public static <T extends Throwable> T assertFutureThrows(Future<?> future, Class<T> exceptionCauseClass) {
-        ExecutionException exception = assertThrows(ExecutionException.class, future::get);
-        assertInstanceOf(exceptionCauseClass, exception.getCause(),
-            "Unexpected exception cause " + exception.getCause());
-        return exceptionCauseClass.cast(exception.getCause());
+        try {
+            future.get(5, TimeUnit.SECONDS);
+            fail("expected to throw ExecutionException...");
+        } catch (TimeoutException e) {
+            fail("timeout waiting");
+            return null;
+        } catch (ExecutionException e) {
+            ExecutionException exception = assertThrows(ExecutionException.class, future::get);
+            assertInstanceOf(exceptionCauseClass, exception.getCause(),
+                    "Unexpected exception cause " + exception.getCause());
+            return exceptionCauseClass.cast(exception.getCause());
+        } catch (InterruptedException e) {
+            fail("Unexpected exception cause" + e.getCause());
+            return null;
+        }
+        return null;

Review Comment:
   Thanks for the comments here's some of my though:
   - The `fail()` will throw a `AssertionFailedError(message)`.
   - if there's no exception thrown, we should return assertion failed or null value to represent no exception thrown.
   WDYT?



-- 
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: jira-unsubscribe@kafka.apache.org

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


Re: [PR] KAFKA-16918: TestUtils#assertFutureThrows should use future.get with timeout [kafka]

Posted by "chiacyu (via GitHub)" <gi...@apache.org>.
chiacyu commented on code in PR #16264:
URL: https://github.com/apache/kafka/pull/16264#discussion_r1636492176


##########
clients/src/test/java/org/apache/kafka/test/TestUtils.java:
##########
@@ -556,10 +558,22 @@ public static Set<TopicPartition> generateRandomTopicPartitions(int numTopic, in
      * @return The caught exception cause
      */
     public static <T extends Throwable> T assertFutureThrows(Future<?> future, Class<T> exceptionCauseClass) {
-        ExecutionException exception = assertThrows(ExecutionException.class, future::get);
-        assertInstanceOf(exceptionCauseClass, exception.getCause(),
-            "Unexpected exception cause " + exception.getCause());
-        return exceptionCauseClass.cast(exception.getCause());
+        try {
+            future.get(5, TimeUnit.SECONDS);
+            fail("expected to throw ExecutionException...");
+        } catch (TimeoutException e) {
+            fail("timeout waiting");
+            return null;
+        } catch (ExecutionException e) {
+            ExecutionException exception = assertThrows(ExecutionException.class, future::get);
+            assertInstanceOf(exceptionCauseClass, exception.getCause(),
+                    "Unexpected exception cause " + exception.getCause());
+            return exceptionCauseClass.cast(exception.getCause());
+        } catch (InterruptedException e) {
+            fail("Unexpected exception cause" + e.getCause());
+            return null;
+        }
+        return null;

Review Comment:
   Thanks for the comments here's some of my though:
   - The `fail()` will throw a `AssertionFailedError(message)`.
   - if there's no exception thrown, we should return assertion failed.
   WDYT?



-- 
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: jira-unsubscribe@kafka.apache.org

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


Re: [PR] KAFKA-16918: TestUtils#assertFutureThrows should use future.get with timeout [kafka]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #16264:
URL: https://github.com/apache/kafka/pull/16264#issuecomment-2635629251

   This PR has been closed since it has not had any activity in 120 days. If you feel like this
   was a mistake, or you would like to continue working on it, please feel free to re-open the 
   PR and ask for a review.


-- 
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: jira-unsubscribe@kafka.apache.org

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


Re: [PR] KAFKA-16918: TestUtils#assertFutureThrows should use future.get with timeout [kafka]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #16264: KAFKA-16918: TestUtils#assertFutureThrows should use future.get with timeout
URL: https://github.com/apache/kafka/pull/16264


-- 
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: jira-unsubscribe@kafka.apache.org

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