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.7 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.19 by jsr166, Sun May 22 20:05:57 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);
20 >        return new TestSuite(ExchangerTest.class);
21      }
22  
23      /**
# Line 24 | Line 25 | public class ExchangerTest extends JSR16
25       */
26      public void testExchange() {
27          final Exchanger e = new Exchanger();
28 <        Thread t1 = new Thread(new Runnable(){
29 <                public void run(){
30 <                    try {
31 <                        Object v = e.exchange(one);
32 <                        threadAssertEquals(v, two);
33 <                        Object w = e.exchange(v);
34 <                        threadAssertEquals(w, one);
35 <                    } catch (InterruptedException e){
36 <                        threadUnexpectedException();
37 <                    }
38 <                }
39 <            });
40 <        Thread t2 = new Thread(new Runnable(){
40 <                public void run(){
41 <                    try {
42 <                        Object v = e.exchange(two);
43 <                        threadAssertEquals(v, one);
44 <                        Object w = e.exchange(v);
45 <                        threadAssertEquals(w, two);
46 <                    } catch (InterruptedException e){
47 <                        threadUnexpectedException();
48 <                    }
49 <                }
50 <            });
51 <        try {
52 <            t1.start();
53 <            t2.start();
54 <            t1.join();
55 <            t2.join();
56 <        } catch (InterruptedException ex) {
57 <            unexpectedException();
58 <        }
28 >        Thread t1 = newStartedThread(new CheckedRunnable() {
29 >            public void realRun() throws InterruptedException {
30 >                assertSame(one, e.exchange(two));
31 >                assertSame(two, e.exchange(one));
32 >            }});
33 >        Thread t2 = newStartedThread(new CheckedRunnable() {
34 >            public void realRun() throws InterruptedException {
35 >                assertSame(two, e.exchange(one));
36 >                assertSame(one, e.exchange(two));
37 >            }});
38 >
39 >        awaitTermination(t1);
40 >        awaitTermination(t2);
41      }
42  
43      /**
# Line 63 | Line 45 | public class ExchangerTest extends JSR16
45       */
46      public void testTimedExchange() {
47          final Exchanger e = new Exchanger();
48 <        Thread t1 = new Thread(new Runnable(){
49 <                public void run(){
50 <                    try {
51 <                        Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
52 <                        threadAssertEquals(v, two);
53 <                        Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
54 <                        threadAssertEquals(w, one);
55 <                    } catch (InterruptedException e){
56 <                        threadUnexpectedException();
57 <                    } catch (TimeoutException toe) {
58 <                        threadUnexpectedException();
59 <                    }
60 <                }
79 <            });
80 <        Thread t2 = new Thread(new Runnable(){
81 <                public void run(){
82 <                    try {
83 <                        Object v = e.exchange(two, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
84 <                        threadAssertEquals(v, one);
85 <                        Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
86 <                        threadAssertEquals(w, two);
87 <                    } catch (InterruptedException e){
88 <                        threadUnexpectedException();
89 <                    } catch (TimeoutException toe) {
90 <                        threadUnexpectedException();
91 <                    }
92 <                }
93 <            });
94 <        try {
95 <            t1.start();
96 <            t2.start();
97 <            t1.join();
98 <            t2.join();
99 <        } catch (InterruptedException ex) {
100 <            unexpectedException();
101 <        }
48 >        Thread t1 = newStartedThread(new CheckedRunnable() {
49 >            public void realRun() throws Exception {
50 >                assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
51 >                assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
52 >            }});
53 >        Thread t2 = newStartedThread(new CheckedRunnable() {
54 >            public void realRun() throws Exception {
55 >                assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
56 >                assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
57 >            }});
58 >
59 >        awaitTermination(t1);
60 >        awaitTermination(t2);
61      }
62  
63      /**
64       * interrupt during wait for exchange throws IE
65       */
66 <    public void testExchange_InterruptedException(){
66 >    public void testExchange_InterruptedException() {
67          final Exchanger e = new Exchanger();
68 <        Thread t = new Thread(new Runnable() {
69 <                public void run(){
70 <                    try {
71 <                        e.exchange(one);
72 <                        threadShouldThrow();
73 <                    } catch (InterruptedException success){
74 <                    }
75 <                }
76 <            });
77 <        try {
119 <            t.start();
120 <            Thread.sleep(SHORT_DELAY_MS);
121 <            t.interrupt();
122 <            t.join();
123 <        } catch (InterruptedException ex) {
124 <            unexpectedException();
125 <        }
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 >        await(threadStarted);
76 >        t.interrupt();
77 >        awaitTermination(t);
78      }
79  
80      /**
81       * interrupt during wait for timed exchange throws IE
82       */
83 <    public void testTimedExchange_InterruptedException(){
83 >    public void testTimedExchange_InterruptedException() {
84          final Exchanger e = new Exchanger();
85 <        Thread t = new Thread(new Runnable() {
86 <                public void run(){
87 <                    try {
88 <                        e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
89 <                        threadShouldThrow();
90 <                    } catch (InterruptedException success){
91 <                    } catch (Exception e2){
92 <                        threadFail("should throw IE");
93 <                    }
94 <                }
143 <            });
144 <        try {
145 <            t.start();
146 <            t.interrupt();
147 <            t.join();
148 <        } catch (InterruptedException ex){
149 <            unexpectedException();
150 <        }
85 >        final CountDownLatch threadStarted = new CountDownLatch(1);
86 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
87 >            public void realRun() throws Exception {
88 >                threadStarted.countDown();
89 >                e.exchange(null, LONG_DELAY_MS, MILLISECONDS);
90 >            }});
91 >
92 >        await(threadStarted);
93 >        t.interrupt();
94 >        awaitTermination(t);
95      }
96  
97      /**
98 <     * timeout during wait for timed exchange throws TOE
99 <     */
100 <    public void testExchange_TimeOutException(){
101 <        final Exchanger e = new Exchanger();
102 <        Thread t = new Thread(new Runnable() {
103 <                public void run(){
104 <                    try {
105 <                        e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
106 <                        threadShouldThrow();
107 <                    } catch (TimeoutException success){
108 <                    } catch (InterruptedException e2){
109 <                        threadFail("should throw TOE");
110 <                    }
98 >     * timeout during wait for timed exchange throws TimeoutException
99 >     */
100 >    public void testExchange_TimeoutException() {
101 >        final Exchanger e = new Exchanger();
102 >        Thread t = newStartedThread(new CheckedRunnable() {
103 >            public void realRun() throws Exception {
104 >                long timeoutMillis = SHORT_DELAY_MS;
105 >                long startTime = System.nanoTime();
106 >                try {
107 >                    e.exchange(null, timeoutMillis, MILLISECONDS);
108 >                    shouldThrow();
109 >                } catch (TimeoutException success) {
110 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
111                  }
112 <            });
113 <        try {
114 <            t.start();
171 <            t.join();
172 <        } catch (InterruptedException ex){
173 <            unexpectedException();
174 <        }
112 >            }});
113 >
114 >        awaitTermination(t);
115      }
116  
117      /**
# Line 179 | Line 119 | public class ExchangerTest extends JSR16
119       */
120      public void testReplacementAfterExchange() {
121          final Exchanger e = new Exchanger();
122 <        Thread t1 = new Thread(new Runnable(){
123 <                public void run(){
124 <                    try {
125 <                        Object v = e.exchange(one);
126 <                        threadAssertEquals(v, two);
127 <                        Object w = e.exchange(v);
128 <                        threadShouldThrow();
129 <                    } catch (InterruptedException success){
130 <                    }
131 <                }
132 <            });
133 <        Thread t2 = new Thread(new Runnable(){
134 <                public void run(){
135 <                    try {
136 <                        Object v = e.exchange(two);
137 <                        threadAssertEquals(v, one);
138 <                        Thread.sleep(SMALL_DELAY_MS);
139 <                        Object w = e.exchange(v);
140 <                        threadAssertEquals(w, three);
141 <                    } catch (InterruptedException e){
142 <                        threadUnexpectedException();
143 <                    }
144 <                }
145 <            });
146 <        Thread t3 = new Thread(new Runnable(){
147 <                public void run(){
148 <                    try {
209 <                        Thread.sleep(SMALL_DELAY_MS);
210 <                        Object w = e.exchange(three);
211 <                        threadAssertEquals(w, one);
212 <                    } catch (InterruptedException e){
213 <                        threadUnexpectedException();
214 <                    }
215 <                }
216 <            });
217 <
218 <        try {
219 <            t1.start();
220 <            t2.start();
221 <            t3.start();
222 <            Thread.sleep(SHORT_DELAY_MS);
223 <            t1.interrupt();
224 <            t1.join();
225 <            t2.join();
226 <            t3.join();
227 <        } catch (InterruptedException ex) {
228 <            unexpectedException();
229 <        }
122 >        final CountDownLatch exchanged = new CountDownLatch(2);
123 >        final CountDownLatch interrupted = new CountDownLatch(1);
124 >        Thread t1 = newStartedThread(new CheckedInterruptedRunnable() {
125 >            public void realRun() throws InterruptedException {
126 >                assertSame(two, e.exchange(one));
127 >                exchanged.countDown();
128 >                e.exchange(two);
129 >            }});
130 >        Thread t2 = newStartedThread(new CheckedRunnable() {
131 >            public void realRun() throws InterruptedException {
132 >                assertSame(one, e.exchange(two));
133 >                exchanged.countDown();
134 >                interrupted.await();
135 >                assertSame(three, e.exchange(one));
136 >            }});
137 >        Thread t3 = newStartedThread(new CheckedRunnable() {
138 >            public void realRun() throws InterruptedException {
139 >                interrupted.await();
140 >                assertSame(one, e.exchange(three));
141 >            }});
142 >
143 >        await(exchanged);
144 >        t1.interrupt();
145 >        awaitTermination(t1);
146 >        interrupted.countDown();
147 >        awaitTermination(t2);
148 >        awaitTermination(t3);
149      }
150  
151   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines