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

Comparing jsr166/src/test/tck/ExchangerTest.java (file contents):
Revision 1.10 by jsr166, Sat Nov 21 02:07:26 2009 UTC vs.
Revision 1.20 by jsr166, Sun May 29 06:48:42 2011 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   */
# Line 9 | Line 9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13  
14   public class ExchangerTest extends JSR166TestCase {
15  
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());
17 >        junit.textui.TestRunner.run(suite());
18      }
19      public static Test suite() {
20          return new TestSuite(ExchangerTest.class);
# Line 22 | Line 23 | public class ExchangerTest extends JSR16
23      /**
24       * exchange exchanges objects across two threads
25       */
26 <    public void testExchange() throws InterruptedException {
26 >    public void testExchange() {
27          final Exchanger e = new Exchanger();
28 <        Thread t1 = new Thread(new CheckedRunnable() {
28 >        Thread t1 = newStartedThread(new CheckedRunnable() {
29              public void realRun() throws InterruptedException {
30 <                Object v = e.exchange(one);
31 <                threadAssertEquals(v, two);
31 <                Object w = e.exchange(v);
32 <                threadAssertEquals(w, one);
30 >                assertSame(one, e.exchange(two));
31 >                assertSame(two, e.exchange(one));
32              }});
33 <        Thread t2 = new Thread(new CheckedRunnable() {
33 >        Thread t2 = newStartedThread(new CheckedRunnable() {
34              public void realRun() throws InterruptedException {
35 <                Object v = e.exchange(two);
36 <                threadAssertEquals(v, one);
38 <                Object w = e.exchange(v);
39 <                threadAssertEquals(w, two);
35 >                assertSame(two, e.exchange(one));
36 >                assertSame(one, e.exchange(two));
37              }});
38  
39 <        t1.start();
40 <        t2.start();
44 <        t1.join();
45 <        t2.join();
39 >        awaitTermination(t1);
40 >        awaitTermination(t2);
41      }
42  
43      /**
44       * timed exchange exchanges objects across two threads
45       */
46 <    public void testTimedExchange() throws InterruptedException {
46 >    public void testTimedExchange() {
47          final Exchanger e = new Exchanger();
48 <        Thread t1 = new Thread(new CheckedRunnable() {
48 >        Thread t1 = newStartedThread(new CheckedRunnable() {
49              public void realRun() throws Exception {
50 <                Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
51 <                threadAssertEquals(v, two);
57 <                Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
58 <                threadAssertEquals(w, one);
50 >                assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
51 >                assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
52              }});
53 <        Thread t2 = new Thread(new CheckedRunnable() {
53 >        Thread t2 = newStartedThread(new CheckedRunnable() {
54              public void realRun() throws Exception {
55 <                Object v = e.exchange(two, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
56 <                threadAssertEquals(v, one);
64 <                Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
65 <                threadAssertEquals(w, two);
55 >                assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
56 >                assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
57              }});
58  
59 <        t1.start();
60 <        t2.start();
70 <        t1.join();
71 <        t2.join();
59 >        awaitTermination(t1);
60 >        awaitTermination(t2);
61      }
62  
63      /**
64       * interrupt during wait for exchange throws IE
65       */
66 <    public void testExchange_InterruptedException() throws InterruptedException {
66 >    public void testExchange_InterruptedException() {
67          final Exchanger e = new Exchanger();
68 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
68 >        final CountDownLatch threadStarted = new CountDownLatch(1);
69 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
70              public void realRun() throws InterruptedException {
71 +                threadStarted.countDown();
72                  e.exchange(one);
73              }});
74  
75 <        t.start();
85 <        Thread.sleep(SHORT_DELAY_MS);
75 >        await(threadStarted);
76          t.interrupt();
77 <        t.join();
77 >        awaitTermination(t);
78      }
79  
80      /**
81       * interrupt during wait for timed exchange throws IE
82       */
83 <    public void testTimedExchange_InterruptedException() throws InterruptedException {
83 >    public void testTimedExchange_InterruptedException() {
84          final Exchanger e = new Exchanger();
85 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
85 >        final CountDownLatch threadStarted = new CountDownLatch(1);
86 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
87              public void realRun() throws Exception {
88 <                e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
88 >                threadStarted.countDown();
89 >                e.exchange(null, LONG_DELAY_MS, MILLISECONDS);
90              }});
91  
92 <        t.start();
92 >        await(threadStarted);
93          t.interrupt();
94 <        t.join();
94 >        awaitTermination(t);
95      }
96  
97      /**
98 <     * timeout during wait for timed exchange throws TOE
98 >     * timeout during wait for timed exchange throws TimeoutException
99       */
100 <    public void testExchange_TimeOutException() throws InterruptedException {
100 >    public void testExchange_TimeoutException() {
101          final Exchanger e = new Exchanger();
102 <        Thread t = new Thread(new CheckedRunnable() {
102 >        Thread t = newStartedThread(new CheckedRunnable() {
103              public void realRun() throws Exception {
104 +                long startTime = System.nanoTime();
105                  try {
106 <                    e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
107 <                    threadShouldThrow();
106 >                    e.exchange(null, timeoutMillis(), MILLISECONDS);
107 >                    shouldThrow();
108                  } catch (TimeoutException success) {}
109 +                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
110              }});
111  
112 <        t.start();
119 <        t.join();
112 >        awaitTermination(t);
113      }
114  
115      /**
116       * If one exchanging thread is interrupted, another succeeds.
117       */
118 <    public void testReplacementAfterExchange() throws InterruptedException {
118 >    public void testReplacementAfterExchange() {
119          final Exchanger e = new Exchanger();
120 <        Thread t1 = new Thread(new CheckedInterruptedRunnable() {
120 >        final CountDownLatch exchanged = new CountDownLatch(2);
121 >        final CountDownLatch interrupted = new CountDownLatch(1);
122 >        Thread t1 = newStartedThread(new CheckedInterruptedRunnable() {
123              public void realRun() throws InterruptedException {
124 <                Object v = e.exchange(one);
125 <                threadAssertEquals(v, two);
126 <                Object w = e.exchange(v);
124 >                assertSame(two, e.exchange(one));
125 >                exchanged.countDown();
126 >                e.exchange(two);
127              }});
128 <        Thread t2 = new Thread(new CheckedRunnable() {
128 >        Thread t2 = newStartedThread(new CheckedRunnable() {
129              public void realRun() throws InterruptedException {
130 <                Object v = e.exchange(two);
131 <                threadAssertEquals(v, one);
132 <                Thread.sleep(SMALL_DELAY_MS);
133 <                Object w = e.exchange(v);
139 <                threadAssertEquals(w, three);
130 >                assertSame(one, e.exchange(two));
131 >                exchanged.countDown();
132 >                interrupted.await();
133 >                assertSame(three, e.exchange(one));
134              }});
135 <        Thread t3 = new Thread(new CheckedRunnable() {
135 >        Thread t3 = newStartedThread(new CheckedRunnable() {
136              public void realRun() throws InterruptedException {
137 <                Thread.sleep(SMALL_DELAY_MS);
138 <                Object w = e.exchange(three);
145 <                threadAssertEquals(w, one);
137 >                interrupted.await();
138 >                assertSame(one, e.exchange(three));
139              }});
140  
141 <        t1.start();
149 <        t2.start();
150 <        t3.start();
151 <        Thread.sleep(SHORT_DELAY_MS);
141 >        await(exchanged);
142          t1.interrupt();
143 <        t1.join();
144 <        t2.join();
145 <        t3.join();
143 >        awaitTermination(t1);
144 >        interrupted.countDown();
145 >        awaitTermination(t2);
146 >        awaitTermination(t3);
147      }
148  
149   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines