ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CyclicBarrierTest.java
Revision: 1.3
Committed: Sat Sep 20 18:20:07 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.2: +68 -44 lines
Log Message:
Documentation scaffolding

File Contents

# Content
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 public static void main(String[] args) {
14 junit.textui.TestRunner.run (suite());
15 }
16 public static Test suite() {
17 return new TestSuite(CyclicBarrierTest.class);
18 }
19
20 private volatile int countAction;
21 private class MyAction implements Runnable {
22 public void run() { ++countAction; }
23 }
24
25 /**
26 *
27 */
28 public void testConstructor1() {
29 try {
30 new CyclicBarrier(-1, (Runnable)null);
31 shouldThrow();
32 } catch(IllegalArgumentException e){}
33 }
34
35 /**
36 *
37 */
38 public void testConstructor2() {
39 try {
40 new CyclicBarrier(-1);
41 shouldThrow();
42 } catch(IllegalArgumentException e){}
43 }
44
45 /**
46 *
47 */
48 public void testConstructor3() {
49 CyclicBarrier b = new CyclicBarrier(2);
50 assertEquals(2, b.getParties());
51 assertEquals(0, b.getNumberWaiting());
52 }
53
54 /**
55 *
56 */
57 public void testSingleParty() {
58 try {
59 CyclicBarrier b = new CyclicBarrier(1);
60 assertEquals(1, b.getParties());
61 assertEquals(0, b.getNumberWaiting());
62 b.await();
63 b.await();
64 assertEquals(0, b.getNumberWaiting());
65 }
66 catch(Exception e) {
67 unexpectedException();
68 }
69 }
70
71 /**
72 *
73 */
74 public void testBarrierAction() {
75 try {
76 countAction = 0;
77 CyclicBarrier b = new CyclicBarrier(1, new MyAction());
78 assertEquals(1, b.getParties());
79 assertEquals(0, b.getNumberWaiting());
80 b.await();
81 b.await();
82 assertEquals(0, b.getNumberWaiting());
83 assertEquals(countAction, 2);
84 }
85 catch(Exception e) {
86 unexpectedException();
87 }
88 }
89
90
91 /**
92 *
93 */
94 public void testTwoParties() {
95 final CyclicBarrier b = new CyclicBarrier(2);
96 Thread t = new Thread(new Runnable() {
97 public void run() {
98 try {
99 b.await();
100 b.await();
101 b.await();
102 b.await();
103 } catch(Exception e){
104 threadUnexpectedException();
105 }}});
106
107 try {
108 t.start();
109 b.await();
110 b.await();
111 b.await();
112 b.await();
113 t.join();
114 } catch(Exception e){
115 unexpectedException();
116 }
117 }
118
119
120 /**
121 *
122 */
123 public void testAwait1_Interrupted_BrokenBarrier() {
124 final CyclicBarrier c = new CyclicBarrier(3);
125 Thread t1 = new Thread(new Runnable() {
126 public void run() {
127 try {
128 c.await();
129 threadShouldThrow();
130 } catch(InterruptedException success){}
131 catch(Exception b){
132 threadFail("should throw IE");
133 }
134 }
135 });
136 Thread t2 = new Thread(new Runnable() {
137 public void run() {
138 try {
139 c.await();
140 threadShouldThrow();
141 } catch(BrokenBarrierException success){
142 } catch(Exception i){
143 threadFail("should throw BBE");
144 }
145 }
146 });
147 try {
148 t1.start();
149 t2.start();
150 Thread.sleep(SHORT_DELAY_MS);
151 t1.interrupt();
152 t1.join();
153 t2.join();
154 } catch(InterruptedException e){
155 unexpectedException();
156 }
157 }
158
159 /**
160 *
161 */
162 public void testAwait2_Interrupted_BrokenBarrier() {
163 final CyclicBarrier c = new CyclicBarrier(3);
164 Thread t1 = new Thread(new Runnable() {
165 public void run() {
166 try {
167 c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
168 threadShouldThrow();
169 } catch(InterruptedException success){
170 } catch(Exception b){
171 threadFail("should throw IE");
172 }
173 }
174 });
175 Thread t2 = new Thread(new Runnable() {
176 public void run() {
177 try {
178 c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
179 threadShouldThrow();
180 } catch(BrokenBarrierException success){
181 } catch(Exception i){
182 threadFail("should throw BBE");
183 }
184 }
185 });
186 try {
187 t1.start();
188 t2.start();
189 Thread.sleep(SHORT_DELAY_MS);
190 t1.interrupt();
191 t1.join();
192 t2.join();
193 } catch(InterruptedException e){
194 unexpectedException();
195 }
196 }
197
198 /**
199 *
200 */
201 public void testAwait3_TimeOutException() {
202 final CyclicBarrier c = new CyclicBarrier(2);
203 Thread t = new Thread(new Runnable() {
204 public void run() {
205 try {
206 c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
207 threadShouldThrow();
208 } catch(TimeoutException success){
209 } catch(Exception b){
210 threadFail("should throw TOE");
211
212 }
213 }
214 });
215 try {
216 t.start();
217 t.join();
218 } catch(InterruptedException e){
219 unexpectedException();
220 }
221 }
222
223 }