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 |
|
*/ |
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 |
|
/** |
25 |
|
*/ |
26 |
|
public void testExchange() throws InterruptedException { |
27 |
|
final Exchanger e = new Exchanger(); |
28 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
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 { |
36 |
< |
Object v = e.exchange(two); |
37 |
< |
threadAssertEquals(v, one); |
38 |
< |
Object w = e.exchange(v); |
39 |
< |
threadAssertEquals(w, two); |
30 |
> |
assertSame(one, e.exchange(two)); |
31 |
> |
assertSame(two, e.exchange(one)); |
32 |
> |
}}); |
33 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
34 |
> |
public void realRun() throws InterruptedException { |
35 |
> |
assertSame(two, e.exchange(one)); |
36 |
> |
assertSame(one, e.exchange(two)); |
37 |
|
}}); |
38 |
|
|
39 |
|
t1.start(); |
47 |
|
*/ |
48 |
|
public void testTimedExchange() throws InterruptedException { |
49 |
|
final Exchanger e = new Exchanger(); |
50 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
50 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
51 |
|
public void realRun() throws Exception { |
52 |
< |
Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
53 |
< |
threadAssertEquals(v, two); |
57 |
< |
Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
58 |
< |
threadAssertEquals(w, one); |
52 |
> |
assertSame(one, e.exchange(two, SHORT_DELAY_MS, MILLISECONDS)); |
53 |
> |
assertSame(two, e.exchange(one, SHORT_DELAY_MS, MILLISECONDS)); |
54 |
|
}}); |
55 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
55 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
56 |
|
public void realRun() throws Exception { |
57 |
< |
Object v = e.exchange(two, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
58 |
< |
threadAssertEquals(v, one); |
64 |
< |
Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
65 |
< |
threadAssertEquals(w, two); |
57 |
> |
assertSame(two, e.exchange(one, SHORT_DELAY_MS, MILLISECONDS)); |
58 |
> |
assertSame(one, e.exchange(two, SHORT_DELAY_MS, MILLISECONDS)); |
59 |
|
}}); |
60 |
|
|
61 |
|
t1.start(); |
69 |
|
*/ |
70 |
|
public void testExchange_InterruptedException() throws InterruptedException { |
71 |
|
final Exchanger e = new Exchanger(); |
72 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
72 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
73 |
|
public void realRun() throws InterruptedException { |
74 |
|
e.exchange(one); |
75 |
|
}}); |
76 |
|
|
77 |
|
t.start(); |
78 |
< |
Thread.sleep(SHORT_DELAY_MS); |
78 |
> |
delay(SHORT_DELAY_MS); |
79 |
|
t.interrupt(); |
80 |
|
t.join(); |
81 |
|
} |
85 |
|
*/ |
86 |
|
public void testTimedExchange_InterruptedException() throws InterruptedException { |
87 |
|
final Exchanger e = new Exchanger(); |
88 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
88 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
89 |
|
public void realRun() throws Exception { |
90 |
< |
e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
90 |
> |
e.exchange(null, SMALL_DELAY_MS, MILLISECONDS); |
91 |
|
}}); |
92 |
|
|
93 |
|
t.start(); |
94 |
+ |
delay(SHORT_DELAY_MS); |
95 |
|
t.interrupt(); |
96 |
|
t.join(); |
97 |
|
} |
101 |
|
*/ |
102 |
|
public void testExchange_TimeOutException() throws InterruptedException { |
103 |
|
final Exchanger e = new Exchanger(); |
104 |
< |
Thread t = new Thread(new CheckedRunnable() { |
104 |
> |
Thread t = new ThreadShouldThrow(TimeoutException.class) { |
105 |
|
public void realRun() throws Exception { |
106 |
< |
try { |
107 |
< |
e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
114 |
< |
threadShouldThrow(); |
115 |
< |
} catch (TimeoutException success) {} |
116 |
< |
}}); |
106 |
> |
e.exchange(null, SHORT_DELAY_MS, MILLISECONDS); |
107 |
> |
}}; |
108 |
|
|
109 |
|
t.start(); |
110 |
|
t.join(); |
115 |
|
*/ |
116 |
|
public void testReplacementAfterExchange() throws InterruptedException { |
117 |
|
final Exchanger e = new Exchanger(); |
118 |
< |
Thread t1 = new Thread(new CheckedInterruptedRunnable() { |
118 |
> |
Thread t1 = new Thread(new CheckedInterruptedRunnable() { |
119 |
|
public void realRun() throws InterruptedException { |
120 |
< |
Object v = e.exchange(one); |
121 |
< |
threadAssertEquals(v, two); |
131 |
< |
Object w = e.exchange(v); |
120 |
> |
assertSame(two, e.exchange(one)); |
121 |
> |
e.exchange(two); |
122 |
|
}}); |
123 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
123 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
124 |
|
public void realRun() throws InterruptedException { |
125 |
< |
Object v = e.exchange(two); |
126 |
< |
threadAssertEquals(v, one); |
127 |
< |
Thread.sleep(SMALL_DELAY_MS); |
138 |
< |
Object w = e.exchange(v); |
139 |
< |
threadAssertEquals(w, three); |
125 |
> |
assertSame(one, e.exchange(two)); |
126 |
> |
delay(SMALL_DELAY_MS); |
127 |
> |
assertSame(three, e.exchange(one)); |
128 |
|
}}); |
129 |
< |
Thread t3 = new Thread(new CheckedRunnable() { |
129 |
> |
Thread t3 = new Thread(new CheckedRunnable() { |
130 |
|
public void realRun() throws InterruptedException { |
131 |
< |
Thread.sleep(SMALL_DELAY_MS); |
132 |
< |
Object w = e.exchange(three); |
145 |
< |
threadAssertEquals(w, one); |
131 |
> |
delay(SMALL_DELAY_MS); |
132 |
> |
assertSame(one, e.exchange(three)); |
133 |
|
}}); |
134 |
|
|
135 |
|
t1.start(); |
136 |
|
t2.start(); |
137 |
|
t3.start(); |
138 |
< |
Thread.sleep(SHORT_DELAY_MS); |
138 |
> |
delay(SHORT_DELAY_MS); |
139 |
|
t1.interrupt(); |
140 |
|
t1.join(); |
141 |
|
t2.join(); |