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); |
59 |
|
|
60 |
|
Thread t = new Thread(new CheckedRunnable() { |
61 |
|
public void realRun() throws InterruptedException { |
62 |
< |
threadAssertTrue(l.getCount() > 0); |
62 |
> |
assertTrue(l.getCount() > 0); |
63 |
|
l.await(); |
64 |
< |
threadAssertTrue(l.getCount() == 0); |
64 |
> |
assertEquals(0, l.getCount()); |
65 |
|
}}); |
66 |
|
|
67 |
|
t.start(); |
68 |
|
assertEquals(l.getCount(), 2); |
69 |
< |
Thread.sleep(SHORT_DELAY_MS); |
69 |
> |
delay(SHORT_DELAY_MS); |
70 |
|
l.countDown(); |
71 |
|
assertEquals(l.getCount(), 1); |
72 |
|
l.countDown(); |
83 |
|
|
84 |
|
Thread t = new Thread(new CheckedRunnable() { |
85 |
|
public void realRun() throws InterruptedException { |
86 |
< |
threadAssertTrue(l.getCount() > 0); |
87 |
< |
threadAssertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS)); |
86 |
> |
assertTrue(l.getCount() > 0); |
87 |
> |
assertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS)); |
88 |
|
}}); |
89 |
|
|
90 |
|
t.start(); |
91 |
|
assertEquals(l.getCount(), 2); |
92 |
< |
Thread.sleep(SHORT_DELAY_MS); |
92 |
> |
delay(SHORT_DELAY_MS); |
93 |
|
l.countDown(); |
94 |
|
assertEquals(l.getCount(), 1); |
95 |
|
l.countDown(); |
104 |
|
final CountDownLatch l = new CountDownLatch(1); |
105 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
106 |
|
public void realRun() throws InterruptedException { |
107 |
< |
threadAssertTrue(l.getCount() > 0); |
107 |
> |
assertTrue(l.getCount() > 0); |
108 |
|
l.await(); |
109 |
|
}}); |
110 |
|
|
121 |
|
final CountDownLatch l = new CountDownLatch(1); |
122 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
123 |
|
public void realRun() throws InterruptedException { |
124 |
< |
threadAssertTrue(l.getCount() > 0); |
124 |
> |
assertTrue(l.getCount() > 0); |
125 |
|
l.await(MEDIUM_DELAY_MS, MILLISECONDS); |
126 |
|
}}); |
127 |
|
|
128 |
|
t.start(); |
129 |
< |
Thread.sleep(SHORT_DELAY_MS); |
129 |
> |
delay(SHORT_DELAY_MS); |
130 |
|
assertEquals(l.getCount(), 1); |
131 |
|
t.interrupt(); |
132 |
|
t.join(); |
139 |
|
final CountDownLatch l = new CountDownLatch(1); |
140 |
|
Thread t = new Thread(new CheckedRunnable() { |
141 |
|
public void realRun() throws InterruptedException { |
142 |
< |
threadAssertTrue(l.getCount() > 0); |
143 |
< |
threadAssertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS)); |
144 |
< |
threadAssertTrue(l.getCount() > 0); |
142 |
> |
assertTrue(l.getCount() > 0); |
143 |
> |
assertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS)); |
144 |
> |
assertTrue(l.getCount() > 0); |
145 |
|
}}); |
146 |
|
|
147 |
|
t.start(); |
154 |
|
*/ |
155 |
|
public void testToString() { |
156 |
|
CountDownLatch s = new CountDownLatch(2); |
157 |
< |
String us = s.toString(); |
158 |
< |
assertTrue(us.indexOf("Count = 2") >= 0); |
157 |
> |
assertTrue(s.toString().contains("Count = 2")); |
158 |
|
s.countDown(); |
159 |
< |
String s1 = s.toString(); |
161 |
< |
assertTrue(s1.indexOf("Count = 1") >= 0); |
159 |
> |
assertTrue(s.toString().contains("Count = 1")); |
160 |
|
s.countDown(); |
161 |
< |
String s2 = s.toString(); |
164 |
< |
assertTrue(s2.indexOf("Count = 0") >= 0); |
161 |
> |
assertTrue(s.toString().contains("Count = 0")); |
162 |
|
} |
163 |
|
|
164 |
|
} |