9 |
|
import java.util.*; |
10 |
|
import java.util.concurrent.*; |
11 |
|
|
12 |
< |
public class ExchangerTest extends TestCase{ |
12 |
> |
public class ExchangerTest extends JSR166TestCase { |
13 |
|
|
14 |
|
public static void main(String[] args) { |
15 |
|
junit.textui.TestRunner.run (suite()); |
18 |
|
return new TestSuite(ExchangerTest.class); |
19 |
|
} |
20 |
|
|
21 |
< |
private static long SHORT_DELAY_MS = 100; |
22 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
23 |
< |
private static long LONG_DELAY_MS = 10000; |
24 |
< |
|
25 |
< |
private final static Integer one = new Integer(1); |
26 |
< |
private final static Integer two = new Integer(2); |
27 |
< |
|
28 |
< |
public void testExchange(){ |
21 |
> |
public void testExchange() { |
22 |
|
final Exchanger e = new Exchanger(); |
23 |
|
Thread t1 = new Thread(new Runnable(){ |
24 |
|
public void run(){ |
25 |
< |
try{ |
25 |
> |
try { |
26 |
|
Object v = e.exchange(one); |
27 |
< |
assertEquals(v, two); |
27 |
> |
threadAssertEquals(v, two); |
28 |
|
Object w = e.exchange(v); |
29 |
< |
assertEquals(w, one); |
30 |
< |
}catch(InterruptedException e){ |
31 |
< |
fail("unexpected exception"); |
29 |
> |
threadAssertEquals(w, one); |
30 |
> |
} catch(InterruptedException e){ |
31 |
> |
threadFail("unexpected exception"); |
32 |
|
} |
33 |
|
} |
34 |
|
}); |
35 |
|
Thread t2 = new Thread(new Runnable(){ |
36 |
|
public void run(){ |
37 |
< |
try{ |
37 |
> |
try { |
38 |
|
Object v = e.exchange(two); |
39 |
< |
assertEquals(v, one); |
39 |
> |
threadAssertEquals(v, one); |
40 |
|
Object w = e.exchange(v); |
41 |
< |
assertEquals(w, two); |
42 |
< |
}catch(InterruptedException e){ |
43 |
< |
fail("unexpected exception"); |
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 |
|
}); |
94 |
|
} |
95 |
|
} |
96 |
|
|
97 |
< |
public void testExchange1_InterruptedException(){ |
97 |
> |
public void testExchange_InterruptedException(){ |
98 |
|
final Exchanger e = new Exchanger(); |
99 |
|
Thread t = new Thread(new Runnable() { |
100 |
|
public void run(){ |
101 |
< |
try{ |
101 |
> |
try { |
102 |
|
e.exchange(one); |
103 |
< |
fail("should throw"); |
104 |
< |
}catch(InterruptedException success){ |
103 |
> |
threadFail("should throw"); |
104 |
> |
} catch(InterruptedException success){ |
105 |
|
} |
106 |
|
} |
107 |
|
}); |
108 |
< |
try{ |
108 |
> |
try { |
109 |
|
t.start(); |
110 |
|
Thread.sleep(SHORT_DELAY_MS); |
111 |
|
t.interrupt(); |
115 |
|
} |
116 |
|
} |
117 |
|
|
118 |
< |
public void testExchange2_InterruptedException(){ |
118 |
> |
public void testTimedExchange_InterruptedException(){ |
119 |
|
final Exchanger e = new Exchanger(); |
120 |
|
Thread t = new Thread(new Runnable() { |
121 |
|
public void run(){ |
122 |
< |
try{ |
122 |
> |
try { |
123 |
|
e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
124 |
< |
fail("should throw"); |
124 |
> |
threadFail("should throw"); |
125 |
|
} catch(InterruptedException success){ |
126 |
|
} catch(Exception e2){ |
127 |
< |
fail("should throw IE"); |
127 |
> |
threadFail("should throw IE"); |
128 |
|
} |
129 |
|
} |
130 |
|
}); |
131 |
< |
try{ |
131 |
> |
try { |
132 |
|
t.start(); |
133 |
|
t.interrupt(); |
134 |
|
t.join(); |
137 |
|
} |
138 |
|
} |
139 |
|
|
140 |
< |
public void testExchange3_TimeOutException(){ |
140 |
> |
public void testExchange_TimeOutException(){ |
141 |
|
final Exchanger e = new Exchanger(); |
142 |
|
Thread t = new Thread(new Runnable() { |
143 |
|
public void run(){ |
144 |
< |
try{ |
144 |
> |
try { |
145 |
|
e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
146 |
< |
fail("should throw"); |
147 |
< |
}catch(TimeoutException success){} |
148 |
< |
catch(InterruptedException e2){ |
149 |
< |
fail("should throw TOE"); |
146 |
> |
threadFail("should throw"); |
147 |
> |
} catch(TimeoutException success){ |
148 |
> |
} catch(InterruptedException e2){ |
149 |
> |
threadFail("should throw TOE"); |
150 |
|
} |
151 |
|
} |
152 |
|
}); |
153 |
< |
try{ |
153 |
> |
try { |
154 |
|
t.start(); |
155 |
|
t.join(); |
156 |
|
} catch(InterruptedException ex){ |