ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CyclicBarrierTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CyclicBarrierTest.java (file contents):
Revision 1.14 by jsr166, Sat Nov 21 06:26:00 2009 UTC vs.
Revision 1.30 by jsr166, Sun Jan 7 23:11:07 2018 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
12 import java.util.concurrent.locks.*;
13 import java.util.concurrent.atomic.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10  
11 + import java.util.concurrent.BrokenBarrierException;
12 + import java.util.concurrent.CountDownLatch;
13 + import java.util.concurrent.CyclicBarrier;
14 + import java.util.concurrent.TimeoutException;
15 + import java.util.concurrent.atomic.AtomicBoolean;
16 + import java.util.concurrent.atomic.AtomicInteger;
17 +
18 + import junit.framework.Test;
19 + import junit.framework.TestSuite;
20 +
21   public class CyclicBarrierTest extends JSR166TestCase {
22      public static void main(String[] args) {
23 <        junit.textui.TestRunner.run (suite());
23 >        main(suite(), args);
24      }
25      public static Test suite() {
26          return new TestSuite(CyclicBarrierTest.class);
27      }
28  
29 <    private volatile int countAction;
30 <    private class MyAction implements Runnable {
31 <        public void run() { ++countAction; }
29 >    /**
30 >     * Spin-waits till the number of waiters == numberOfWaiters.
31 >     */
32 >    void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) {
33 >        long startTime = System.nanoTime();
34 >        while (barrier.getNumberWaiting() != numberOfWaiters) {
35 >            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
36 >                fail("timed out");
37 >            Thread.yield();
38 >        }
39      }
40  
41      /**
42 <     * Creating with negative parties throws IAE
42 >     * Creating with negative parties throws IllegalArgumentException
43       */
44      public void testConstructor1() {
45          try {
# Line 37 | Line 49 | public class CyclicBarrierTest extends J
49      }
50  
51      /**
52 <     * Creating with negative parties and no action throws IAE
52 >     * Creating with negative parties and no action throws
53 >     * IllegalArgumentException
54       */
55      public void testConstructor2() {
56          try {
# Line 71 | Line 84 | public class CyclicBarrierTest extends J
84       * The supplied barrier action is run at barrier
85       */
86      public void testBarrierAction() throws Exception {
87 <        countAction = 0;
88 <        CyclicBarrier b = new CyclicBarrier(1, new MyAction());
87 >        final AtomicInteger count = new AtomicInteger(0);
88 >        final Runnable incCount = new Runnable() { public void run() {
89 >            count.getAndIncrement(); }};
90 >        CyclicBarrier b = new CyclicBarrier(1, incCount);
91          assertEquals(1, b.getParties());
92          assertEquals(0, b.getNumberWaiting());
93          b.await();
94          b.await();
95          assertEquals(0, b.getNumberWaiting());
96 <        assertEquals(countAction, 2);
96 >        assertEquals(2, count.get());
97      }
98  
99      /**
# Line 86 | Line 101 | public class CyclicBarrierTest extends J
101       */
102      public void testTwoParties() throws Exception {
103          final CyclicBarrier b = new CyclicBarrier(2);
104 <        Thread t = new Thread(new CheckedRunnable() {
104 >        Thread t = newStartedThread(new CheckedRunnable() {
105              public void realRun() throws Exception {
106                  b.await();
107                  b.await();
# Line 94 | Line 109 | public class CyclicBarrierTest extends J
109                  b.await();
110              }});
111  
97        t.start();
112          b.await();
113          b.await();
114          b.await();
115          b.await();
116 <        t.join();
116 >        awaitTermination(t);
117      }
118  
105
119      /**
120       * An interruption in one party causes others waiting in await to
121       * throw BrokenBarrierException
122       */
123 <    public void testAwait1_Interrupted_BrokenBarrier() throws Exception {
123 >    public void testAwait1_Interrupted_BrokenBarrier() {
124          final CyclicBarrier c = new CyclicBarrier(3);
125 +        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
126          Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
127              public void realRun() throws Exception {
128 +                pleaseInterrupt.countDown();
129                  c.await();
130              }};
131          Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
132              public void realRun() throws Exception {
133 +                pleaseInterrupt.countDown();
134                  c.await();
135              }};
136  
137          t1.start();
138          t2.start();
139 <        Thread.sleep(SHORT_DELAY_MS);
139 >        await(pleaseInterrupt);
140          t1.interrupt();
141 <        t1.join();
142 <        t2.join();
141 >        awaitTermination(t1);
142 >        awaitTermination(t2);
143      }
144  
145      /**
# Line 132 | Line 148 | public class CyclicBarrierTest extends J
148       */
149      public void testAwait2_Interrupted_BrokenBarrier() throws Exception {
150          final CyclicBarrier c = new CyclicBarrier(3);
151 +        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
152          Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
153              public void realRun() throws Exception {
154 +                pleaseInterrupt.countDown();
155                  c.await(LONG_DELAY_MS, MILLISECONDS);
156              }};
157          Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
158              public void realRun() throws Exception {
159 +                pleaseInterrupt.countDown();
160                  c.await(LONG_DELAY_MS, MILLISECONDS);
161              }};
162  
163          t1.start();
164          t2.start();
165 <        Thread.sleep(SHORT_DELAY_MS);
165 >        await(pleaseInterrupt);
166          t1.interrupt();
167 <        t1.join();
168 <        t2.join();
167 >        awaitTermination(t1);
168 >        awaitTermination(t2);
169      }
170  
171      /**
172       * A timeout in timed await throws TimeoutException
173       */
174 <    public void testAwait3_TimeOutException() throws InterruptedException {
174 >    public void testAwait3_TimeoutException() throws InterruptedException {
175          final CyclicBarrier c = new CyclicBarrier(2);
176 <        Thread t = new ThreadShouldThrow(TimeoutException.class) {
176 >        Thread t = newStartedThread(new CheckedRunnable() {
177              public void realRun() throws Exception {
178 <                c.await(SHORT_DELAY_MS, MILLISECONDS);
179 <            }};
178 >                long startTime = System.nanoTime();
179 >                try {
180 >                    c.await(timeoutMillis(), MILLISECONDS);
181 >                    shouldThrow();
182 >                } catch (TimeoutException success) {}
183 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
184 >            }});
185  
186 <        t.start();
163 <        t.join();
186 >        awaitTermination(t);
187      }
188  
189      /**
# Line 169 | Line 192 | public class CyclicBarrierTest extends J
192       */
193      public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException {
194          final CyclicBarrier c = new CyclicBarrier(3);
195 <        Thread t1 = new ThreadShouldThrow(TimeoutException.class) {
195 >        Thread t1 = newStartedThread(new CheckedRunnable() {
196              public void realRun() throws Exception {
197 <                c.await(SHORT_DELAY_MS, MILLISECONDS);
198 <            }};
199 <        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
197 >                try {
198 >                    c.await(LONG_DELAY_MS, MILLISECONDS);
199 >                    shouldThrow();
200 >                } catch (BrokenBarrierException success) {}
201 >            }});
202 >        Thread t2 = newStartedThread(new CheckedRunnable() {
203              public void realRun() throws Exception {
204 <                c.await(MEDIUM_DELAY_MS, MILLISECONDS);
205 <            }};
204 >                awaitNumberWaiting(c, 1);
205 >                long startTime = System.nanoTime();
206 >                try {
207 >                    c.await(timeoutMillis(), MILLISECONDS);
208 >                    shouldThrow();
209 >                } catch (TimeoutException success) {}
210 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
211 >            }});
212  
213 <        t1.start();
214 <        t2.start();
183 <        t1.join();
184 <        t2.join();
213 >        awaitTermination(t1);
214 >        awaitTermination(t2);
215      }
216  
217      /**
# Line 190 | Line 220 | public class CyclicBarrierTest extends J
220       */
221      public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException {
222          final CyclicBarrier c = new CyclicBarrier(3);
223 <        Thread t1 = new ThreadShouldThrow(TimeoutException.class) {
223 >        Thread t1 = newStartedThread(new CheckedRunnable() {
224              public void realRun() throws Exception {
225 <                c.await(SHORT_DELAY_MS, MILLISECONDS);
226 <            }};
227 <        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
225 >                try {
226 >                    c.await();
227 >                    shouldThrow();
228 >                } catch (BrokenBarrierException success) {}
229 >            }});
230 >        Thread t2 = newStartedThread(new CheckedRunnable() {
231              public void realRun() throws Exception {
232 <                c.await();
233 <            }};
232 >                awaitNumberWaiting(c, 1);
233 >                long startTime = System.nanoTime();
234 >                try {
235 >                    c.await(timeoutMillis(), MILLISECONDS);
236 >                    shouldThrow();
237 >                } catch (TimeoutException success) {}
238 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
239 >            }});
240  
241 <        t1.start();
242 <        t2.start();
204 <        t1.join();
205 <        t2.join();
241 >        awaitTermination(t1);
242 >        awaitTermination(t2);
243      }
244  
245      /**
# Line 211 | Line 248 | public class CyclicBarrierTest extends J
248       */
249      public void testReset_BrokenBarrier() throws InterruptedException {
250          final CyclicBarrier c = new CyclicBarrier(3);
251 +        final CountDownLatch pleaseReset = new CountDownLatch(2);
252          Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
253              public void realRun() throws Exception {
254 +                pleaseReset.countDown();
255                  c.await();
256              }};
257          Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
258              public void realRun() throws Exception {
259 +                pleaseReset.countDown();
260                  c.await();
261              }};
262  
263          t1.start();
264          t2.start();
265 <        Thread.sleep(SHORT_DELAY_MS);
265 >        await(pleaseReset);
266 >
267 >        awaitNumberWaiting(c, 2);
268          c.reset();
269 <        t1.join();
270 <        t2.join();
269 >        awaitTermination(t1);
270 >        awaitTermination(t2);
271      }
272  
273      /**
# Line 234 | Line 276 | public class CyclicBarrierTest extends J
276       */
277      public void testReset_NoBrokenBarrier() throws Exception {
278          final CyclicBarrier c = new CyclicBarrier(3);
279 <        Thread t1 = new Thread(new CheckedRunnable() {
279 >        c.reset();
280 >
281 >        Thread t1 = newStartedThread(new CheckedRunnable() {
282              public void realRun() throws Exception {
283                  c.await();
284              }});
285 <        Thread t2 = new Thread(new CheckedRunnable() {
285 >        Thread t2 = newStartedThread(new CheckedRunnable() {
286              public void realRun() throws Exception {
287                  c.await();
288              }});
289  
246        c.reset();
247        t1.start();
248        t2.start();
290          c.await();
291 <        t1.join();
292 <        t2.join();
291 >        awaitTermination(t1);
292 >        awaitTermination(t2);
293      }
294  
295      /**
# Line 257 | Line 298 | public class CyclicBarrierTest extends J
298      public void testReset_Leakage() throws InterruptedException {
299          final CyclicBarrier c = new CyclicBarrier(2);
300          final AtomicBoolean done = new AtomicBoolean();
301 <        Thread t = new Thread() {
302 <                public void run() {
303 <                    while (!done.get()) {
304 <                        try {
305 <                            while (c.isBroken())
306 <                                c.reset();
307 <
308 <                            c.await();
309 <                            threadFail("await should not return");
269 <                        }
270 <                        catch (BrokenBarrierException e) {
271 <                        }
272 <                        catch (InterruptedException ie) {
273 <                        }
301 >        Thread t = newStartedThread(new CheckedRunnable() {
302 >            public void realRun() {
303 >                while (!done.get()) {
304 >                    try {
305 >                        while (c.isBroken())
306 >                            c.reset();
307 >
308 >                        c.await();
309 >                        shouldThrow();
310                      }
311 <                }
312 <            };
311 >                    catch (BrokenBarrierException | InterruptedException ok) {}
312 >                }}});
313  
278        t.start();
314          for (int i = 0; i < 4; i++) {
315 <            Thread.sleep(SHORT_DELAY_MS);
315 >            delay(timeoutMillis());
316              t.interrupt();
317          }
318          done.set(true);
319          t.interrupt();
320 <        t.join();
320 >        awaitTermination(t);
321      }
322  
323      /**
324       * Reset of a non-broken barrier does not break barrier
325       */
326      public void testResetWithoutBreakage() throws Exception {
292        final CyclicBarrier start = new CyclicBarrier(3);
327          final CyclicBarrier barrier = new CyclicBarrier(3);
328          for (int i = 0; i < 3; i++) {
329 <            Thread t1 = new Thread(new CheckedRunnable() {
329 >            final CyclicBarrier start = new CyclicBarrier(3);
330 >            Thread t1 = newStartedThread(new CheckedRunnable() {
331                  public void realRun() throws Exception {
332                      start.await();
333                      barrier.await();
334                  }});
335  
336 <            Thread t2 = new Thread(new CheckedRunnable() {
336 >            Thread t2 = newStartedThread(new CheckedRunnable() {
337                  public void realRun() throws Exception {
338                      start.await();
339                      barrier.await();
340                  }});
341  
307            t1.start();
308            t2.start();
342              start.await();
343              barrier.await();
344 <            t1.join();
345 <            t2.join();
344 >            awaitTermination(t1);
345 >            awaitTermination(t2);
346              assertFalse(barrier.isBroken());
347              assertEquals(0, barrier.getNumberWaiting());
348              if (i == 1) barrier.reset();
# Line 322 | Line 355 | public class CyclicBarrierTest extends J
355       * Reset of a barrier after interruption reinitializes it.
356       */
357      public void testResetAfterInterrupt() throws Exception {
325        final CyclicBarrier start = new CyclicBarrier(3);
358          final CyclicBarrier barrier = new CyclicBarrier(3);
359          for (int i = 0; i < 2; i++) {
360 +            final CyclicBarrier start = new CyclicBarrier(3);
361              Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
362                  public void realRun() throws Exception {
363                      start.await();
# Line 341 | Line 374 | public class CyclicBarrierTest extends J
374              t2.start();
375              start.await();
376              t1.interrupt();
377 <            t1.join();
378 <            t2.join();
377 >            awaitTermination(t1);
378 >            awaitTermination(t2);
379              assertTrue(barrier.isBroken());
380              assertEquals(0, barrier.getNumberWaiting());
381              barrier.reset();
# Line 355 | Line 388 | public class CyclicBarrierTest extends J
388       * Reset of a barrier after timeout reinitializes it.
389       */
390      public void testResetAfterTimeout() throws Exception {
358        final CyclicBarrier start = new CyclicBarrier(3);
391          final CyclicBarrier barrier = new CyclicBarrier(3);
392          for (int i = 0; i < 2; i++) {
393 <            Thread t1 = new ThreadShouldThrow(TimeoutException.class) {
394 <                    public void realRun() throws Exception {
363 <                        start.await();
364 <                        barrier.await(MEDIUM_DELAY_MS, MILLISECONDS);
365 <                    }};
366 <
367 <            Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
393 >            assertEquals(0, barrier.getNumberWaiting());
394 >            Thread t1 = newStartedThread(new CheckedRunnable() {
395                  public void realRun() throws Exception {
396 <                    start.await();
397 <                    barrier.await();
398 <                }};
396 >                    try {
397 >                        barrier.await();
398 >                        shouldThrow();
399 >                    } catch (BrokenBarrierException success) {}
400 >                }});
401 >            Thread t2 = newStartedThread(new CheckedRunnable() {
402 >                public void realRun() throws Exception {
403 >                    awaitNumberWaiting(barrier, 1);
404 >                    long startTime = System.nanoTime();
405 >                    try {
406 >                        barrier.await(timeoutMillis(), MILLISECONDS);
407 >                        shouldThrow();
408 >                    } catch (TimeoutException success) {}
409 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
410 >                }});
411  
412 <            t1.start();
413 <            t2.start();
414 <            start.await();
376 <            t1.join();
377 <            t2.join();
412 >            awaitTermination(t1);
413 >            awaitTermination(t2);
414 >            assertEquals(0, barrier.getNumberWaiting());
415              assertTrue(barrier.isBroken());
416              assertEquals(0, barrier.getNumberWaiting());
417              barrier.reset();
# Line 383 | Line 420 | public class CyclicBarrierTest extends J
420          }
421      }
422  
386
423      /**
424       * Reset of a barrier after a failed command reinitializes it.
425       */
426      public void testResetAfterCommandException() throws Exception {
391        final CyclicBarrier start = new CyclicBarrier(3);
427          final CyclicBarrier barrier =
428              new CyclicBarrier(3, new Runnable() {
429                      public void run() {
430                          throw new NullPointerException(); }});
431          for (int i = 0; i < 2; i++) {
432 +            final CyclicBarrier start = new CyclicBarrier(3);
433              Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
434                  public void realRun() throws Exception {
435                      start.await();
# Line 409 | Line 445 | public class CyclicBarrierTest extends J
445              t1.start();
446              t2.start();
447              start.await();
448 <            while (barrier.getNumberWaiting() < 2) { Thread.yield(); }
448 >            awaitNumberWaiting(barrier, 2);
449              try {
450                  barrier.await();
451                  shouldThrow();
452              } catch (NullPointerException success) {}
453 <            t1.join();
454 <            t2.join();
453 >            awaitTermination(t1);
454 >            awaitTermination(t2);
455              assertTrue(barrier.isBroken());
456              assertEquals(0, barrier.getNumberWaiting());
457              barrier.reset();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines