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.21 by jsr166, Sat May 28 14:52:11 2011 UTC vs.
Revision 1.26 by jsr166, Wed Dec 31 19:05:42 2014 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 +
17 + import junit.framework.Test;
18 + import junit.framework.TestSuite;
19 +
20   public class CyclicBarrierTest extends JSR166TestCase {
21      public static void main(String[] args) {
22          junit.textui.TestRunner.run(suite());
# Line 27 | Line 31 | public class CyclicBarrierTest extends J
31      }
32  
33      /**
34 +     * Spin-waits till the number of waiters == numberOfWaiters.
35 +     */
36 +    void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) {
37 +        long startTime = System.nanoTime();
38 +        while (barrier.getNumberWaiting() != numberOfWaiters) {
39 +            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
40 +                fail("timed out");
41 +            Thread.yield();
42 +        }
43 +    }
44 +
45 +    /**
46       * Creating with negative parties throws IAE
47       */
48      public void testConstructor1() {
# Line 78 | Line 94 | public class CyclicBarrierTest extends J
94          b.await();
95          b.await();
96          assertEquals(0, b.getNumberWaiting());
97 <        assertEquals(countAction, 2);
97 >        assertEquals(2, countAction);
98      }
99  
100      /**
# Line 186 | Line 202 | public class CyclicBarrierTest extends J
202              }});
203          Thread t2 = newStartedThread(new CheckedRunnable() {
204              public void realRun() throws Exception {
205 <                while (c.getNumberWaiting() == 0)
190 <                    Thread.yield();
205 >                awaitNumberWaiting(c, 1);
206                  long startTime = System.nanoTime();
207                  try {
208                      c.await(timeoutMillis(), MILLISECONDS);
# Line 215 | Line 230 | public class CyclicBarrierTest extends J
230              }});
231          Thread t2 = newStartedThread(new CheckedRunnable() {
232              public void realRun() throws Exception {
233 <                while (c.getNumberWaiting() == 0)
219 <                    Thread.yield();
233 >                awaitNumberWaiting(c, 1);
234                  long startTime = System.nanoTime();
235                  try {
236                      c.await(timeoutMillis(), MILLISECONDS);
# Line 250 | Line 264 | public class CyclicBarrierTest extends J
264          t1.start();
265          t2.start();
266          await(pleaseReset);
267 +
268 +        awaitNumberWaiting(c, 2);
269          c.reset();
270          awaitTermination(t1);
271          awaitTermination(t2);
# Line 386 | Line 402 | public class CyclicBarrierTest extends J
402                  }});
403              Thread t2 = newStartedThread(new CheckedRunnable() {
404                  public void realRun() throws Exception {
405 <                    while (barrier.getNumberWaiting() == 0)
390 <                        Thread.yield();
405 >                    awaitNumberWaiting(barrier, 1);
406                      long startTime = System.nanoTime();
407                      try {
408                          barrier.await(timeoutMillis(), MILLISECONDS);
# Line 432 | Line 447 | public class CyclicBarrierTest extends J
447              t1.start();
448              t2.start();
449              start.await();
450 <            while (barrier.getNumberWaiting() < 2) { Thread.yield(); }
450 >            awaitNumberWaiting(barrier, 2);
451              try {
452                  barrier.await();
453                  shouldThrow();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines