Skip to content

Commit f37379c

Browse files
committed
Test for exceptional handling of Promise.orTimeout
1 parent 56a1228 commit f37379c

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.tascalate.concurrent;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.time.Duration;
6+
import java.util.concurrent.TimeUnit;
7+
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
public class OrTimeoutExceptionTests {
13+
14+
private TaskExecutorService executor;
15+
16+
@Before
17+
public void setUp() {
18+
executor = TaskExecutors.newFixedThreadPool(4);
19+
}
20+
21+
@After
22+
public void tearDown() {
23+
executor.shutdown();
24+
}
25+
26+
@Test
27+
public void testExceptionalOrTimeout() {
28+
Promise<?> p =
29+
CompletableTask
30+
.submit(this::doTask, executor)
31+
.orTimeout(Duration.ofSeconds(5L))
32+
.whenComplete((res, err) -> {
33+
if (res != null)
34+
System.out.println("RESULT: " + res);
35+
else
36+
System.out.println("ERROR: " + err.getMessage());
37+
});
38+
p.join();
39+
}
40+
41+
private String doTask () throws Exception {
42+
TimeUnit.SECONDS.sleep(3);
43+
if (null != System.out)
44+
throw new Exception("my error");
45+
return "executed ok";
46+
}
47+
48+
}

0 commit comments

Comments
 (0)