File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
src/test/java/net/tascalate/concurrent Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments