4
4
import org .junit .Before ;
5
5
import org .junit .Rule ;
6
6
import org .junit .Test ;
7
+ import org .junit .rules .TemporaryFolder ;
7
8
import org .junit .rules .TestRule ;
8
9
import org .junit .rules .Timeout ;
9
10
import org .junit .runner .JUnitCore ;
10
11
import org .junit .runner .Result ;
11
12
13
+ import java .io .File ;
14
+ import java .io .IOException ;
15
+ import java .io .InterruptedIOException ;
16
+ import java .io .RandomAccessFile ;
17
+ import java .nio .ByteBuffer ;
18
+ import java .nio .channels .FileChannel ;
19
+ import java .util .Random ;
12
20
import java .util .concurrent .TimeUnit ;
13
21
import java .util .concurrent .locks .ReentrantLock ;
14
22
@@ -24,6 +32,9 @@ public class TimeoutRuleTest {
24
32
public abstract static class AbstractTimeoutTest {
25
33
public static final StringBuffer logger = new StringBuffer ();
26
34
35
+ @ Rule
36
+ public final TemporaryFolder tmpFile = new TemporaryFolder ();
37
+
27
38
@ Test
28
39
public void run1 () throws InterruptedException {
29
40
logger .append ("run1" );
@@ -49,6 +60,30 @@ public void run4() {
49
60
while (!run4done ) {
50
61
}
51
62
}
63
+
64
+ @ Test
65
+ public void run5 () throws IOException {
66
+ logger .append ("run5" );
67
+ Random rnd = new Random ();
68
+ byte [] data = new byte [1024 ];
69
+ File tmp = tmpFile .newFile ();
70
+ while (true ) {
71
+ FileChannel channel = new RandomAccessFile (tmp , "rw" ).getChannel ();
72
+ rnd .nextBytes (data );
73
+ ByteBuffer buffer = ByteBuffer .wrap (data );
74
+ // Interrupted thread closes channel and throws ClosedByInterruptException.
75
+ channel .write (buffer );
76
+ channel .close ();
77
+ tmp .delete ();
78
+ }
79
+ }
80
+
81
+ @ Test
82
+ public void run6 () throws InterruptedIOException {
83
+ logger .append ("run6" );
84
+ // Java IO throws InterruptedIOException only on SUN machines.
85
+ throw new InterruptedIOException ();
86
+ }
52
87
}
53
88
54
89
public static class HasGlobalLongTimeout extends AbstractTimeoutTest {
@@ -71,29 +106,34 @@ public void before() {
71
106
72
107
@ After
73
108
public void after () {
74
- run4done = true ;//to make sure that the thread won't continue at run4()
109
+ // set run4done to make sure that the thread won't continue at run4()
110
+ run4done = true ;
75
111
run1Lock .unlock ();
76
112
}
77
113
78
114
@ Test
79
115
public void timeUnitTimeout () throws InterruptedException {
80
116
HasGlobalTimeUnitTimeout .logger .setLength (0 );
81
117
Result result = JUnitCore .runClasses (HasGlobalTimeUnitTimeout .class );
82
- assertEquals (4 , result .getFailureCount ());
118
+ assertEquals (6 , result .getFailureCount ());
83
119
assertThat (HasGlobalTimeUnitTimeout .logger .toString (), containsString ("run1" ));
84
120
assertThat (HasGlobalTimeUnitTimeout .logger .toString (), containsString ("run2" ));
85
121
assertThat (HasGlobalTimeUnitTimeout .logger .toString (), containsString ("run3" ));
86
122
assertThat (HasGlobalTimeUnitTimeout .logger .toString (), containsString ("run4" ));
123
+ assertThat (HasGlobalTimeUnitTimeout .logger .toString (), containsString ("run5" ));
124
+ assertThat (HasGlobalTimeUnitTimeout .logger .toString (), containsString ("run6" ));
87
125
}
88
126
89
127
@ Test
90
128
public void longTimeout () throws InterruptedException {
91
129
HasGlobalLongTimeout .logger .setLength (0 );
92
130
Result result = JUnitCore .runClasses (HasGlobalLongTimeout .class );
93
- assertEquals (4 , result .getFailureCount ());
131
+ assertEquals (6 , result .getFailureCount ());
94
132
assertThat (HasGlobalLongTimeout .logger .toString (), containsString ("run1" ));
95
133
assertThat (HasGlobalLongTimeout .logger .toString (), containsString ("run2" ));
96
134
assertThat (HasGlobalLongTimeout .logger .toString (), containsString ("run3" ));
97
135
assertThat (HasGlobalLongTimeout .logger .toString (), containsString ("run4" ));
136
+ assertThat (HasGlobalLongTimeout .logger .toString (), containsString ("run5" ));
137
+ assertThat (HasGlobalLongTimeout .logger .toString (), containsString ("run6" ));
98
138
}
99
139
}
0 commit comments