1
1
package cucumber .runtime ;
2
2
3
+ import org .junit .Rule ;
3
4
import org .junit .Test ;
5
+ import org .junit .rules .ExpectedException ;
4
6
5
7
import java .util .concurrent .CountDownLatch ;
6
8
import java .util .concurrent .TimeoutException ;
11
13
import static org .junit .Assert .fail ;
12
14
13
15
public class TimeoutTest {
16
+ @ Rule
17
+ public ExpectedException exception = ExpectedException .none ();
18
+
14
19
@ Test
15
20
public void doesnt_time_out_if_it_doesnt_take_too_long () throws Throwable {
16
21
final Slow slow = new Slow ();
@@ -23,42 +28,54 @@ public String call() throws Throwable {
23
28
assertEquals ("slept 10ms" , what );
24
29
}
25
30
26
- @ Test ( expected = TimeoutException . class )
31
+ @ Test
27
32
public void times_out_if_it_takes_too_long () throws Throwable {
28
- final Slow slow = new Slow ();
29
- Timeout .timeout (new Timeout .Callback <String >() {
30
- @ Override
31
- public String call () throws Throwable {
32
- return slow .slow (100 );
33
- }
34
- }, 50 );
35
- fail ();
33
+ try {
34
+ final Slow slow = new Slow ();
35
+ Timeout .timeout (new Timeout .Callback <String >() {
36
+ @ Override
37
+ public String call () throws Throwable {
38
+ return slow .slow (100 );
39
+ }
40
+ }, 50 );
41
+ fail ();
42
+ } catch (TimeoutException expected ) {
43
+ assertEquals ("Timed out after 50ms." , expected .getMessage ());
44
+ }
36
45
}
37
46
38
- @ Test (expected = TimeoutException .class )
39
- public void times_out_infinite_loop_if_it_takes_too_long () throws Throwable {
40
- final Slow slow = new Slow ();
41
- Timeout .timeout (new Timeout .Callback <Void >() {
42
- @ Override
43
- public Void call () throws Throwable {
44
- slow .infinite ();
45
- return null ;
46
- }
47
- }, 10 );
48
- fail ();
47
+ @ Test
48
+ public void times_out_infinite_spin_loop_if_it_takes_too_long () throws Throwable {
49
+ try {
50
+ final Slow slow = new Slow ();
51
+ Timeout .timeout (new Timeout .Callback <Void >() {
52
+ @ Override
53
+ public Void call () throws Throwable {
54
+ slow .infiniteSpin ();
55
+ return null ;
56
+ }
57
+ }, 10 );
58
+ fail ();
59
+ } catch (TimeoutException expected ) {
60
+ assertEquals ("Timed out after 10ms. (Stopped the thread was uninterruptible)." , expected .getMessage ());
61
+ }
49
62
}
50
63
51
- @ Test ( expected = TimeoutException . class )
64
+ @ Test
52
65
public void times_out_infinite_latch_wait_if_it_takes_too_long () throws Throwable {
53
- final Slow slow = new Slow ();
54
- Timeout .timeout (new Timeout .Callback <Void >() {
55
- @ Override
56
- public Void call () throws Throwable {
57
- slow .infiniteLatchWait ();
58
- return null ;
59
- }
60
- }, 10 );
61
- fail ();
66
+ try {
67
+ final Slow slow = new Slow ();
68
+ Timeout .timeout (new Timeout .Callback <Void >() {
69
+ @ Override
70
+ public Void call () throws Throwable {
71
+ slow .infiniteLatchWait ();
72
+ return null ;
73
+ }
74
+ }, 10 );
75
+ fail ();
76
+ } catch (TimeoutException expected ) {
77
+ assertEquals ("Timed out after 10ms." , expected .getMessage ());
78
+ }
62
79
}
63
80
64
81
@ Test
@@ -93,9 +110,8 @@ public String slow(int millis) throws InterruptedException {
93
110
return String .format ("slept %sms" , millis );
94
111
}
95
112
96
- public void infinite () throws InterruptedException {
113
+ public void infiniteSpin () throws InterruptedException {
97
114
while (true ) {
98
- sleep (1 );
99
115
}
100
116
}
101
117
0 commit comments