9 |
|
import java.util.*; |
10 |
|
import java.util.concurrent.*; |
11 |
|
|
12 |
< |
public class CountDownLatchTest extends TestCase{ |
13 |
< |
|
12 |
> |
public class CountDownLatchTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
|
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
17 |
– |
|
18 |
– |
|
16 |
|
public static Test suite() { |
17 |
|
return new TestSuite(CountDownLatchTest.class); |
18 |
|
} |
19 |
|
|
20 |
< |
private static long SHORT_DELAY_MS = 100; |
21 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
22 |
< |
private static long LONG_DELAY_MS = 10000; |
20 |
> |
public void testConstructor(){ |
21 |
> |
try { |
22 |
> |
new CountDownLatch(-1); |
23 |
> |
fail("should throw IllegalArgumentException"); |
24 |
> |
} catch(IllegalArgumentException success){} |
25 |
> |
} |
26 |
|
|
27 |
|
public void testGetCount(){ |
28 |
|
final CountDownLatch l = new CountDownLatch(2); |
31 |
|
assertEquals(1, l.getCount()); |
32 |
|
} |
33 |
|
|
34 |
< |
public void testAwait1(){ |
34 |
> |
public void testAwait(){ |
35 |
|
final CountDownLatch l = new CountDownLatch(2); |
36 |
|
|
37 |
|
Thread t = new Thread(new Runnable(){ |
38 |
|
public void run(){ |
39 |
< |
try{ |
39 |
> |
try { |
40 |
|
l.await(); |
41 |
< |
}catch(InterruptedException e){ |
42 |
< |
fail("unexpected exception"); |
41 |
> |
} catch(InterruptedException e){ |
42 |
> |
threadFail("unexpected exception"); |
43 |
|
} |
44 |
|
} |
45 |
|
}); |
46 |
|
t.start(); |
47 |
< |
try{ |
47 |
> |
try { |
48 |
|
assertEquals(l.getCount(), 2); |
49 |
|
Thread.sleep(SHORT_DELAY_MS); |
50 |
|
l.countDown(); |
52 |
|
l.countDown(); |
53 |
|
assertEquals(l.getCount(), 0); |
54 |
|
t.join(); |
55 |
< |
}catch (InterruptedException e){ |
55 |
> |
} catch (InterruptedException e){ |
56 |
|
fail("unexpected exception"); |
57 |
|
} |
58 |
|
} |
59 |
|
|
60 |
|
|
61 |
+ |
public void testTimedAwait(){ |
62 |
+ |
final CountDownLatch l = new CountDownLatch(2); |
63 |
|
|
64 |
< |
public void testConstructor(){ |
65 |
< |
try{ |
66 |
< |
new CountDownLatch(-1); |
67 |
< |
fail("should throw IllegalArgumentException"); |
68 |
< |
}catch(IllegalArgumentException success){} |
64 |
> |
Thread t = new Thread(new Runnable(){ |
65 |
> |
public void run(){ |
66 |
> |
try { |
67 |
> |
threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS)); |
68 |
> |
} catch(InterruptedException e){ |
69 |
> |
threadFail("unexpected exception"); |
70 |
> |
} |
71 |
> |
} |
72 |
> |
}); |
73 |
> |
t.start(); |
74 |
> |
try { |
75 |
> |
assertEquals(l.getCount(), 2); |
76 |
> |
Thread.sleep(SHORT_DELAY_MS); |
77 |
> |
l.countDown(); |
78 |
> |
assertEquals(l.getCount(), 1); |
79 |
> |
l.countDown(); |
80 |
> |
assertEquals(l.getCount(), 0); |
81 |
> |
t.join(); |
82 |
> |
} catch (InterruptedException e){ |
83 |
> |
fail("unexpected exception"); |
84 |
> |
} |
85 |
|
} |
86 |
+ |
|
87 |
+ |
|
88 |
+ |
|
89 |
|
|
90 |
< |
public void testAwait1_InterruptedException(){ |
90 |
> |
public void testAwait_InterruptedException(){ |
91 |
|
final CountDownLatch l = new CountDownLatch(1); |
92 |
|
Thread t = new Thread(new Runnable(){ |
93 |
|
public void run(){ |
94 |
< |
try{ |
94 |
> |
try { |
95 |
|
l.await(); |
96 |
< |
fail("should throw"); |
97 |
< |
}catch(InterruptedException success){} |
96 |
> |
threadFail("should throw"); |
97 |
> |
} catch(InterruptedException success){} |
98 |
|
} |
99 |
|
}); |
100 |
|
t.start(); |
101 |
< |
try{ |
101 |
> |
try { |
102 |
|
assertEquals(l.getCount(), 1); |
103 |
|
t.interrupt(); |
104 |
|
t.join(); |
105 |
< |
}catch (InterruptedException e){ |
105 |
> |
} catch (InterruptedException e){ |
106 |
|
fail("unexpected exception"); |
107 |
|
} |
108 |
|
} |
109 |
|
|
110 |
< |
public void testAwait2_InterruptedException(){ |
110 |
> |
public void testTimedAwait_InterruptedException(){ |
111 |
|
final CountDownLatch l = new CountDownLatch(1); |
112 |
|
Thread t = new Thread(new Runnable(){ |
113 |
|
public void run(){ |
114 |
< |
try{ |
114 |
> |
try { |
115 |
|
l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
116 |
< |
fail("should throw"); |
117 |
< |
}catch(InterruptedException success){} |
116 |
> |
threadFail("should throw"); |
117 |
> |
} catch(InterruptedException success){} |
118 |
|
} |
119 |
|
}); |
120 |
|
t.start(); |
121 |
< |
try{ |
121 |
> |
try { |
122 |
|
Thread.sleep(SHORT_DELAY_MS); |
123 |
|
assertEquals(l.getCount(), 1); |
124 |
|
t.interrupt(); |
125 |
|
t.join(); |
126 |
< |
}catch (InterruptedException e){ |
126 |
> |
} catch (InterruptedException e){ |
127 |
|
fail("unexpected exception"); |
128 |
|
} |
129 |
|
} |
132 |
|
final CountDownLatch l = new CountDownLatch(1); |
133 |
|
Thread t = new Thread(new Runnable(){ |
134 |
|
public void run(){ |
135 |
< |
try{ |
136 |
< |
assertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
137 |
< |
}catch(InterruptedException ie){ |
138 |
< |
fail("unexpected exception"); |
135 |
> |
try { |
136 |
> |
threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
137 |
> |
} catch(InterruptedException ie){ |
138 |
> |
threadFail("unexpected exception"); |
139 |
|
} |
140 |
|
} |
141 |
|
}); |
142 |
|
t.start(); |
143 |
< |
try{ |
143 |
> |
try { |
144 |
|
assertEquals(l.getCount(), 1); |
145 |
|
t.join(); |
146 |
< |
}catch (InterruptedException e){ |
146 |
> |
} catch (InterruptedException e){ |
147 |
|
fail("unexpected exception"); |
148 |
|
} |
149 |
|
} |