18 |
|
} |
19 |
|
|
20 |
|
/** |
21 |
< |
* |
21 |
> |
* negative constructor argument throws IAE |
22 |
|
*/ |
23 |
|
public void testConstructor() { |
24 |
|
try { |
28 |
|
} |
29 |
|
|
30 |
|
/** |
31 |
< |
* |
31 |
> |
* getCount returns initial count and decreases after countDown |
32 |
|
*/ |
33 |
|
public void testGetCount() { |
34 |
|
final CountDownLatch l = new CountDownLatch(2); |
38 |
|
} |
39 |
|
|
40 |
|
/** |
41 |
< |
* |
41 |
> |
* countDown has no effect when count is 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); |
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 |
|
threadUnexpectedException(); |
66 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
/** |
85 |
< |
* |
85 |
> |
* timed await returns after countDown to zero |
86 |
|
*/ |
87 |
|
public void testTimedAwait() { |
88 |
|
final CountDownLatch l = new CountDownLatch(2); |
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 |
|
threadUnexpectedException(); |
111 |
|
} |
112 |
|
} |
113 |
|
|
99 |
– |
|
100 |
– |
|
101 |
– |
|
114 |
|
/** |
115 |
< |
* |
115 |
> |
* await throws IE ig 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() { |
121 |
|
try { |
122 |
+ |
threadAssertTrue(l.getCount() > 0); |
123 |
|
l.await(); |
124 |
|
threadShouldThrow(); |
125 |
|
} catch(InterruptedException success){} |
136 |
|
} |
137 |
|
|
138 |
|
/** |
139 |
< |
* |
139 |
> |
* timed await throws IE ig 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() { |
145 |
|
try { |
146 |
+ |
threadAssertTrue(l.getCount() > 0); |
147 |
|
l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
148 |
|
threadShouldThrow(); |
149 |
|
} catch(InterruptedException success){} |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
< |
* |
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() { |
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 |
|
threadUnexpectedException(); |
176 |
|
} |