1 |
/* |
2 |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
* public domain. Use, modify, and redistribute this code in any way |
4 |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.*; |
10 |
import java.util.concurrent.*; |
11 |
|
12 |
public class CyclicBarrierTest extends JSR166TestCase{ |
13 |
|
14 |
public static void main(String[] args) { |
15 |
junit.textui.TestRunner.run (suite()); |
16 |
} |
17 |
|
18 |
|
19 |
public static Test suite() { |
20 |
return new TestSuite(CyclicBarrierTest.class); |
21 |
} |
22 |
|
23 |
public void testConstructor1(){ |
24 |
try{ |
25 |
new CyclicBarrier(-1, (Runnable)null); |
26 |
fail("should throw"); |
27 |
} catch(IllegalArgumentException e){} |
28 |
} |
29 |
|
30 |
public void testConstructor2(){ |
31 |
try{ |
32 |
new CyclicBarrier(-1); |
33 |
fail("should throw"); |
34 |
} catch(IllegalArgumentException e){} |
35 |
} |
36 |
|
37 |
public void testConstructor3(){ |
38 |
CyclicBarrier b = new CyclicBarrier(2); |
39 |
assertEquals(2, b.getParties()); |
40 |
assertEquals(0, b.getNumberWaiting()); |
41 |
} |
42 |
|
43 |
public void testSingleParty() { |
44 |
try { |
45 |
CyclicBarrier b = new CyclicBarrier(1); |
46 |
assertEquals(1, b.getParties()); |
47 |
assertEquals(0, b.getNumberWaiting()); |
48 |
b.await(); |
49 |
b.await(); |
50 |
assertEquals(0, b.getNumberWaiting()); |
51 |
} |
52 |
catch(Exception e) { |
53 |
fail("unexpected exception"); |
54 |
} |
55 |
} |
56 |
|
57 |
private volatile int countAction; |
58 |
private class MyAction implements Runnable { |
59 |
public void run() { ++countAction; } |
60 |
} |
61 |
|
62 |
public void testBarrierAction() { |
63 |
try { |
64 |
countAction = 0; |
65 |
CyclicBarrier b = new CyclicBarrier(1, new MyAction()); |
66 |
assertEquals(1, b.getParties()); |
67 |
assertEquals(0, b.getNumberWaiting()); |
68 |
b.await(); |
69 |
b.await(); |
70 |
assertEquals(0, b.getNumberWaiting()); |
71 |
assertEquals(countAction, 2); |
72 |
} |
73 |
catch(Exception e) { |
74 |
fail("unexpected exception"); |
75 |
} |
76 |
} |
77 |
|
78 |
|
79 |
public void testTwoParties(){ |
80 |
final CyclicBarrier b = new CyclicBarrier(2); |
81 |
Thread t = new Thread(new Runnable() { |
82 |
public void run(){ |
83 |
try { |
84 |
b.await(); |
85 |
b.await(); |
86 |
b.await(); |
87 |
b.await(); |
88 |
} catch(Exception e){ |
89 |
threadFail("unexpected exception"); |
90 |
}}}); |
91 |
|
92 |
try { |
93 |
t.start(); |
94 |
b.await(); |
95 |
b.await(); |
96 |
b.await(); |
97 |
b.await(); |
98 |
t.join(); |
99 |
} catch(Exception e){ |
100 |
fail("unexpected exception"); |
101 |
} |
102 |
} |
103 |
|
104 |
|
105 |
public void testAwait1_Interrupted_BrokenBarrier(){ |
106 |
final CyclicBarrier c = new CyclicBarrier(3); |
107 |
Thread t1 = new Thread(new Runnable() { |
108 |
public void run(){ |
109 |
try{ |
110 |
c.await(); |
111 |
threadFail("should throw"); |
112 |
} catch(InterruptedException success){} |
113 |
catch(Exception b){ |
114 |
threadFail("should throw IE"); |
115 |
} |
116 |
} |
117 |
}); |
118 |
Thread t2 = new Thread(new Runnable(){ |
119 |
public void run(){ |
120 |
try{ |
121 |
c.await(); |
122 |
threadFail("should throw"); |
123 |
} catch(BrokenBarrierException success){ |
124 |
} catch(Exception i){ |
125 |
threadFail("should throw BBE"); |
126 |
} |
127 |
} |
128 |
}); |
129 |
try { |
130 |
t1.start(); |
131 |
t2.start(); |
132 |
Thread.sleep(SHORT_DELAY_MS); |
133 |
t1.interrupt(); |
134 |
t1.join(); |
135 |
t2.join(); |
136 |
} catch(InterruptedException e){ |
137 |
fail("unexpected exception"); |
138 |
} |
139 |
} |
140 |
|
141 |
public void testAwait2_Interrupted_BrokenBarrier(){ |
142 |
final CyclicBarrier c = new CyclicBarrier(3); |
143 |
Thread t1 = new Thread(new Runnable() { |
144 |
public void run(){ |
145 |
try{ |
146 |
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
147 |
threadFail("should throw"); |
148 |
} catch(InterruptedException success){ |
149 |
} catch(Exception b){ |
150 |
threadFail("should throw IE"); |
151 |
} |
152 |
} |
153 |
}); |
154 |
Thread t2 = new Thread(new Runnable(){ |
155 |
public void run(){ |
156 |
try{ |
157 |
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
158 |
threadFail("should throw"); |
159 |
} catch(BrokenBarrierException success){ |
160 |
} catch(Exception i){ |
161 |
threadFail("should throw BBE"); |
162 |
} |
163 |
} |
164 |
}); |
165 |
try { |
166 |
t1.start(); |
167 |
t2.start(); |
168 |
Thread.sleep(SHORT_DELAY_MS); |
169 |
t1.interrupt(); |
170 |
t1.join(); |
171 |
t2.join(); |
172 |
} catch(InterruptedException e){ |
173 |
fail("unexpected exception"); |
174 |
} |
175 |
} |
176 |
|
177 |
public void testAwait3_TimeOutException(){ |
178 |
final CyclicBarrier c = new CyclicBarrier(2); |
179 |
Thread t = new Thread(new Runnable() { |
180 |
public void run(){ |
181 |
try{ |
182 |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
183 |
threadFail("should throw"); |
184 |
} catch(TimeoutException success){ |
185 |
} catch(Exception b){ |
186 |
threadFail("should throw TOE"); |
187 |
|
188 |
} |
189 |
} |
190 |
}); |
191 |
try { |
192 |
t.start(); |
193 |
t.join(); |
194 |
} catch(InterruptedException e){ |
195 |
fail("unexpected exception"); |
196 |
} |
197 |
} |
198 |
|
199 |
} |