1 |
|
/* |
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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
13 |
|
|
14 |
|
public class CountDownLatchTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run(suite()); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(CountDownLatchTest.class); |
54 |
|
/** |
55 |
|
* await returns after countDown to zero, but not before |
56 |
|
*/ |
57 |
< |
public void testAwait() { |
57 |
> |
public void testAwait() throws InterruptedException { |
58 |
|
final CountDownLatch l = new CountDownLatch(2); |
59 |
|
|
60 |
< |
Thread t = new Thread(new Runnable() { |
61 |
< |
public void run() { |
62 |
< |
try { |
63 |
< |
threadAssertTrue(l.getCount() > 0); |
64 |
< |
l.await(); |
65 |
< |
threadAssertTrue(l.getCount() == 0); |
66 |
< |
} catch (InterruptedException e) { |
67 |
< |
threadUnexpectedException(); |
68 |
< |
} |
69 |
< |
} |
70 |
< |
}); |
60 |
> |
Thread t = new Thread(new CheckedRunnable() { |
61 |
> |
public void realRun() throws InterruptedException { |
62 |
> |
assertTrue(l.getCount() > 0); |
63 |
> |
l.await(); |
64 |
> |
assertEquals(0, l.getCount()); |
65 |
> |
}}); |
66 |
> |
|
67 |
|
t.start(); |
68 |
< |
try { |
69 |
< |
assertEquals(l.getCount(), 2); |
70 |
< |
Thread.sleep(SHORT_DELAY_MS); |
71 |
< |
l.countDown(); |
72 |
< |
assertEquals(l.getCount(), 1); |
73 |
< |
l.countDown(); |
74 |
< |
assertEquals(l.getCount(), 0); |
79 |
< |
t.join(); |
80 |
< |
} catch (InterruptedException e) { |
81 |
< |
unexpectedException(); |
82 |
< |
} |
68 |
> |
assertEquals(l.getCount(), 2); |
69 |
> |
delay(SHORT_DELAY_MS); |
70 |
> |
l.countDown(); |
71 |
> |
assertEquals(l.getCount(), 1); |
72 |
> |
l.countDown(); |
73 |
> |
assertEquals(l.getCount(), 0); |
74 |
> |
t.join(); |
75 |
|
} |
76 |
|
|
85 |
– |
|
77 |
|
/** |
78 |
|
* timed await returns after countDown to zero |
79 |
|
*/ |
80 |
< |
public void testTimedAwait() { |
80 |
> |
public void testTimedAwait() throws InterruptedException { |
81 |
|
final CountDownLatch l = new CountDownLatch(2); |
82 |
|
|
83 |
< |
Thread t = new Thread(new Runnable() { |
84 |
< |
public void run() { |
85 |
< |
try { |
86 |
< |
threadAssertTrue(l.getCount() > 0); |
87 |
< |
threadAssertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS)); |
88 |
< |
} catch (InterruptedException e) { |
98 |
< |
threadUnexpectedException(); |
99 |
< |
} |
100 |
< |
} |
101 |
< |
}); |
83 |
> |
Thread t = new Thread(new CheckedRunnable() { |
84 |
> |
public void realRun() throws InterruptedException { |
85 |
> |
assertTrue(l.getCount() > 0); |
86 |
> |
assertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS)); |
87 |
> |
}}); |
88 |
> |
|
89 |
|
t.start(); |
90 |
< |
try { |
91 |
< |
assertEquals(l.getCount(), 2); |
92 |
< |
Thread.sleep(SHORT_DELAY_MS); |
93 |
< |
l.countDown(); |
94 |
< |
assertEquals(l.getCount(), 1); |
95 |
< |
l.countDown(); |
96 |
< |
assertEquals(l.getCount(), 0); |
110 |
< |
t.join(); |
111 |
< |
} catch (InterruptedException e) { |
112 |
< |
unexpectedException(); |
113 |
< |
} |
90 |
> |
assertEquals(l.getCount(), 2); |
91 |
> |
delay(SHORT_DELAY_MS); |
92 |
> |
l.countDown(); |
93 |
> |
assertEquals(l.getCount(), 1); |
94 |
> |
l.countDown(); |
95 |
> |
assertEquals(l.getCount(), 0); |
96 |
> |
t.join(); |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
|
* await throws IE if interrupted before counted down |
101 |
|
*/ |
102 |
< |
public void testAwait_InterruptedException() { |
102 |
> |
public void testAwait_InterruptedException() throws InterruptedException { |
103 |
|
final CountDownLatch l = new CountDownLatch(1); |
104 |
< |
Thread t = new Thread(new Runnable() { |
105 |
< |
public void run() { |
106 |
< |
try { |
107 |
< |
threadAssertTrue(l.getCount() > 0); |
108 |
< |
l.await(); |
109 |
< |
threadShouldThrow(); |
127 |
< |
} catch (InterruptedException success) {} |
128 |
< |
} |
129 |
< |
}); |
104 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
105 |
> |
public void realRun() throws InterruptedException { |
106 |
> |
assertTrue(l.getCount() > 0); |
107 |
> |
l.await(); |
108 |
> |
}}); |
109 |
> |
|
110 |
|
t.start(); |
111 |
< |
try { |
112 |
< |
assertEquals(l.getCount(), 1); |
113 |
< |
t.interrupt(); |
134 |
< |
t.join(); |
135 |
< |
} catch (InterruptedException e) { |
136 |
< |
unexpectedException(); |
137 |
< |
} |
111 |
> |
assertEquals(l.getCount(), 1); |
112 |
> |
t.interrupt(); |
113 |
> |
t.join(); |
114 |
|
} |
115 |
|
|
116 |
|
/** |
117 |
|
* timed await throws IE if interrupted before counted down |
118 |
|
*/ |
119 |
< |
public void testTimedAwait_InterruptedException() { |
119 |
> |
public void testTimedAwait_InterruptedException() throws InterruptedException { |
120 |
|
final CountDownLatch l = new CountDownLatch(1); |
121 |
< |
Thread t = new Thread(new Runnable() { |
122 |
< |
public void run() { |
123 |
< |
try { |
124 |
< |
threadAssertTrue(l.getCount() > 0); |
125 |
< |
l.await(MEDIUM_DELAY_MS, MILLISECONDS); |
126 |
< |
threadShouldThrow(); |
151 |
< |
} catch (InterruptedException success) {} |
152 |
< |
} |
153 |
< |
}); |
121 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
122 |
> |
public void realRun() throws InterruptedException { |
123 |
> |
assertTrue(l.getCount() > 0); |
124 |
> |
l.await(MEDIUM_DELAY_MS, MILLISECONDS); |
125 |
> |
}}); |
126 |
> |
|
127 |
|
t.start(); |
128 |
< |
try { |
129 |
< |
Thread.sleep(SHORT_DELAY_MS); |
130 |
< |
assertEquals(l.getCount(), 1); |
131 |
< |
t.interrupt(); |
159 |
< |
t.join(); |
160 |
< |
} catch (InterruptedException e) { |
161 |
< |
unexpectedException(); |
162 |
< |
} |
128 |
> |
delay(SHORT_DELAY_MS); |
129 |
> |
assertEquals(l.getCount(), 1); |
130 |
> |
t.interrupt(); |
131 |
> |
t.join(); |
132 |
|
} |
133 |
|
|
134 |
|
/** |
135 |
|
* timed await times out if not counted down before timeout |
136 |
|
*/ |
137 |
< |
public void testAwaitTimeout() { |
137 |
> |
public void testAwaitTimeout() throws InterruptedException { |
138 |
|
final CountDownLatch l = new CountDownLatch(1); |
139 |
< |
Thread t = new Thread(new Runnable() { |
140 |
< |
public void run() { |
141 |
< |
try { |
142 |
< |
threadAssertTrue(l.getCount() > 0); |
143 |
< |
threadAssertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS)); |
144 |
< |
threadAssertTrue(l.getCount() > 0); |
145 |
< |
} catch (InterruptedException ie) { |
177 |
< |
threadUnexpectedException(); |
178 |
< |
} |
179 |
< |
} |
180 |
< |
}); |
139 |
> |
Thread t = new Thread(new CheckedRunnable() { |
140 |
> |
public void realRun() throws InterruptedException { |
141 |
> |
assertTrue(l.getCount() > 0); |
142 |
> |
assertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS)); |
143 |
> |
assertTrue(l.getCount() > 0); |
144 |
> |
}}); |
145 |
> |
|
146 |
|
t.start(); |
147 |
< |
try { |
148 |
< |
assertEquals(l.getCount(), 1); |
184 |
< |
t.join(); |
185 |
< |
} catch (InterruptedException e) { |
186 |
< |
unexpectedException(); |
187 |
< |
} |
147 |
> |
assertEquals(l.getCount(), 1); |
148 |
> |
t.join(); |
149 |
|
} |
150 |
|
|
151 |
|
/** |
153 |
|
*/ |
154 |
|
public void testToString() { |
155 |
|
CountDownLatch s = new CountDownLatch(2); |
156 |
< |
String us = s.toString(); |
196 |
< |
assertTrue(us.indexOf("Count = 2") >= 0); |
156 |
> |
assertTrue(s.toString().contains("Count = 2")); |
157 |
|
s.countDown(); |
158 |
< |
String s1 = s.toString(); |
199 |
< |
assertTrue(s1.indexOf("Count = 1") >= 0); |
158 |
> |
assertTrue(s.toString().contains("Count = 1")); |
159 |
|
s.countDown(); |
160 |
< |
String s2 = s.toString(); |
202 |
< |
assertTrue(s2.indexOf("Count = 0") >= 0); |
160 |
> |
assertTrue(s.toString().contains("Count = 0")); |
161 |
|
} |
162 |
|
|
163 |
|
} |