17 |
|
return new TestSuite(CountDownLatchTest.class); |
18 |
|
} |
19 |
|
|
20 |
< |
public void testConstructor(){ |
20 |
> |
/** |
21 |
> |
* negative constructor argument throws IAE |
22 |
> |
*/ |
23 |
> |
public void testConstructor() { |
24 |
|
try { |
25 |
|
new CountDownLatch(-1); |
26 |
< |
fail("should throw IllegalArgumentException"); |
26 |
> |
shouldThrow(); |
27 |
|
} catch(IllegalArgumentException success){} |
28 |
|
} |
29 |
|
|
30 |
< |
public void testGetCount(){ |
30 |
> |
/** |
31 |
> |
* getCount returns initial count and decreases after countDown |
32 |
> |
*/ |
33 |
> |
public void testGetCount() { |
34 |
|
final CountDownLatch l = new CountDownLatch(2); |
35 |
|
assertEquals(2, l.getCount()); |
36 |
|
l.countDown(); |
37 |
|
assertEquals(1, l.getCount()); |
38 |
|
} |
39 |
|
|
40 |
< |
public void testAwait(){ |
40 |
> |
/** |
41 |
> |
* countDown decrements count when positive and has no effect when zero |
42 |
> |
*/ |
43 |
> |
public void testCountDown() { |
44 |
> |
final CountDownLatch l = new CountDownLatch(1); |
45 |
> |
assertEquals(1, l.getCount()); |
46 |
> |
l.countDown(); |
47 |
> |
assertEquals(0, l.getCount()); |
48 |
> |
l.countDown(); |
49 |
> |
assertEquals(0, l.getCount()); |
50 |
> |
} |
51 |
> |
|
52 |
> |
/** |
53 |
> |
* await returns after countDown to zero, but not before |
54 |
> |
*/ |
55 |
> |
public void testAwait() { |
56 |
|
final CountDownLatch l = new CountDownLatch(2); |
57 |
|
|
58 |
< |
Thread t = new Thread(new Runnable(){ |
59 |
< |
public void run(){ |
58 |
> |
Thread t = new Thread(new Runnable() { |
59 |
> |
public void run() { |
60 |
|
try { |
61 |
+ |
threadAssertTrue(l.getCount() > 0); |
62 |
|
l.await(); |
63 |
+ |
threadAssertTrue(l.getCount() == 0); |
64 |
|
} catch(InterruptedException e){ |
65 |
< |
threadFail("unexpected exception"); |
65 |
> |
threadUnexpectedException(); |
66 |
|
} |
67 |
|
} |
68 |
|
}); |
76 |
|
assertEquals(l.getCount(), 0); |
77 |
|
t.join(); |
78 |
|
} catch (InterruptedException e){ |
79 |
< |
fail("unexpected exception"); |
79 |
> |
unexpectedException(); |
80 |
|
} |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
< |
public void testTimedAwait(){ |
84 |
> |
/** |
85 |
> |
* timed await returns after countDown to zero |
86 |
> |
*/ |
87 |
> |
public void testTimedAwait() { |
88 |
|
final CountDownLatch l = new CountDownLatch(2); |
89 |
|
|
90 |
< |
Thread t = new Thread(new Runnable(){ |
91 |
< |
public void run(){ |
90 |
> |
Thread t = new Thread(new Runnable() { |
91 |
> |
public void run() { |
92 |
|
try { |
93 |
+ |
threadAssertTrue(l.getCount() > 0); |
94 |
|
threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS)); |
95 |
|
} catch(InterruptedException e){ |
96 |
< |
threadFail("unexpected exception"); |
96 |
> |
threadUnexpectedException(); |
97 |
|
} |
98 |
|
} |
99 |
|
}); |
107 |
|
assertEquals(l.getCount(), 0); |
108 |
|
t.join(); |
109 |
|
} catch (InterruptedException e){ |
110 |
< |
fail("unexpected exception"); |
110 |
> |
unexpectedException(); |
111 |
|
} |
112 |
|
} |
113 |
|
|
114 |
< |
|
115 |
< |
|
116 |
< |
|
117 |
< |
public void testAwait_InterruptedException(){ |
114 |
> |
/** |
115 |
> |
* await throws IE if interrupted before counted down |
116 |
> |
*/ |
117 |
> |
public void testAwait_InterruptedException() { |
118 |
|
final CountDownLatch l = new CountDownLatch(1); |
119 |
< |
Thread t = new Thread(new Runnable(){ |
120 |
< |
public void run(){ |
119 |
> |
Thread t = new Thread(new Runnable() { |
120 |
> |
public void run() { |
121 |
|
try { |
122 |
+ |
threadAssertTrue(l.getCount() > 0); |
123 |
|
l.await(); |
124 |
< |
threadFail("should throw"); |
124 |
> |
threadShouldThrow(); |
125 |
|
} catch(InterruptedException success){} |
126 |
|
} |
127 |
|
}); |
131 |
|
t.interrupt(); |
132 |
|
t.join(); |
133 |
|
} catch (InterruptedException e){ |
134 |
< |
fail("unexpected exception"); |
134 |
> |
unexpectedException(); |
135 |
|
} |
136 |
|
} |
137 |
|
|
138 |
< |
public void testTimedAwait_InterruptedException(){ |
138 |
> |
/** |
139 |
> |
* timed await throws IE if interrupted before counted down |
140 |
> |
*/ |
141 |
> |
public void testTimedAwait_InterruptedException() { |
142 |
|
final CountDownLatch l = new CountDownLatch(1); |
143 |
< |
Thread t = new Thread(new Runnable(){ |
144 |
< |
public void run(){ |
143 |
> |
Thread t = new Thread(new Runnable() { |
144 |
> |
public void run() { |
145 |
|
try { |
146 |
+ |
threadAssertTrue(l.getCount() > 0); |
147 |
|
l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
148 |
< |
threadFail("should throw"); |
148 |
> |
threadShouldThrow(); |
149 |
|
} catch(InterruptedException success){} |
150 |
|
} |
151 |
|
}); |
156 |
|
t.interrupt(); |
157 |
|
t.join(); |
158 |
|
} catch (InterruptedException e){ |
159 |
< |
fail("unexpected exception"); |
159 |
> |
unexpectedException(); |
160 |
|
} |
161 |
|
} |
162 |
|
|
163 |
< |
public void testAwaitTimeout(){ |
163 |
> |
/** |
164 |
> |
* timed await times out if not counted down before timeout |
165 |
> |
*/ |
166 |
> |
public void testAwaitTimeout() { |
167 |
|
final CountDownLatch l = new CountDownLatch(1); |
168 |
< |
Thread t = new Thread(new Runnable(){ |
169 |
< |
public void run(){ |
168 |
> |
Thread t = new Thread(new Runnable() { |
169 |
> |
public void run() { |
170 |
|
try { |
171 |
+ |
threadAssertTrue(l.getCount() > 0); |
172 |
|
threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
173 |
+ |
threadAssertTrue(l.getCount() > 0); |
174 |
|
} catch(InterruptedException ie){ |
175 |
< |
threadFail("unexpected exception"); |
175 |
> |
threadUnexpectedException(); |
176 |
|
} |
177 |
|
} |
178 |
|
}); |
181 |
|
assertEquals(l.getCount(), 1); |
182 |
|
t.join(); |
183 |
|
} catch (InterruptedException e){ |
184 |
< |
fail("unexpected exception"); |
184 |
> |
unexpectedException(); |
185 |
|
} |
186 |
|
} |
187 |
|
|