54 |
|
/** |
55 |
|
* await returns after countDown to zero, but not before |
56 |
|
*/ |
57 |
< |
public void testAwait() throws InterruptedException { |
57 |
> |
public void testAwait() { |
58 |
|
final CountDownLatch l = new CountDownLatch(2); |
59 |
+ |
final CountDownLatch pleaseCountDown = new CountDownLatch(1); |
60 |
|
|
61 |
< |
Thread t = new Thread(new CheckedRunnable() { |
61 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
62 |
|
public void realRun() throws InterruptedException { |
63 |
< |
assertTrue(l.getCount() > 0); |
63 |
> |
assertEquals(2, l.getCount()); |
64 |
> |
pleaseCountDown.countDown(); |
65 |
|
l.await(); |
66 |
|
assertEquals(0, l.getCount()); |
67 |
|
}}); |
68 |
|
|
69 |
< |
t.start(); |
70 |
< |
assertEquals(l.getCount(), 2); |
69 |
< |
delay(SHORT_DELAY_MS); |
69 |
> |
await(pleaseCountDown); |
70 |
> |
assertEquals(2, l.getCount()); |
71 |
|
l.countDown(); |
72 |
< |
assertEquals(l.getCount(), 1); |
72 |
> |
assertEquals(1, l.getCount()); |
73 |
> |
assertThreadStaysAlive(t); |
74 |
|
l.countDown(); |
75 |
< |
assertEquals(l.getCount(), 0); |
76 |
< |
t.join(); |
75 |
> |
assertEquals(0, l.getCount()); |
76 |
> |
awaitTermination(t); |
77 |
|
} |
78 |
|
|
79 |
|
/** |
80 |
|
* timed await returns after countDown to zero |
81 |
|
*/ |
82 |
< |
public void testTimedAwait() throws InterruptedException { |
82 |
> |
public void testTimedAwait() { |
83 |
|
final CountDownLatch l = new CountDownLatch(2); |
84 |
+ |
final CountDownLatch pleaseCountDown = new CountDownLatch(1); |
85 |
|
|
86 |
< |
Thread t = new Thread(new CheckedRunnable() { |
86 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
87 |
|
public void realRun() throws InterruptedException { |
88 |
< |
assertTrue(l.getCount() > 0); |
89 |
< |
assertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS)); |
88 |
> |
assertEquals(2, l.getCount()); |
89 |
> |
pleaseCountDown.countDown(); |
90 |
> |
assertTrue(l.await(LONG_DELAY_MS, MILLISECONDS)); |
91 |
> |
assertEquals(0, l.getCount()); |
92 |
|
}}); |
93 |
|
|
94 |
< |
t.start(); |
95 |
< |
assertEquals(l.getCount(), 2); |
91 |
< |
delay(SHORT_DELAY_MS); |
94 |
> |
await(pleaseCountDown); |
95 |
> |
assertEquals(2, l.getCount()); |
96 |
|
l.countDown(); |
97 |
< |
assertEquals(l.getCount(), 1); |
97 |
> |
assertEquals(1, l.getCount()); |
98 |
> |
assertThreadStaysAlive(t); |
99 |
|
l.countDown(); |
100 |
< |
assertEquals(l.getCount(), 0); |
101 |
< |
t.join(); |
100 |
> |
assertEquals(0, l.getCount()); |
101 |
> |
awaitTermination(t); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
|
* await throws IE if interrupted before counted down |
106 |
|
*/ |
107 |
< |
public void testAwait_InterruptedException() throws InterruptedException { |
107 |
> |
public void testAwait_Interruptible() { |
108 |
|
final CountDownLatch l = new CountDownLatch(1); |
109 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
109 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
110 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
111 |
|
public void realRun() throws InterruptedException { |
112 |
< |
assertTrue(l.getCount() > 0); |
113 |
< |
l.await(); |
112 |
> |
Thread.currentThread().interrupt(); |
113 |
> |
try { |
114 |
> |
l.await(); |
115 |
> |
shouldThrow(); |
116 |
> |
} catch (InterruptedException success) {} |
117 |
> |
assertFalse(Thread.interrupted()); |
118 |
> |
|
119 |
> |
pleaseInterrupt.countDown(); |
120 |
> |
try { |
121 |
> |
l.await(); |
122 |
> |
shouldThrow(); |
123 |
> |
} catch (InterruptedException success) {} |
124 |
> |
assertFalse(Thread.interrupted()); |
125 |
> |
|
126 |
> |
assertEquals(1, l.getCount()); |
127 |
|
}}); |
128 |
|
|
129 |
< |
t.start(); |
130 |
< |
assertEquals(l.getCount(), 1); |
129 |
> |
await(pleaseInterrupt); |
130 |
> |
assertThreadStaysAlive(t); |
131 |
|
t.interrupt(); |
132 |
< |
t.join(); |
132 |
> |
awaitTermination(t); |
133 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
|
* timed await throws IE if interrupted before counted down |
137 |
|
*/ |
138 |
< |
public void testTimedAwait_InterruptedException() throws InterruptedException { |
138 |
> |
public void testTimedAwait_Interruptible() { |
139 |
|
final CountDownLatch l = new CountDownLatch(1); |
140 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
140 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
141 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
142 |
|
public void realRun() throws InterruptedException { |
143 |
< |
assertTrue(l.getCount() > 0); |
144 |
< |
l.await(MEDIUM_DELAY_MS, MILLISECONDS); |
143 |
> |
Thread.currentThread().interrupt(); |
144 |
> |
try { |
145 |
> |
l.await(LONG_DELAY_MS, MILLISECONDS); |
146 |
> |
shouldThrow(); |
147 |
> |
} catch (InterruptedException success) {} |
148 |
> |
assertFalse(Thread.interrupted()); |
149 |
> |
|
150 |
> |
pleaseInterrupt.countDown(); |
151 |
> |
try { |
152 |
> |
l.await(LONG_DELAY_MS, MILLISECONDS); |
153 |
> |
shouldThrow(); |
154 |
> |
} catch (InterruptedException success) {} |
155 |
> |
assertFalse(Thread.interrupted()); |
156 |
> |
|
157 |
> |
assertEquals(1, l.getCount()); |
158 |
|
}}); |
159 |
|
|
160 |
< |
t.start(); |
161 |
< |
delay(SHORT_DELAY_MS); |
129 |
< |
assertEquals(l.getCount(), 1); |
160 |
> |
await(pleaseInterrupt); |
161 |
> |
assertThreadStaysAlive(t); |
162 |
|
t.interrupt(); |
163 |
< |
t.join(); |
163 |
> |
awaitTermination(t); |
164 |
|
} |
165 |
|
|
166 |
|
/** |
168 |
|
*/ |
169 |
|
public void testAwaitTimeout() throws InterruptedException { |
170 |
|
final CountDownLatch l = new CountDownLatch(1); |
171 |
< |
Thread t = new Thread(new CheckedRunnable() { |
171 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
172 |
|
public void realRun() throws InterruptedException { |
173 |
< |
assertTrue(l.getCount() > 0); |
174 |
< |
assertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS)); |
175 |
< |
assertTrue(l.getCount() > 0); |
173 |
> |
assertEquals(1, l.getCount()); |
174 |
> |
assertFalse(l.await(timeoutMillis(), MILLISECONDS)); |
175 |
> |
assertEquals(1, l.getCount()); |
176 |
|
}}); |
177 |
|
|
178 |
< |
t.start(); |
179 |
< |
assertEquals(l.getCount(), 1); |
148 |
< |
t.join(); |
178 |
> |
awaitTermination(t); |
179 |
> |
assertEquals(1, l.getCount()); |
180 |
|
} |
181 |
|
|
182 |
|
/** |