152 |
|
assertTrue(lock.isLocked()); |
153 |
|
assertTrue(lock.isHeldByCurrentThread()); |
154 |
|
} |
155 |
< |
|
155 |
> |
|
156 |
> |
public void testAwait_IllegalMonitor() { |
157 |
> |
final ReentrantLock lock = new ReentrantLock(); |
158 |
> |
final Condition c = lock.newCondition(); |
159 |
> |
try { |
160 |
> |
c.await(); |
161 |
> |
fail("should throw"); |
162 |
> |
} |
163 |
> |
catch (IllegalMonitorStateException success) { |
164 |
> |
} |
165 |
> |
catch (Exception ex) { |
166 |
> |
fail("should throw IMSE"); |
167 |
> |
} |
168 |
> |
} |
169 |
> |
|
170 |
> |
public void testSignal_IllegalMonitor() { |
171 |
> |
final ReentrantLock lock = new ReentrantLock(); |
172 |
> |
final Condition c = lock.newCondition(); |
173 |
> |
try { |
174 |
> |
c.signal(); |
175 |
> |
fail("should throw"); |
176 |
> |
} |
177 |
> |
catch (IllegalMonitorStateException success) { |
178 |
> |
} |
179 |
> |
catch (Exception ex) { |
180 |
> |
fail("should throw IMSE"); |
181 |
> |
} |
182 |
> |
} |
183 |
> |
|
184 |
> |
public void testAwaitNanos_Timeout() { |
185 |
> |
final ReentrantLock lock = new ReentrantLock(); |
186 |
> |
final Condition c = lock.newCondition(); |
187 |
> |
try { |
188 |
> |
lock.lock(); |
189 |
> |
long t = c.awaitNanos(100); |
190 |
> |
assertTrue(t <= 0); |
191 |
> |
lock.unlock(); |
192 |
> |
} |
193 |
> |
catch (Exception ex) { |
194 |
> |
fail("unexpected exception"); |
195 |
> |
} |
196 |
> |
} |
197 |
> |
|
198 |
> |
public void testAwait_Timeout() { |
199 |
> |
final ReentrantLock lock = new ReentrantLock(); |
200 |
> |
final Condition c = lock.newCondition(); |
201 |
> |
try { |
202 |
> |
lock.lock(); |
203 |
> |
assertFalse(c.await(10, TimeUnit.MILLISECONDS)); |
204 |
> |
lock.unlock(); |
205 |
> |
} |
206 |
> |
catch (Exception ex) { |
207 |
> |
fail("unexpected exception"); |
208 |
> |
} |
209 |
> |
} |
210 |
> |
|
211 |
> |
public void testAwaitUntil_Timeout() { |
212 |
> |
final ReentrantLock lock = new ReentrantLock(); |
213 |
> |
final Condition c = lock.newCondition(); |
214 |
> |
try { |
215 |
> |
lock.lock(); |
216 |
> |
java.util.Date d = new java.util.Date(); |
217 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
218 |
> |
lock.unlock(); |
219 |
> |
} |
220 |
> |
catch (Exception ex) { |
221 |
> |
fail("unexpected exception"); |
222 |
> |
} |
223 |
> |
} |
224 |
> |
|
225 |
> |
public void testAwait() { |
226 |
> |
final ReentrantLock lock = new ReentrantLock(); |
227 |
> |
final Condition c = lock.newCondition(); |
228 |
> |
Thread t = new Thread(new Runnable() { |
229 |
> |
public void run() { |
230 |
> |
try { |
231 |
> |
lock.lock(); |
232 |
> |
c.await(); |
233 |
> |
lock.unlock(); |
234 |
> |
} |
235 |
> |
catch(InterruptedException e) { |
236 |
> |
fail("unexpected exception"); |
237 |
> |
} |
238 |
> |
} |
239 |
> |
}); |
240 |
> |
|
241 |
> |
try { |
242 |
> |
t.start(); |
243 |
> |
Thread.sleep(SHORT_DELAY_MS); |
244 |
> |
lock.lock(); |
245 |
> |
c.signal(); |
246 |
> |
lock.unlock(); |
247 |
> |
t.join(SHORT_DELAY_MS); |
248 |
> |
assertFalse(t.isAlive()); |
249 |
> |
} |
250 |
> |
catch (Exception ex) { |
251 |
> |
fail("unexpected exception"); |
252 |
> |
} |
253 |
> |
} |
254 |
> |
|
255 |
> |
public void testAwaitUninterruptibly() { |
256 |
> |
final ReentrantLock lock = new ReentrantLock(); |
257 |
> |
final Condition c = lock.newCondition(); |
258 |
> |
Thread t = new Thread(new Runnable() { |
259 |
> |
public void run() { |
260 |
> |
lock.lock(); |
261 |
> |
c.awaitUninterruptibly(); |
262 |
> |
lock.unlock(); |
263 |
> |
} |
264 |
> |
}); |
265 |
> |
|
266 |
> |
try { |
267 |
> |
t.start(); |
268 |
> |
Thread.sleep(SHORT_DELAY_MS); |
269 |
> |
t.interrupt(); |
270 |
> |
lock.lock(); |
271 |
> |
c.signal(); |
272 |
> |
lock.unlock(); |
273 |
> |
t.join(SHORT_DELAY_MS); |
274 |
> |
assertFalse(t.isAlive()); |
275 |
> |
} |
276 |
> |
catch (Exception ex) { |
277 |
> |
fail("unexpected exception"); |
278 |
> |
} |
279 |
> |
} |
280 |
> |
|
281 |
> |
public void testAwait_Interrupt() { |
282 |
> |
final ReentrantLock lock = new ReentrantLock(); |
283 |
> |
final Condition c = lock.newCondition(); |
284 |
> |
Thread t = new Thread(new Runnable() { |
285 |
> |
public void run() { |
286 |
> |
try { |
287 |
> |
lock.lock(); |
288 |
> |
c.await(); |
289 |
> |
lock.unlock(); |
290 |
> |
fail("should throw"); |
291 |
> |
} |
292 |
> |
catch(InterruptedException success) { |
293 |
> |
} |
294 |
> |
} |
295 |
> |
}); |
296 |
> |
|
297 |
> |
try { |
298 |
> |
t.start(); |
299 |
> |
Thread.sleep(SHORT_DELAY_MS); |
300 |
> |
t.interrupt(); |
301 |
> |
t.join(SHORT_DELAY_MS); |
302 |
> |
assertFalse(t.isAlive()); |
303 |
> |
} |
304 |
> |
catch (Exception ex) { |
305 |
> |
fail("unexpected exception"); |
306 |
> |
} |
307 |
> |
} |
308 |
> |
|
309 |
> |
public void testAwaitNanos_Interrupt() { |
310 |
> |
final ReentrantLock lock = new ReentrantLock(); |
311 |
> |
final Condition c = lock.newCondition(); |
312 |
> |
Thread t = new Thread(new Runnable() { |
313 |
> |
public void run() { |
314 |
> |
try { |
315 |
> |
lock.lock(); |
316 |
> |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
317 |
> |
lock.unlock(); |
318 |
> |
fail("should throw"); |
319 |
> |
} |
320 |
> |
catch(InterruptedException success) { |
321 |
> |
} |
322 |
> |
} |
323 |
> |
}); |
324 |
> |
|
325 |
> |
try { |
326 |
> |
t.start(); |
327 |
> |
Thread.sleep(SHORT_DELAY_MS); |
328 |
> |
t.interrupt(); |
329 |
> |
t.join(SHORT_DELAY_MS); |
330 |
> |
assertFalse(t.isAlive()); |
331 |
> |
} |
332 |
> |
catch (Exception ex) { |
333 |
> |
fail("unexpected exception"); |
334 |
> |
} |
335 |
> |
} |
336 |
> |
|
337 |
> |
public void testAwaitUntil_Interrupt() { |
338 |
> |
final ReentrantLock lock = new ReentrantLock(); |
339 |
> |
final Condition c = lock.newCondition(); |
340 |
> |
Thread t = new Thread(new Runnable() { |
341 |
> |
public void run() { |
342 |
> |
try { |
343 |
> |
lock.lock(); |
344 |
> |
java.util.Date d = new java.util.Date(); |
345 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
346 |
> |
lock.unlock(); |
347 |
> |
fail("should throw"); |
348 |
> |
} |
349 |
> |
catch(InterruptedException success) { |
350 |
> |
} |
351 |
> |
} |
352 |
> |
}); |
353 |
> |
|
354 |
> |
try { |
355 |
> |
t.start(); |
356 |
> |
Thread.sleep(SHORT_DELAY_MS); |
357 |
> |
t.interrupt(); |
358 |
> |
t.join(SHORT_DELAY_MS); |
359 |
> |
assertFalse(t.isAlive()); |
360 |
> |
} |
361 |
> |
catch (Exception ex) { |
362 |
> |
fail("unexpected exception"); |
363 |
> |
} |
364 |
> |
} |
365 |
> |
|
366 |
> |
public void testSignalAll() { |
367 |
> |
final ReentrantLock lock = new ReentrantLock(); |
368 |
> |
final Condition c = lock.newCondition(); |
369 |
> |
Thread t1 = new Thread(new Runnable() { |
370 |
> |
public void run() { |
371 |
> |
try { |
372 |
> |
lock.lock(); |
373 |
> |
c.await(); |
374 |
> |
lock.unlock(); |
375 |
> |
} |
376 |
> |
catch(InterruptedException e) { |
377 |
> |
fail("unexpected exception"); |
378 |
> |
} |
379 |
> |
} |
380 |
> |
}); |
381 |
> |
|
382 |
> |
Thread t2 = new Thread(new Runnable() { |
383 |
> |
public void run() { |
384 |
> |
try { |
385 |
> |
lock.lock(); |
386 |
> |
c.await(); |
387 |
> |
lock.unlock(); |
388 |
> |
} |
389 |
> |
catch(InterruptedException e) { |
390 |
> |
fail("unexpected exception"); |
391 |
> |
} |
392 |
> |
} |
393 |
> |
}); |
394 |
> |
|
395 |
> |
try { |
396 |
> |
t1.start(); |
397 |
> |
t2.start(); |
398 |
> |
Thread.sleep(SHORT_DELAY_MS); |
399 |
> |
lock.lock(); |
400 |
> |
c.signalAll(); |
401 |
> |
lock.unlock(); |
402 |
> |
t1.join(SHORT_DELAY_MS); |
403 |
> |
t2.join(SHORT_DELAY_MS); |
404 |
> |
assertFalse(t1.isAlive()); |
405 |
> |
assertFalse(t2.isAlive()); |
406 |
> |
} |
407 |
> |
catch (Exception ex) { |
408 |
> |
fail("unexpected exception"); |
409 |
> |
} |
410 |
> |
} |
411 |
|
|
412 |
|
} |