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/licenses/publicdomain |
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.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
|
14 |
|
public class ExchangerTest extends JSR166TestCase { |
15 |
< |
|
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 |
> |
/** |
24 |
> |
* exchange exchanges objects across two threads |
25 |
> |
*/ |
26 |
> |
public void testExchange() throws InterruptedException { |
27 |
> |
final Exchanger e = new Exchanger(); |
28 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
29 |
> |
public void realRun() throws InterruptedException { |
30 |
> |
Object v = e.exchange(one); |
31 |
> |
threadAssertEquals(v, two); |
32 |
> |
Object w = e.exchange(v); |
33 |
> |
threadAssertEquals(w, one); |
34 |
> |
}}); |
35 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
36 |
> |
public void realRun() throws InterruptedException { |
37 |
> |
Object v = e.exchange(two); |
38 |
> |
threadAssertEquals(v, one); |
39 |
> |
Object w = e.exchange(v); |
40 |
> |
threadAssertEquals(w, two); |
41 |
> |
}}); |
42 |
> |
|
43 |
> |
t1.start(); |
44 |
> |
t2.start(); |
45 |
> |
t1.join(); |
46 |
> |
t2.join(); |
47 |
> |
} |
48 |
> |
|
49 |
> |
/** |
50 |
> |
* timed exchange exchanges objects across two threads |
51 |
> |
*/ |
52 |
> |
public void testTimedExchange() throws InterruptedException { |
53 |
> |
final Exchanger e = new Exchanger(); |
54 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
55 |
> |
public void realRun() throws Exception { |
56 |
> |
Object v = e.exchange(one, SHORT_DELAY_MS, MILLISECONDS); |
57 |
> |
threadAssertEquals(v, two); |
58 |
> |
Object w = e.exchange(v, SHORT_DELAY_MS, MILLISECONDS); |
59 |
> |
threadAssertEquals(w, one); |
60 |
> |
}}); |
61 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
62 |
> |
public void realRun() throws Exception { |
63 |
> |
Object v = e.exchange(two, SHORT_DELAY_MS, MILLISECONDS); |
64 |
> |
threadAssertEquals(v, one); |
65 |
> |
Object w = e.exchange(v, SHORT_DELAY_MS, MILLISECONDS); |
66 |
> |
threadAssertEquals(w, two); |
67 |
> |
}}); |
68 |
> |
|
69 |
> |
t1.start(); |
70 |
> |
t2.start(); |
71 |
> |
t1.join(); |
72 |
> |
t2.join(); |
73 |
> |
} |
74 |
> |
|
75 |
> |
/** |
76 |
> |
* interrupt during wait for exchange throws IE |
77 |
> |
*/ |
78 |
> |
public void testExchange_InterruptedException() throws InterruptedException { |
79 |
> |
final Exchanger e = new Exchanger(); |
80 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
81 |
> |
public void realRun() throws InterruptedException { |
82 |
> |
e.exchange(one); |
83 |
> |
}}); |
84 |
> |
|
85 |
> |
t.start(); |
86 |
> |
Thread.sleep(SHORT_DELAY_MS); |
87 |
> |
t.interrupt(); |
88 |
> |
t.join(); |
89 |
|
} |
90 |
|
|
91 |
< |
public void testExchange() { |
91 |
> |
/** |
92 |
> |
* interrupt during wait for timed exchange throws IE |
93 |
> |
*/ |
94 |
> |
public void testTimedExchange_InterruptedException() throws InterruptedException { |
95 |
|
final Exchanger e = new Exchanger(); |
96 |
< |
Thread t1 = new Thread(new Runnable(){ |
97 |
< |
public void run(){ |
98 |
< |
try { |
99 |
< |
Object v = e.exchange(one); |
100 |
< |
threadAssertEquals(v, two); |
101 |
< |
Object w = e.exchange(v); |
102 |
< |
threadAssertEquals(w, one); |
103 |
< |
} catch(InterruptedException e){ |
104 |
< |
threadFail("unexpected exception"); |
32 |
< |
} |
33 |
< |
} |
34 |
< |
}); |
35 |
< |
Thread t2 = new Thread(new Runnable(){ |
36 |
< |
public void run(){ |
37 |
< |
try { |
38 |
< |
Object v = e.exchange(two); |
39 |
< |
threadAssertEquals(v, one); |
40 |
< |
Object w = e.exchange(v); |
41 |
< |
threadAssertEquals(w, two); |
42 |
< |
} catch(InterruptedException e){ |
43 |
< |
threadFail("unexpected exception"); |
44 |
< |
} |
45 |
< |
} |
46 |
< |
}); |
47 |
< |
try { |
48 |
< |
t1.start(); |
49 |
< |
t2.start(); |
50 |
< |
t1.join(); |
51 |
< |
t2.join(); |
52 |
< |
} catch(InterruptedException ex) { |
53 |
< |
fail("unexpected exception"); |
54 |
< |
} |
55 |
< |
} |
56 |
< |
|
57 |
< |
public void testTimedExchange() { |
58 |
< |
final Exchanger e = new Exchanger(); |
59 |
< |
Thread t1 = new Thread(new Runnable(){ |
60 |
< |
public void run(){ |
61 |
< |
try { |
62 |
< |
Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
63 |
< |
threadAssertEquals(v, two); |
64 |
< |
Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
65 |
< |
threadAssertEquals(w, one); |
66 |
< |
} catch(InterruptedException e){ |
67 |
< |
threadFail("unexpected exception"); |
68 |
< |
} catch(TimeoutException toe) { |
69 |
< |
threadFail("unexpected exception"); |
70 |
< |
} |
71 |
< |
} |
72 |
< |
}); |
73 |
< |
Thread t2 = new Thread(new Runnable(){ |
74 |
< |
public void run(){ |
75 |
< |
try { |
76 |
< |
Object v = e.exchange(two, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
77 |
< |
threadAssertEquals(v, one); |
78 |
< |
Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
79 |
< |
threadAssertEquals(w, two); |
80 |
< |
} catch(InterruptedException e){ |
81 |
< |
threadFail("unexpected exception"); |
82 |
< |
} catch(TimeoutException toe) { |
83 |
< |
threadFail("unexpected exception"); |
84 |
< |
} |
85 |
< |
} |
86 |
< |
}); |
87 |
< |
try { |
88 |
< |
t1.start(); |
89 |
< |
t2.start(); |
90 |
< |
t1.join(); |
91 |
< |
t2.join(); |
92 |
< |
} catch(InterruptedException ex) { |
93 |
< |
fail("unexpected exception"); |
94 |
< |
} |
95 |
< |
} |
96 |
< |
|
97 |
< |
public void testExchange_InterruptedException(){ |
98 |
< |
final Exchanger e = new Exchanger(); |
99 |
< |
Thread t = new Thread(new Runnable() { |
100 |
< |
public void run(){ |
101 |
< |
try { |
102 |
< |
e.exchange(one); |
103 |
< |
threadFail("should throw"); |
104 |
< |
} catch(InterruptedException success){ |
105 |
< |
} |
106 |
< |
} |
107 |
< |
}); |
108 |
< |
try { |
109 |
< |
t.start(); |
110 |
< |
Thread.sleep(SHORT_DELAY_MS); |
111 |
< |
t.interrupt(); |
112 |
< |
t.join(); |
113 |
< |
} catch(InterruptedException ex) { |
114 |
< |
fail("unexpected exception"); |
115 |
< |
} |
116 |
< |
} |
117 |
< |
|
118 |
< |
public void testTimedExchange_InterruptedException(){ |
119 |
< |
final Exchanger e = new Exchanger(); |
120 |
< |
Thread t = new Thread(new Runnable() { |
121 |
< |
public void run(){ |
122 |
< |
try { |
123 |
< |
e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
124 |
< |
threadFail("should throw"); |
125 |
< |
} catch(InterruptedException success){ |
126 |
< |
} catch(Exception e2){ |
127 |
< |
threadFail("should throw IE"); |
128 |
< |
} |
129 |
< |
} |
130 |
< |
}); |
131 |
< |
try { |
132 |
< |
t.start(); |
133 |
< |
t.interrupt(); |
134 |
< |
t.join(); |
135 |
< |
} catch(InterruptedException ex){ |
136 |
< |
fail("unexpected exception"); |
137 |
< |
} |
138 |
< |
} |
139 |
< |
|
140 |
< |
public void testExchange_TimeOutException(){ |
141 |
< |
final Exchanger e = new Exchanger(); |
142 |
< |
Thread t = new Thread(new Runnable() { |
143 |
< |
public void run(){ |
144 |
< |
try { |
145 |
< |
e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
146 |
< |
threadFail("should throw"); |
147 |
< |
} catch(TimeoutException success){ |
148 |
< |
} catch(InterruptedException e2){ |
149 |
< |
threadFail("should throw TOE"); |
150 |
< |
} |
151 |
< |
} |
152 |
< |
}); |
153 |
< |
try { |
154 |
< |
t.start(); |
155 |
< |
t.join(); |
156 |
< |
} catch(InterruptedException ex){ |
157 |
< |
fail("unexpected exception"); |
158 |
< |
} |
96 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
97 |
> |
public void realRun() throws Exception { |
98 |
> |
e.exchange(null, MEDIUM_DELAY_MS, MILLISECONDS); |
99 |
> |
}}); |
100 |
> |
|
101 |
> |
t.start(); |
102 |
> |
Thread.sleep(SHORT_DELAY_MS); |
103 |
> |
t.interrupt(); |
104 |
> |
t.join(); |
105 |
|
} |
106 |
+ |
|
107 |
+ |
/** |
108 |
+ |
* timeout during wait for timed exchange throws TOE |
109 |
+ |
*/ |
110 |
+ |
public void testExchange_TimeOutException() throws InterruptedException { |
111 |
+ |
final Exchanger e = new Exchanger(); |
112 |
+ |
Thread t = new Thread(new CheckedRunnable() { |
113 |
+ |
public void realRun() throws Exception { |
114 |
+ |
try { |
115 |
+ |
e.exchange(null, SHORT_DELAY_MS, MILLISECONDS); |
116 |
+ |
threadShouldThrow(); |
117 |
+ |
} catch (TimeoutException success) {} |
118 |
+ |
}}); |
119 |
+ |
|
120 |
+ |
t.start(); |
121 |
+ |
t.join(); |
122 |
+ |
} |
123 |
+ |
|
124 |
+ |
/** |
125 |
+ |
* If one exchanging thread is interrupted, another succeeds. |
126 |
+ |
*/ |
127 |
+ |
public void testReplacementAfterExchange() throws InterruptedException { |
128 |
+ |
final Exchanger e = new Exchanger(); |
129 |
+ |
Thread t1 = new Thread(new CheckedInterruptedRunnable() { |
130 |
+ |
public void realRun() throws InterruptedException { |
131 |
+ |
Object v = e.exchange(one); |
132 |
+ |
threadAssertEquals(v, two); |
133 |
+ |
Object w = e.exchange(v); |
134 |
+ |
}}); |
135 |
+ |
Thread t2 = new Thread(new CheckedRunnable() { |
136 |
+ |
public void realRun() throws InterruptedException { |
137 |
+ |
Object v = e.exchange(two); |
138 |
+ |
threadAssertEquals(v, one); |
139 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
140 |
+ |
Object w = e.exchange(v); |
141 |
+ |
threadAssertEquals(w, three); |
142 |
+ |
}}); |
143 |
+ |
Thread t3 = new Thread(new CheckedRunnable() { |
144 |
+ |
public void realRun() throws InterruptedException { |
145 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
146 |
+ |
Object w = e.exchange(three); |
147 |
+ |
threadAssertEquals(w, one); |
148 |
+ |
}}); |
149 |
+ |
|
150 |
+ |
t1.start(); |
151 |
+ |
t2.start(); |
152 |
+ |
t3.start(); |
153 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
154 |
+ |
t1.interrupt(); |
155 |
+ |
t1.join(); |
156 |
+ |
t2.join(); |
157 |
+ |
t3.join(); |
158 |
+ |
} |
159 |
+ |
|
160 |
|
} |