1996fanrui commented on code in PR #2243:
URL:
https://github.com/apache/incubator-streampark/pull/2243#discussion_r1065361014
##########
streampark-console/streampark-console-service/src/test/java/org/apache/org/apache/streampark/common/util/CompletableFutureUtilsTest.java:
##########
@@ -143,4 +147,77 @@ private String runStart(int sec) {
}
return "start successful";
}
+
+ @Test
+ public void thenSupplyNormally() throws Exception {
+ String successResult = "success";
+ String exceptionResult = "error";
+
+ String resp =
+ CompletableFutureUtils.supplyTimeout(
+ CompletableFuture.supplyAsync(() -> successResult),
+ 3,
+ TimeUnit.SECONDS,
+ success -> success,
+ e -> exceptionResult)
+ .thenApply(r -> r)
+ .get();
+
+ Assertions.assertEquals(resp, successResult);
+ }
+
+ @Test
+ public void thenSupplyTimeout() throws Exception {
+ String successResult = "success";
+ String exceptionResult = "error";
+ CompletableFuture<String> future =
+ CompletableFuture.supplyAsync(
+ () -> {
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ return successResult;
+ });
+ String resp =
+ CompletableFutureUtils.supplyTimeout(
+ future, 1, TimeUnit.SECONDS, success -> success, e ->
exceptionResult)
+ .thenApply(r -> r)
+ .get();
+ Assertions.assertEquals(resp, exceptionResult);
+ }
+
+ @Test
+ public void thenSupplyException() {
+ String resp;
+ String exResult = "exception";
+ try {
+ resp = exceptionally();
+ } catch (Exception e) {
+ resp = exResult;
+ }
+ Assertions.assertEquals(resp, exResult);
+ }
+
+ private String exceptionally() throws Exception {
+ return CompletableFutureUtils.supplyTimeout(
+ CompletableFuture.supplyAsync(
+ () -> {
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ return "success";
+ }),
+ 2,
+ TimeUnit.SECONDS,
+ success -> success,
+ e -> {
+ throw new RuntimeException("exception");
+ })
+ .thenApply(r -> r)
+ .get();
+ }
Review Comment:
```suggestion
@Test
public void thenSupplyException() {
String expectedExceptionMessage = "Exception in exceptionally handler";
CompletableFuture<String> future =
CompletableFutureUtils.supplyTimeout(
CompletableFuture.supplyAsync(
() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return "success";
}),
2,
TimeUnit.SECONDS,
success -> success,
e -> {
throw new RuntimeException(expectedExceptionMessage);
});
assertThatThrownBy(future::get)
.cause()
.as(expectedExceptionMessage)
.isInstanceOf(RuntimeException.class);
}
```
Using the `assertThatThrownBy` instead of try catch.
--
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]