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 |
|
|
12 |
– |
public class ExchangerTest extends TestCase{ |
13 |
– |
|
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 |
> |
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(); |
40 |
> |
t2.start(); |
41 |
> |
t1.join(); |
42 |
> |
t2.join(); |
43 |
> |
} |
44 |
> |
|
45 |
> |
/** |
46 |
> |
* timed exchange exchanges objects across two threads |
47 |
> |
*/ |
48 |
> |
public void testTimedExchange() throws InterruptedException { |
49 |
> |
final Exchanger e = new Exchanger(); |
50 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
51 |
> |
public void realRun() throws Exception { |
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() { |
56 |
> |
public void realRun() throws Exception { |
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(); |
62 |
> |
t2.start(); |
63 |
> |
t1.join(); |
64 |
> |
t2.join(); |
65 |
> |
} |
66 |
> |
|
67 |
> |
/** |
68 |
> |
* interrupt during wait for exchange throws IE |
69 |
> |
*/ |
70 |
> |
public void testExchange_InterruptedException() throws InterruptedException { |
71 |
> |
final Exchanger e = new Exchanger(); |
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); |
79 |
> |
t.interrupt(); |
80 |
> |
t.join(); |
81 |
> |
} |
82 |
> |
|
83 |
> |
/** |
84 |
> |
* interrupt during wait for timed exchange throws IE |
85 |
> |
*/ |
86 |
> |
public void testTimedExchange_InterruptedException() throws InterruptedException { |
87 |
> |
final Exchanger e = new Exchanger(); |
88 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
89 |
> |
public void realRun() throws Exception { |
90 |
> |
e.exchange(null, SMALL_DELAY_MS, MILLISECONDS); |
91 |
> |
}}); |
92 |
> |
|
93 |
> |
t.start(); |
94 |
> |
Thread.sleep(SHORT_DELAY_MS); |
95 |
> |
t.interrupt(); |
96 |
> |
t.join(); |
97 |
|
} |
98 |
|
|
99 |
< |
private static long SHORT_DELAY_MS = 100; |
100 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
101 |
< |
private static long LONG_DELAY_MS = 10000; |
102 |
< |
|
103 |
< |
private final static Integer one = new Integer(1); |
104 |
< |
private final static Integer two = new Integer(2); |
105 |
< |
|
106 |
< |
public void testExchange(){ |
107 |
< |
final Exchanger e = new Exchanger(); |
108 |
< |
Thread t1 = new Thread(new Runnable(){ |
109 |
< |
public void run(){ |
110 |
< |
try{ |
33 |
< |
Object v = e.exchange(one); |
34 |
< |
assertEquals(v, two); |
35 |
< |
Object w = e.exchange(v); |
36 |
< |
assertEquals(w, one); |
37 |
< |
}catch(InterruptedException e){ |
38 |
< |
fail("unexpected exception"); |
39 |
< |
} |
40 |
< |
} |
41 |
< |
}); |
42 |
< |
Thread t2 = new Thread(new Runnable(){ |
43 |
< |
public void run(){ |
44 |
< |
try{ |
45 |
< |
Object v = e.exchange(two); |
46 |
< |
assertEquals(v, one); |
47 |
< |
Object w = e.exchange(v); |
48 |
< |
assertEquals(w, two); |
49 |
< |
}catch(InterruptedException e){ |
50 |
< |
fail("unexpected exception"); |
51 |
< |
} |
52 |
< |
} |
53 |
< |
}); |
54 |
< |
try { |
55 |
< |
t1.start(); |
56 |
< |
t2.start(); |
57 |
< |
t1.join(); |
58 |
< |
t2.join(); |
59 |
< |
} catch(InterruptedException ex) { |
60 |
< |
fail("unexpected exception"); |
61 |
< |
} |
62 |
< |
} |
63 |
< |
|
64 |
< |
public void testExchange1_InterruptedException(){ |
65 |
< |
final Exchanger e = new Exchanger(); |
66 |
< |
Thread t = new Thread(new Runnable() { |
67 |
< |
public void run(){ |
68 |
< |
try{ |
69 |
< |
e.exchange(one); |
70 |
< |
fail("should throw"); |
71 |
< |
}catch(InterruptedException success){ |
72 |
< |
} |
73 |
< |
} |
74 |
< |
}); |
75 |
< |
try{ |
76 |
< |
t.start(); |
77 |
< |
Thread.sleep(SHORT_DELAY_MS); |
78 |
< |
t.interrupt(); |
79 |
< |
t.join(); |
80 |
< |
} catch(InterruptedException ex) { |
81 |
< |
fail("unexpected exception"); |
82 |
< |
} |
83 |
< |
} |
84 |
< |
|
85 |
< |
public void testExchange2_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 |
< |
fail("should throw"); |
92 |
< |
} catch(InterruptedException success){ |
93 |
< |
} catch(Exception e2){ |
94 |
< |
fail("should throw IE"); |
95 |
< |
} |
96 |
< |
} |
97 |
< |
}); |
98 |
< |
try{ |
99 |
< |
t.start(); |
100 |
< |
t.interrupt(); |
101 |
< |
t.join(); |
102 |
< |
} catch(InterruptedException ex){ |
103 |
< |
fail("unexpected exception"); |
104 |
< |
} |
105 |
< |
} |
106 |
< |
|
107 |
< |
public void testExchange3_TimeOutException(){ |
108 |
< |
final Exchanger e = new Exchanger(); |
109 |
< |
Thread t = new Thread(new Runnable() { |
110 |
< |
public void run(){ |
111 |
< |
try{ |
112 |
< |
e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
113 |
< |
fail("should throw"); |
114 |
< |
}catch(TimeoutException success){} |
115 |
< |
catch(InterruptedException e2){ |
116 |
< |
fail("should throw TOE"); |
117 |
< |
} |
118 |
< |
} |
119 |
< |
}); |
120 |
< |
try{ |
121 |
< |
t.start(); |
122 |
< |
t.join(); |
123 |
< |
} catch(InterruptedException ex){ |
124 |
< |
fail("unexpected exception"); |
125 |
< |
} |
99 |
> |
/** |
100 |
> |
* timeout during wait for timed exchange throws TOE |
101 |
> |
*/ |
102 |
> |
public void testExchange_TimeOutException() throws InterruptedException { |
103 |
> |
final Exchanger e = new Exchanger(); |
104 |
> |
Thread t = new ThreadShouldThrow(TimeoutException.class) { |
105 |
> |
public void realRun() throws Exception { |
106 |
> |
e.exchange(null, SHORT_DELAY_MS, MILLISECONDS); |
107 |
> |
}}; |
108 |
> |
|
109 |
> |
t.start(); |
110 |
> |
t.join(); |
111 |
|
} |
112 |
+ |
|
113 |
+ |
/** |
114 |
+ |
* If one exchanging thread is interrupted, another succeeds. |
115 |
+ |
*/ |
116 |
+ |
public void testReplacementAfterExchange() throws InterruptedException { |
117 |
+ |
final Exchanger e = new Exchanger(); |
118 |
+ |
Thread t1 = new Thread(new CheckedInterruptedRunnable() { |
119 |
+ |
public void realRun() throws InterruptedException { |
120 |
+ |
assertSame(two, e.exchange(one)); |
121 |
+ |
e.exchange(two); |
122 |
+ |
}}); |
123 |
+ |
Thread t2 = new Thread(new CheckedRunnable() { |
124 |
+ |
public void realRun() throws InterruptedException { |
125 |
+ |
assertSame(one, e.exchange(two)); |
126 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
127 |
+ |
assertSame(three, e.exchange(one)); |
128 |
+ |
}}); |
129 |
+ |
Thread t3 = new Thread(new CheckedRunnable() { |
130 |
+ |
public void realRun() throws InterruptedException { |
131 |
+ |
Thread.sleep(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); |
139 |
+ |
t1.interrupt(); |
140 |
+ |
t1.join(); |
141 |
+ |
t2.join(); |
142 |
+ |
t3.join(); |
143 |
+ |
} |
144 |
+ |
|
145 |
|
} |