ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExchangerTest.java
Revision: 1.6
Committed: Mon Nov 2 20:28:31 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +4 -4 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.5 * 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
5 jsr166 1.6 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.*;
11     import java.util.concurrent.*;
12    
13 dl 1.2 public class ExchangerTest extends JSR166TestCase {
14 jsr166 1.6
15 dl 1.1 public static void main(String[] args) {
16 jsr166 1.6 junit.textui.TestRunner.run (suite());
17 dl 1.1 }
18     public static Test suite() {
19     return new TestSuite(ExchangerTest.class);
20     }
21    
22 dl 1.3 /**
23 dl 1.4 * exchange exchanges objects across two threads
24 dl 1.3 */
25 dl 1.2 public void testExchange() {
26 dl 1.1 final Exchanger e = new Exchanger();
27     Thread t1 = new Thread(new Runnable(){
28     public void run(){
29 dl 1.2 try {
30 dl 1.1 Object v = e.exchange(one);
31 dl 1.2 threadAssertEquals(v, two);
32 dl 1.1 Object w = e.exchange(v);
33 dl 1.2 threadAssertEquals(w, one);
34     } catch(InterruptedException e){
35 dl 1.3 threadUnexpectedException();
36 dl 1.1 }
37     }
38     });
39     Thread t2 = new Thread(new Runnable(){
40     public void run(){
41 dl 1.2 try {
42 dl 1.1 Object v = e.exchange(two);
43 dl 1.2 threadAssertEquals(v, one);
44 dl 1.1 Object w = e.exchange(v);
45 dl 1.2 threadAssertEquals(w, two);
46     } catch(InterruptedException e){
47 dl 1.3 threadUnexpectedException();
48 dl 1.2 }
49     }
50     });
51     try {
52     t1.start();
53     t2.start();
54     t1.join();
55     t2.join();
56     } catch(InterruptedException ex) {
57 dl 1.3 unexpectedException();
58 dl 1.2 }
59     }
60    
61 dl 1.3 /**
62 dl 1.4 * timed exchange exchanges objects across two threads
63 dl 1.3 */
64 dl 1.2 public void testTimedExchange() {
65     final Exchanger e = new Exchanger();
66     Thread t1 = new Thread(new Runnable(){
67     public void run(){
68     try {
69     Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
70     threadAssertEquals(v, two);
71     Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
72     threadAssertEquals(w, one);
73     } catch(InterruptedException e){
74 dl 1.3 threadUnexpectedException();
75 dl 1.2 } catch(TimeoutException toe) {
76 dl 1.3 threadUnexpectedException();
77 dl 1.2 }
78     }
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 dl 1.3 threadUnexpectedException();
89 dl 1.2 } catch(TimeoutException toe) {
90 dl 1.3 threadUnexpectedException();
91 dl 1.1 }
92     }
93     });
94     try {
95     t1.start();
96     t2.start();
97     t1.join();
98     t2.join();
99     } catch(InterruptedException ex) {
100 dl 1.3 unexpectedException();
101 dl 1.1 }
102     }
103    
104 dl 1.3 /**
105 dl 1.4 * interrupt during wait for exchange throws IE
106 dl 1.3 */
107 dl 1.2 public void testExchange_InterruptedException(){
108 dl 1.1 final Exchanger e = new Exchanger();
109     Thread t = new Thread(new Runnable() {
110     public void run(){
111 dl 1.2 try {
112 dl 1.1 e.exchange(one);
113 dl 1.3 threadShouldThrow();
114 dl 1.2 } catch(InterruptedException success){
115 dl 1.1 }
116     }
117     });
118 dl 1.2 try {
119 dl 1.1 t.start();
120     Thread.sleep(SHORT_DELAY_MS);
121     t.interrupt();
122     t.join();
123     } catch(InterruptedException ex) {
124 dl 1.3 unexpectedException();
125 dl 1.1 }
126     }
127    
128 dl 1.3 /**
129 dl 1.4 * interrupt during wait for timed exchange throws IE
130 dl 1.3 */
131 dl 1.2 public void testTimedExchange_InterruptedException(){
132 dl 1.1 final Exchanger e = new Exchanger();
133     Thread t = new Thread(new Runnable() {
134     public void run(){
135 dl 1.2 try {
136 dl 1.1 e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
137 dl 1.3 threadShouldThrow();
138 dl 1.1 } catch(InterruptedException success){
139     } catch(Exception e2){
140 dl 1.2 threadFail("should throw IE");
141 dl 1.1 }
142     }
143     });
144 dl 1.2 try {
145 dl 1.1 t.start();
146     t.interrupt();
147     t.join();
148     } catch(InterruptedException ex){
149 dl 1.3 unexpectedException();
150 dl 1.1 }
151     }
152    
153 dl 1.3 /**
154 dl 1.4 * timeout during wait for timed exchange throws TOE
155 dl 1.3 */
156 dl 1.2 public void testExchange_TimeOutException(){
157 dl 1.1 final Exchanger e = new Exchanger();
158     Thread t = new Thread(new Runnable() {
159     public void run(){
160 dl 1.2 try {
161 dl 1.1 e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
162 dl 1.3 threadShouldThrow();
163 dl 1.2 } catch(TimeoutException success){
164     } catch(InterruptedException e2){
165     threadFail("should throw TOE");
166 dl 1.1 }
167     }
168     });
169 dl 1.2 try {
170 dl 1.1 t.start();
171     t.join();
172     } catch(InterruptedException ex){
173 dl 1.3 unexpectedException();
174 dl 1.1 }
175     }
176 dl 1.4
177     /**
178     * If one exchanging thread is interrupted, another succeeds.
179     */
180     public void testReplacementAfterExchange() {
181     final Exchanger e = new Exchanger();
182     Thread t1 = new Thread(new Runnable(){
183     public void run(){
184     try {
185     Object v = e.exchange(one);
186     threadAssertEquals(v, two);
187     Object w = e.exchange(v);
188     threadShouldThrow();
189     } catch(InterruptedException success){
190     }
191     }
192     });
193     Thread t2 = new Thread(new Runnable(){
194     public void run(){
195     try {
196     Object v = e.exchange(two);
197     threadAssertEquals(v, one);
198     Thread.sleep(SMALL_DELAY_MS);
199     Object w = e.exchange(v);
200     threadAssertEquals(w, three);
201     } catch(InterruptedException e){
202     threadUnexpectedException();
203     }
204     }
205     });
206     Thread t3 = new Thread(new Runnable(){
207     public void run(){
208     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     }
230     }
231    
232 dl 1.1 }