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.20 by jsr166, Fri May 27 20:07:24 2011 UTC vs.
Revision 1.28 by jsr166, Thu Sep 15 01:18:01 2016 UTC

# Line 6 | Line 6
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      /**
# Line 71 | Line 83 | public class CyclicBarrierTest extends J
83       * The supplied barrier action is run at barrier
84       */
85      public void testBarrierAction() throws Exception {
86 <        countAction = 0;
87 <        CyclicBarrier b = new CyclicBarrier(1, new MyAction());
86 >        final AtomicInteger count = new AtomicInteger(0);
87 >        final Runnable incCount = new Runnable() { public void run() {
88 >            count.getAndIncrement(); }};
89 >        CyclicBarrier b = new CyclicBarrier(1, incCount);
90          assertEquals(1, b.getParties());
91          assertEquals(0, b.getNumberWaiting());
92          b.await();
93          b.await();
94          assertEquals(0, b.getNumberWaiting());
95 <        assertEquals(countAction, 2);
95 >        assertEquals(2, count.get());
96      }
97  
98      /**
# Line 86 | Line 100 | public class CyclicBarrierTest extends J
100       */
101      public void testTwoParties() throws Exception {
102          final CyclicBarrier b = new CyclicBarrier(2);
103 <        Thread t = new Thread(new CheckedRunnable() {
103 >        Thread t = newStartedThread(new CheckedRunnable() {
104              public void realRun() throws Exception {
105                  b.await();
106                  b.await();
# Line 94 | Line 108 | public class CyclicBarrierTest extends J
108                  b.await();
109              }});
110  
97        t.start();
111          b.await();
112          b.await();
113          b.await();
114          b.await();
115 <        t.join();
115 >        awaitTermination(t);
116      }
117  
118      /**
119       * An interruption in one party causes others waiting in await to
120       * throw BrokenBarrierException
121       */
122 <    public void testAwait1_Interrupted_BrokenBarrier() throws Exception {
122 >    public void testAwait1_Interrupted_BrokenBarrier() {
123          final CyclicBarrier c = new CyclicBarrier(3);
124 +        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
125          Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
126              public void realRun() throws Exception {
127 +                pleaseInterrupt.countDown();
128                  c.await();
129              }};
130          Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
131              public void realRun() throws Exception {
132 +                pleaseInterrupt.countDown();
133                  c.await();
134              }};
135  
136          t1.start();
137          t2.start();
138 <        delay(SHORT_DELAY_MS);
138 >        await(pleaseInterrupt);
139          t1.interrupt();
140 <        t1.join();
141 <        t2.join();
140 >        awaitTermination(t1);
141 >        awaitTermination(t2);
142      }
143  
144      /**
# Line 131 | Line 147 | public class CyclicBarrierTest extends J
147       */
148      public void testAwait2_Interrupted_BrokenBarrier() throws Exception {
149          final CyclicBarrier c = new CyclicBarrier(3);
150 +        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
151          Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
152              public void realRun() throws Exception {
153 +                pleaseInterrupt.countDown();
154                  c.await(LONG_DELAY_MS, MILLISECONDS);
155              }};
156          Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
157              public void realRun() throws Exception {
158 +                pleaseInterrupt.countDown();
159                  c.await(LONG_DELAY_MS, MILLISECONDS);
160              }};
161  
162          t1.start();
163          t2.start();
164 <        delay(SHORT_DELAY_MS);
164 >        await(pleaseInterrupt);
165          t1.interrupt();
166 <        t1.join();
167 <        t2.join();
166 >        awaitTermination(t1);
167 >        awaitTermination(t2);
168      }
169  
170      /**
# Line 153 | Line 172 | public class CyclicBarrierTest extends J
172       */
173      public void testAwait3_TimeoutException() throws InterruptedException {
174          final CyclicBarrier c = new CyclicBarrier(2);
175 <        Thread t = new ThreadShouldThrow(TimeoutException.class) {
175 >        Thread t = newStartedThread(new CheckedRunnable() {
176              public void realRun() throws Exception {
177 <                c.await(SHORT_DELAY_MS, MILLISECONDS);
178 <            }};
177 >                long startTime = System.nanoTime();
178 >                try {
179 >                    c.await(timeoutMillis(), MILLISECONDS);
180 >                    shouldThrow();
181 >                } catch (TimeoutException success) {}
182 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
183 >            }});
184  
185 <        t.start();
162 <        t.join();
185 >        awaitTermination(t);
186      }
187  
188      /**
# Line 168 | Line 191 | public class CyclicBarrierTest extends J
191       */
192      public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException {
193          final CyclicBarrier c = new CyclicBarrier(3);
194 <        Thread t1 = new ThreadShouldThrow(TimeoutException.class) {
194 >        Thread t1 = newStartedThread(new CheckedRunnable() {
195              public void realRun() throws Exception {
196 <                c.await(SHORT_DELAY_MS, MILLISECONDS);
197 <            }};
198 <        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
196 >                try {
197 >                    c.await(LONG_DELAY_MS, MILLISECONDS);
198 >                    shouldThrow();
199 >                } catch (BrokenBarrierException success) {}
200 >            }});
201 >        Thread t2 = newStartedThread(new CheckedRunnable() {
202              public void realRun() throws Exception {
203 <                c.await(MEDIUM_DELAY_MS, MILLISECONDS);
204 <            }};
203 >                awaitNumberWaiting(c, 1);
204 >                long startTime = System.nanoTime();
205 >                try {
206 >                    c.await(timeoutMillis(), MILLISECONDS);
207 >                    shouldThrow();
208 >                } catch (TimeoutException success) {}
209 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
210 >            }});
211  
212 <        t1.start();
213 <        t2.start();
182 <        t1.join();
183 <        t2.join();
212 >        awaitTermination(t1);
213 >        awaitTermination(t2);
214      }
215  
216      /**
# Line 189 | Line 219 | public class CyclicBarrierTest extends J
219       */
220      public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException {
221          final CyclicBarrier c = new CyclicBarrier(3);
222 <        Thread t1 = new ThreadShouldThrow(TimeoutException.class) {
222 >        Thread t1 = newStartedThread(new CheckedRunnable() {
223              public void realRun() throws Exception {
224 <                c.await(SHORT_DELAY_MS, MILLISECONDS);
225 <            }};
226 <        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
224 >                try {
225 >                    c.await();
226 >                    shouldThrow();
227 >                } catch (BrokenBarrierException success) {}
228 >            }});
229 >        Thread t2 = newStartedThread(new CheckedRunnable() {
230              public void realRun() throws Exception {
231 <                c.await();
232 <            }};
231 >                awaitNumberWaiting(c, 1);
232 >                long startTime = System.nanoTime();
233 >                try {
234 >                    c.await(timeoutMillis(), MILLISECONDS);
235 >                    shouldThrow();
236 >                } catch (TimeoutException success) {}
237 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
238 >            }});
239  
240 <        t1.start();
241 <        t2.start();
203 <        t1.join();
204 <        t2.join();
240 >        awaitTermination(t1);
241 >        awaitTermination(t2);
242      }
243  
244      /**
# Line 210 | Line 247 | public class CyclicBarrierTest extends J
247       */
248      public void testReset_BrokenBarrier() throws InterruptedException {
249          final CyclicBarrier c = new CyclicBarrier(3);
250 +        final CountDownLatch pleaseReset = new CountDownLatch(2);
251          Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
252              public void realRun() throws Exception {
253 +                pleaseReset.countDown();
254                  c.await();
255              }};
256          Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
257              public void realRun() throws Exception {
258 +                pleaseReset.countDown();
259                  c.await();
260              }};
261  
262          t1.start();
263          t2.start();
264 <        delay(SHORT_DELAY_MS);
264 >        await(pleaseReset);
265 >
266 >        awaitNumberWaiting(c, 2);
267          c.reset();
268 <        t1.join();
269 <        t2.join();
268 >        awaitTermination(t1);
269 >        awaitTermination(t2);
270      }
271  
272      /**
# Line 233 | Line 275 | public class CyclicBarrierTest extends J
275       */
276      public void testReset_NoBrokenBarrier() throws Exception {
277          final CyclicBarrier c = new CyclicBarrier(3);
278 <        Thread t1 = new Thread(new CheckedRunnable() {
278 >        c.reset();
279 >
280 >        Thread t1 = newStartedThread(new CheckedRunnable() {
281              public void realRun() throws Exception {
282                  c.await();
283              }});
284 <        Thread t2 = new Thread(new CheckedRunnable() {
284 >        Thread t2 = newStartedThread(new CheckedRunnable() {
285              public void realRun() throws Exception {
286                  c.await();
287              }});
288  
245        c.reset();
246        t1.start();
247        t2.start();
289          c.await();
290 <        t1.join();
291 <        t2.join();
290 >        awaitTermination(t1);
291 >        awaitTermination(t2);
292      }
293  
294      /**
# Line 256 | Line 297 | public class CyclicBarrierTest extends J
297      public void testReset_Leakage() throws InterruptedException {
298          final CyclicBarrier c = new CyclicBarrier(2);
299          final AtomicBoolean done = new AtomicBoolean();
300 <        Thread t = new Thread(new CheckedRunnable() {
300 >        Thread t = newStartedThread(new CheckedRunnable() {
301              public void realRun() {
302                  while (!done.get()) {
303                      try {
# Line 270 | Line 311 | public class CyclicBarrierTest extends J
311                      catch (InterruptedException ok) {}
312                  }}});
313  
273        t.start();
314          for (int i = 0; i < 4; i++) {
315              delay(timeoutMillis());
316              t.interrupt();
# Line 284 | Line 324 | public class CyclicBarrierTest extends J
324       * Reset of a non-broken barrier does not break barrier
325       */
326      public void testResetWithoutBreakage() throws Exception {
287        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  
302            t1.start();
303            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 317 | Line 355 | public class CyclicBarrierTest extends J
355       * Reset of a barrier after interruption reinitializes it.
356       */
357      public void testResetAfterInterrupt() throws Exception {
320        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 336 | 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 350 | Line 388 | public class CyclicBarrierTest extends J
388       * Reset of a barrier after timeout reinitializes it.
389       */
390      public void testResetAfterTimeout() throws Exception {
353        final CyclicBarrier start = new CyclicBarrier(2);
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 {
358 <                        start.await();
359 <                        barrier.await(SHORT_DELAY_MS, MILLISECONDS);
360 <                    }};
361 <
362 <            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 <            t1.join();
371 <            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 381 | Line 424 | public class CyclicBarrierTest extends J
424       * Reset of a barrier after a failed command reinitializes it.
425       */
426      public void testResetAfterCommandException() throws Exception {
384        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 402 | 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