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.4 by dl, Thu Sep 25 11:02:41 2003 UTC vs.
Revision 1.21 by jsr166, Tue May 31 16:16:23 2011 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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/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.*;
11 > import java.util.concurrent.CountDownLatch;
12 > import java.util.concurrent.Exchanger;
13 > import java.util.concurrent.TimeoutException;
14 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
15  
16   public class ExchangerTest extends JSR166TestCase {
17 <  
17 >
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run (suite());  
19 >        junit.textui.TestRunner.run(suite());
20      }
21      public static Test suite() {
22 <        return new TestSuite(ExchangerTest.class);
22 >        return new TestSuite(ExchangerTest.class);
23      }
24  
25      /**
# Line 23 | Line 27 | public class ExchangerTest extends JSR16
27       */
28      public void testExchange() {
29          final Exchanger e = new Exchanger();
30 <        Thread t1 = new Thread(new Runnable(){
31 <                public void run(){
32 <                    try {
33 <                        Object v = e.exchange(one);
34 <                        threadAssertEquals(v, two);
35 <                        Object w = e.exchange(v);
36 <                        threadAssertEquals(w, one);
37 <                    } catch(InterruptedException e){
38 <                        threadUnexpectedException();
39 <                    }
40 <                }
41 <            });
42 <        Thread t2 = new Thread(new Runnable(){
39 <                public void run(){
40 <                    try {
41 <                        Object v = e.exchange(two);
42 <                        threadAssertEquals(v, one);
43 <                        Object w = e.exchange(v);
44 <                        threadAssertEquals(w, two);
45 <                    } catch(InterruptedException e){
46 <                        threadUnexpectedException();
47 <                    }
48 <                }
49 <            });
50 <        try {
51 <            t1.start();
52 <            t2.start();
53 <            t1.join();
54 <            t2.join();
55 <        } catch(InterruptedException ex) {
56 <            unexpectedException();
57 <        }
30 >        Thread t1 = newStartedThread(new CheckedRunnable() {
31 >            public void realRun() throws InterruptedException {
32 >                assertSame(one, e.exchange(two));
33 >                assertSame(two, e.exchange(one));
34 >            }});
35 >        Thread t2 = newStartedThread(new CheckedRunnable() {
36 >            public void realRun() throws InterruptedException {
37 >                assertSame(two, e.exchange(one));
38 >                assertSame(one, e.exchange(two));
39 >            }});
40 >
41 >        awaitTermination(t1);
42 >        awaitTermination(t2);
43      }
44  
45      /**
# Line 62 | Line 47 | public class ExchangerTest extends JSR16
47       */
48      public void testTimedExchange() {
49          final Exchanger e = new Exchanger();
50 <        Thread t1 = new Thread(new Runnable(){
51 <                public void run(){
52 <                    try {
53 <                        Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
54 <                        threadAssertEquals(v, two);
55 <                        Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
56 <                        threadAssertEquals(w, one);
57 <                    } catch(InterruptedException e){
58 <                        threadUnexpectedException();
59 <                    } catch(TimeoutException toe) {
60 <                        threadUnexpectedException();
61 <                    }
62 <                }
78 <            });
79 <        Thread t2 = new Thread(new Runnable(){
80 <                public void run(){
81 <                    try {
82 <                        Object v = e.exchange(two, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
83 <                        threadAssertEquals(v, one);
84 <                        Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
85 <                        threadAssertEquals(w, two);
86 <                    } catch(InterruptedException e){
87 <                        threadUnexpectedException();
88 <                    } catch(TimeoutException toe) {
89 <                        threadUnexpectedException();
90 <                    }
91 <                }
92 <            });
93 <        try {
94 <            t1.start();
95 <            t2.start();
96 <            t1.join();
97 <            t2.join();
98 <        } catch(InterruptedException ex) {
99 <            unexpectedException();
100 <        }
50 >        Thread t1 = newStartedThread(new CheckedRunnable() {
51 >            public void realRun() throws Exception {
52 >                assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
53 >                assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
54 >            }});
55 >        Thread t2 = newStartedThread(new CheckedRunnable() {
56 >            public void realRun() throws Exception {
57 >                assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS));
58 >                assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS));
59 >            }});
60 >
61 >        awaitTermination(t1);
62 >        awaitTermination(t2);
63      }
64  
65      /**
66       * interrupt during wait for exchange throws IE
67       */
68 <    public void testExchange_InterruptedException(){
68 >    public void testExchange_InterruptedException() {
69          final Exchanger e = new Exchanger();
70 <        Thread t = new Thread(new Runnable() {
71 <                public void run(){
72 <                    try {
73 <                        e.exchange(one);
74 <                        threadShouldThrow();
75 <                    } catch(InterruptedException success){
76 <                    }
77 <                }
78 <            });
79 <        try {
118 <            t.start();
119 <            Thread.sleep(SHORT_DELAY_MS);
120 <            t.interrupt();
121 <            t.join();
122 <        } catch(InterruptedException ex) {
123 <            unexpectedException();
124 <        }
70 >        final CountDownLatch threadStarted = new CountDownLatch(1);
71 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
72 >            public void realRun() throws InterruptedException {
73 >                threadStarted.countDown();
74 >                e.exchange(one);
75 >            }});
76 >
77 >        await(threadStarted);
78 >        t.interrupt();
79 >        awaitTermination(t);
80      }
81  
82      /**
83       * interrupt during wait for timed exchange throws IE
84       */
85 <    public void testTimedExchange_InterruptedException(){
85 >    public void testTimedExchange_InterruptedException() {
86          final Exchanger e = new Exchanger();
87 <        Thread t = new Thread(new Runnable() {
88 <                public void run(){
89 <                    try {
90 <                        e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
91 <                        threadShouldThrow();
92 <                    } catch(InterruptedException success){
93 <                    } catch(Exception e2){
94 <                        threadFail("should throw IE");
95 <                    }
96 <                }
142 <            });
143 <        try {
144 <            t.start();
145 <            t.interrupt();
146 <            t.join();
147 <        } catch(InterruptedException ex){
148 <            unexpectedException();
149 <        }
87 >        final CountDownLatch threadStarted = new CountDownLatch(1);
88 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
89 >            public void realRun() throws Exception {
90 >                threadStarted.countDown();
91 >                e.exchange(null, LONG_DELAY_MS, MILLISECONDS);
92 >            }});
93 >
94 >        await(threadStarted);
95 >        t.interrupt();
96 >        awaitTermination(t);
97      }
98  
99      /**
100 <     * timeout during wait for timed exchange throws TOE
101 <     */
102 <    public void testExchange_TimeOutException(){
103 <        final Exchanger e = new Exchanger();
104 <        Thread t = new Thread(new Runnable() {
105 <                public void run(){
106 <                    try {
107 <                        e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
108 <                        threadShouldThrow();
109 <                    } catch(TimeoutException success){
110 <                    } catch(InterruptedException e2){
111 <                        threadFail("should throw TOE");
112 <                    }
113 <                }
114 <            });
168 <        try {
169 <            t.start();
170 <            t.join();
171 <        } catch(InterruptedException ex){
172 <            unexpectedException();
173 <        }
100 >     * timeout during wait for timed exchange throws TimeoutException
101 >     */
102 >    public void testExchange_TimeoutException() {
103 >        final Exchanger e = new Exchanger();
104 >        Thread t = newStartedThread(new CheckedRunnable() {
105 >            public void realRun() throws Exception {
106 >                long startTime = System.nanoTime();
107 >                try {
108 >                    e.exchange(null, timeoutMillis(), MILLISECONDS);
109 >                    shouldThrow();
110 >                } catch (TimeoutException success) {}
111 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
112 >            }});
113 >
114 >        awaitTermination(t);
115      }
116  
117      /**
# Line 178 | 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 {
208 <                        Thread.sleep(SMALL_DELAY_MS);
209 <                        Object w = e.exchange(three);
210 <                        threadAssertEquals(w, one);
211 <                    } catch(InterruptedException e){
212 <                        threadUnexpectedException();
213 <                    }
214 <                }
215 <            });
216 <
217 <        try {
218 <            t1.start();
219 <            t2.start();
220 <            t3.start();
221 <            Thread.sleep(SHORT_DELAY_MS);
222 <            t1.interrupt();
223 <            t1.join();
224 <            t2.join();
225 <            t3.join();
226 <        } catch(InterruptedException ex) {
227 <            unexpectedException();
228 <        }
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