10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
< |
public class ReentrantReadWriteLockTest extends TestCase { |
14 |
< |
static int HOLD_COUNT_TEST_LIMIT = 20; |
15 |
< |
|
13 |
> |
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
|
junit.textui.TestRunner.run (suite()); |
16 |
|
} |
19 |
– |
|
17 |
|
public static Test suite() { |
18 |
|
return new TestSuite(ReentrantReadWriteLockTest.class); |
19 |
|
} |
20 |
|
|
21 |
|
|
25 |
– |
private static long SHORT_DELAY_MS = 100; |
26 |
– |
private static long MEDIUM_DELAY_MS = 1000; |
27 |
– |
private static long LONG_DELAY_MS = 10000; |
28 |
– |
|
22 |
|
/* |
23 |
< |
* Unlocks an unlocked lock, throws Illegal Monitor State |
31 |
< |
* |
23 |
> |
* write-locking and read-locking an unlocked lock succeed |
24 |
|
*/ |
25 |
< |
|
34 |
< |
public void testIllegalMonitorStateException(){ |
25 |
> |
public void testLock() { |
26 |
|
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
27 |
< |
try{ |
28 |
< |
rl.writeLock().unlock(); |
29 |
< |
fail("Should of thown Illegal Monitor State Exception"); |
27 |
> |
rl.writeLock().lock(); |
28 |
> |
rl.writeLock().unlock(); |
29 |
> |
rl.readLock().lock(); |
30 |
> |
rl.readLock().unlock(); |
31 |
> |
} |
32 |
|
|
33 |
< |
}catch(IllegalMonitorStateException success){} |
33 |
> |
|
34 |
> |
/* |
35 |
> |
* locking an unlocked fair lock succeeds |
36 |
> |
*/ |
37 |
> |
public void testFairLock() { |
38 |
> |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true); |
39 |
> |
rl.writeLock().lock(); |
40 |
> |
rl.writeLock().unlock(); |
41 |
> |
rl.readLock().lock(); |
42 |
> |
rl.readLock().unlock(); |
43 |
|
} |
44 |
|
|
45 |
+ |
/** |
46 |
+ |
* write-unlocking an unlocked lock throws IllegalMonitorStateException |
47 |
+ |
*/ |
48 |
+ |
public void testUnlock_IllegalMonitorStateException() { |
49 |
+ |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
50 |
+ |
try { |
51 |
+ |
rl.writeLock().unlock(); |
52 |
+ |
shouldThrow(); |
53 |
+ |
} catch(IllegalMonitorStateException success){} |
54 |
+ |
} |
55 |
|
|
56 |
|
|
57 |
< |
public void testInterruptedException(){ |
57 |
> |
/** |
58 |
> |
* write-lockInterruptibly is interruptible |
59 |
> |
*/ |
60 |
> |
public void testWriteLockInterruptibly_Interrupted() { |
61 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
62 |
|
lock.writeLock().lock(); |
63 |
|
Thread t = new Thread(new Runnable() { |
64 |
< |
public void run(){ |
65 |
< |
try{ |
64 |
> |
public void run() { |
65 |
> |
try { |
66 |
|
lock.writeLock().lockInterruptibly(); |
67 |
< |
fail("should throw"); |
68 |
< |
}catch(InterruptedException success){} |
67 |
> |
threadShouldThrow(); |
68 |
> |
} catch(InterruptedException success){} |
69 |
|
} |
70 |
|
}); |
71 |
|
try { |
74 |
|
lock.writeLock().unlock(); |
75 |
|
t.join(); |
76 |
|
} catch(Exception e){ |
77 |
< |
fail("unexpected exception"); |
77 |
> |
unexpectedException(); |
78 |
|
} |
79 |
|
} |
80 |
|
|
81 |
< |
public void testInterruptedException2(){ |
81 |
> |
/** |
82 |
> |
* timed write-trylock is interruptible |
83 |
> |
*/ |
84 |
> |
public void testWriteTryLock_Interrupted() { |
85 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
86 |
|
lock.writeLock().lock(); |
87 |
|
Thread t = new Thread(new Runnable() { |
88 |
< |
public void run(){ |
89 |
< |
try{ |
88 |
> |
public void run() { |
89 |
> |
try { |
90 |
|
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
91 |
< |
fail("should throw"); |
92 |
< |
}catch(InterruptedException success){} |
91 |
> |
threadShouldThrow(); |
92 |
> |
} catch(InterruptedException success){} |
93 |
|
} |
94 |
|
}); |
95 |
|
try { |
98 |
|
lock.writeLock().unlock(); |
99 |
|
t.join(); |
100 |
|
} catch(Exception e){ |
101 |
< |
fail("unexpected exception"); |
101 |
> |
unexpectedException(); |
102 |
|
} |
103 |
|
} |
104 |
|
|
105 |
< |
public void testInterruptedException3(){ |
105 |
> |
/** |
106 |
> |
* read-lockInterruptibly is interruptible |
107 |
> |
*/ |
108 |
> |
public void testReadLockInterruptibly_Interrupted() { |
109 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
110 |
|
lock.writeLock().lock(); |
111 |
|
Thread t = new Thread(new Runnable() { |
112 |
< |
public void run(){ |
113 |
< |
try{ |
112 |
> |
public void run() { |
113 |
> |
try { |
114 |
|
lock.readLock().lockInterruptibly(); |
115 |
< |
fail("should throw"); |
116 |
< |
}catch(InterruptedException success){} |
115 |
> |
threadShouldThrow(); |
116 |
> |
} catch(InterruptedException success){} |
117 |
|
} |
118 |
|
}); |
119 |
|
try { |
122 |
|
lock.writeLock().unlock(); |
123 |
|
t.join(); |
124 |
|
} catch(Exception e){ |
125 |
< |
fail("unexpected exception"); |
125 |
> |
unexpectedException(); |
126 |
|
} |
127 |
|
} |
128 |
|
|
129 |
< |
public void testInterruptedException4(){ |
129 |
> |
/** |
130 |
> |
* timed read-trylock is interruptible |
131 |
> |
*/ |
132 |
> |
public void testReadTryLock_Interrupted() { |
133 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
134 |
|
lock.writeLock().lock(); |
135 |
|
Thread t = new Thread(new Runnable() { |
136 |
< |
public void run(){ |
137 |
< |
try{ |
136 |
> |
public void run() { |
137 |
> |
try { |
138 |
|
lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS); |
139 |
< |
fail("should throw"); |
140 |
< |
}catch(InterruptedException success){} |
139 |
> |
threadShouldThrow(); |
140 |
> |
} catch(InterruptedException success){} |
141 |
|
} |
142 |
|
}); |
143 |
|
try { |
145 |
|
t.interrupt(); |
146 |
|
t.join(); |
147 |
|
} catch(Exception e){ |
148 |
< |
fail("unexpected exception"); |
148 |
> |
unexpectedException(); |
149 |
|
} |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
< |
public void testTryLockWhenLocked() { |
153 |
> |
/** |
154 |
> |
* write-trylock fails if locked |
155 |
> |
*/ |
156 |
> |
public void testWriteTryLockWhenLocked() { |
157 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
158 |
|
lock.writeLock().lock(); |
159 |
|
Thread t = new Thread(new Runnable() { |
160 |
< |
public void run(){ |
161 |
< |
assertFalse(lock.writeLock().tryLock()); |
160 |
> |
public void run() { |
161 |
> |
threadAssertFalse(lock.writeLock().tryLock()); |
162 |
|
} |
163 |
|
}); |
164 |
|
try { |
166 |
|
t.join(); |
167 |
|
lock.writeLock().unlock(); |
168 |
|
} catch(Exception e){ |
169 |
< |
fail("unexpected exception"); |
169 |
> |
unexpectedException(); |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
< |
public void testTryLockWhenLocked2() { |
173 |
> |
/** |
174 |
> |
* read-trylock fails if locked |
175 |
> |
*/ |
176 |
> |
public void testReadTryLockWhenLocked() { |
177 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
178 |
|
lock.writeLock().lock(); |
179 |
|
Thread t = new Thread(new Runnable() { |
180 |
< |
public void run(){ |
181 |
< |
assertFalse(lock.readLock().tryLock()); |
180 |
> |
public void run() { |
181 |
> |
threadAssertFalse(lock.readLock().tryLock()); |
182 |
|
} |
183 |
|
}); |
184 |
|
try { |
186 |
|
t.join(); |
187 |
|
lock.writeLock().unlock(); |
188 |
|
} catch(Exception e){ |
189 |
< |
fail("unexpected exception"); |
189 |
> |
unexpectedException(); |
190 |
|
} |
191 |
|
} |
192 |
|
|
193 |
+ |
/** |
194 |
+ |
* Multiple threads can hold a read lock when not write-locked |
195 |
+ |
*/ |
196 |
|
public void testMultipleReadLocks() { |
197 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
198 |
|
lock.readLock().lock(); |
199 |
|
Thread t = new Thread(new Runnable() { |
200 |
< |
public void run(){ |
201 |
< |
assertTrue(lock.readLock().tryLock()); |
200 |
> |
public void run() { |
201 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
202 |
|
lock.readLock().unlock(); |
203 |
|
} |
204 |
|
}); |
207 |
|
t.join(); |
208 |
|
lock.readLock().unlock(); |
209 |
|
} catch(Exception e){ |
210 |
< |
fail("unexpected exception"); |
210 |
> |
unexpectedException(); |
211 |
|
} |
212 |
|
} |
213 |
|
|
214 |
+ |
/** |
215 |
+ |
* A writelock succeeds after reading threads unlock |
216 |
+ |
*/ |
217 |
|
public void testWriteAfterMultipleReadLocks() { |
218 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
219 |
|
lock.readLock().lock(); |
220 |
|
Thread t1 = new Thread(new Runnable() { |
221 |
< |
public void run(){ |
221 |
> |
public void run() { |
222 |
|
lock.readLock().lock(); |
223 |
|
lock.readLock().unlock(); |
224 |
|
} |
225 |
|
}); |
226 |
|
Thread t2 = new Thread(new Runnable() { |
227 |
< |
public void run(){ |
227 |
> |
public void run() { |
228 |
|
lock.writeLock().lock(); |
229 |
|
lock.writeLock().unlock(); |
230 |
|
} |
241 |
|
assertTrue(!t2.isAlive()); |
242 |
|
|
243 |
|
} catch(Exception e){ |
244 |
< |
fail("unexpected exception"); |
244 |
> |
unexpectedException(); |
245 |
|
} |
246 |
|
} |
247 |
|
|
248 |
+ |
/** |
249 |
+ |
* Readlocks succeed after a writing thread unlocks |
250 |
+ |
*/ |
251 |
|
public void testReadAfterWriteLock() { |
252 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
253 |
|
lock.writeLock().lock(); |
254 |
|
Thread t1 = new Thread(new Runnable() { |
255 |
< |
public void run(){ |
255 |
> |
public void run() { |
256 |
|
lock.readLock().lock(); |
257 |
|
lock.readLock().unlock(); |
258 |
|
} |
259 |
|
}); |
260 |
|
Thread t2 = new Thread(new Runnable() { |
261 |
< |
public void run(){ |
261 |
> |
public void run() { |
262 |
|
lock.readLock().lock(); |
263 |
|
lock.readLock().unlock(); |
264 |
|
} |
275 |
|
assertTrue(!t2.isAlive()); |
276 |
|
|
277 |
|
} catch(Exception e){ |
278 |
< |
fail("unexpected exception"); |
278 |
> |
unexpectedException(); |
279 |
|
} |
280 |
|
} |
281 |
|
|
282 |
|
|
283 |
+ |
/** |
284 |
+ |
* Read trylock succeeds if readlocked but not writelocked |
285 |
+ |
*/ |
286 |
|
public void testTryLockWhenReadLocked() { |
287 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
288 |
|
lock.readLock().lock(); |
289 |
|
Thread t = new Thread(new Runnable() { |
290 |
< |
public void run(){ |
291 |
< |
assertTrue(lock.readLock().tryLock()); |
290 |
> |
public void run() { |
291 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
292 |
|
lock.readLock().unlock(); |
293 |
|
} |
294 |
|
}); |
297 |
|
t.join(); |
298 |
|
lock.readLock().unlock(); |
299 |
|
} catch(Exception e){ |
300 |
< |
fail("unexpected exception"); |
300 |
> |
unexpectedException(); |
301 |
|
} |
302 |
|
} |
303 |
|
|
304 |
|
|
305 |
|
|
306 |
+ |
/** |
307 |
+ |
* write trylock fails when readlocked |
308 |
+ |
*/ |
309 |
|
public void testWriteTryLockWhenReadLocked() { |
310 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
311 |
|
lock.readLock().lock(); |
312 |
|
Thread t = new Thread(new Runnable() { |
313 |
< |
public void run(){ |
314 |
< |
assertFalse(lock.writeLock().tryLock()); |
313 |
> |
public void run() { |
314 |
> |
threadAssertFalse(lock.writeLock().tryLock()); |
315 |
|
} |
316 |
|
}); |
317 |
|
try { |
319 |
|
t.join(); |
320 |
|
lock.readLock().unlock(); |
321 |
|
} catch(Exception e){ |
322 |
< |
fail("unexpected exception"); |
322 |
> |
unexpectedException(); |
323 |
|
} |
324 |
|
} |
325 |
|
|
326 |
|
|
327 |
|
|
328 |
< |
public void testTryLock_Timeout(){ |
328 |
> |
/** |
329 |
> |
* write timed trylock times out if locked |
330 |
> |
*/ |
331 |
> |
public void testWriteTryLock_Timeout() { |
332 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
333 |
|
lock.writeLock().lock(); |
334 |
|
Thread t = new Thread(new Runnable() { |
335 |
< |
public void run(){ |
335 |
> |
public void run() { |
336 |
|
try { |
337 |
< |
assertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); |
337 |
> |
threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); |
338 |
|
} catch (Exception ex) { |
339 |
< |
fail("unexpected exception"); |
339 |
> |
threadUnexpectedException(); |
340 |
|
} |
341 |
|
} |
342 |
|
}); |
345 |
|
t.join(); |
346 |
|
lock.writeLock().unlock(); |
347 |
|
} catch(Exception e){ |
348 |
< |
fail("unexpected exception"); |
348 |
> |
unexpectedException(); |
349 |
|
} |
350 |
|
} |
351 |
|
|
352 |
< |
public void testTryLock_Timeout2(){ |
352 |
> |
/** |
353 |
> |
* read timed trylock times out if write-locked |
354 |
> |
*/ |
355 |
> |
public void testReadTryLock_Timeout() { |
356 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
357 |
|
lock.writeLock().lock(); |
358 |
|
Thread t = new Thread(new Runnable() { |
359 |
< |
public void run(){ |
359 |
> |
public void run() { |
360 |
|
try { |
361 |
< |
assertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); |
361 |
> |
threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); |
362 |
|
} catch (Exception ex) { |
363 |
< |
fail("unexpected exception"); |
363 |
> |
threadUnexpectedException(); |
364 |
|
} |
365 |
|
} |
366 |
|
}); |
369 |
|
t.join(); |
370 |
|
lock.writeLock().unlock(); |
371 |
|
} catch(Exception e){ |
372 |
< |
fail("unexpected exception"); |
372 |
> |
unexpectedException(); |
373 |
|
} |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
< |
public void testLockInterruptibly() { |
377 |
> |
/** |
378 |
> |
* write lockInterruptibly succeeds if lock free else is interruptible |
379 |
> |
*/ |
380 |
> |
public void testWriteLockInterruptibly() { |
381 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
382 |
|
try { |
383 |
|
lock.writeLock().lockInterruptibly(); |
384 |
|
} catch(Exception e) { |
385 |
< |
fail("unexpected exception"); |
385 |
> |
unexpectedException(); |
386 |
|
} |
387 |
|
Thread t = new Thread(new Runnable() { |
388 |
|
public void run() { |
389 |
|
try { |
390 |
|
lock.writeLock().lockInterruptibly(); |
391 |
< |
fail("should throw"); |
391 |
> |
threadShouldThrow(); |
392 |
|
} |
393 |
|
catch(InterruptedException success) { |
394 |
|
} |
400 |
|
t.join(); |
401 |
|
lock.writeLock().unlock(); |
402 |
|
} catch(Exception e){ |
403 |
< |
fail("unexpected exception"); |
403 |
> |
unexpectedException(); |
404 |
|
} |
405 |
|
} |
406 |
|
|
407 |
< |
public void testLockInterruptibly2() { |
407 |
> |
/** |
408 |
> |
* read lockInterruptibly succeeds if lock free else is interruptible |
409 |
> |
*/ |
410 |
> |
public void testReadLockInterruptibly() { |
411 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
412 |
|
try { |
413 |
|
lock.writeLock().lockInterruptibly(); |
414 |
|
} catch(Exception e) { |
415 |
< |
fail("unexpected exception"); |
415 |
> |
unexpectedException(); |
416 |
|
} |
417 |
|
Thread t = new Thread(new Runnable() { |
418 |
|
public void run() { |
419 |
|
try { |
420 |
|
lock.readLock().lockInterruptibly(); |
421 |
< |
fail("should throw"); |
421 |
> |
threadShouldThrow(); |
422 |
|
} |
423 |
|
catch(InterruptedException success) { |
424 |
|
} |
430 |
|
t.join(); |
431 |
|
lock.writeLock().unlock(); |
432 |
|
} catch(Exception e){ |
433 |
< |
fail("unexpected exception"); |
433 |
> |
unexpectedException(); |
434 |
|
} |
435 |
|
} |
436 |
|
|
437 |
+ |
/** |
438 |
+ |
* Calling await without holding lock throws IllegalMonitorStateException |
439 |
+ |
*/ |
440 |
|
public void testAwait_IllegalMonitor() { |
441 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
442 |
|
final Condition c = lock.writeLock().newCondition(); |
443 |
|
try { |
444 |
|
c.await(); |
445 |
< |
fail("should throw"); |
445 |
> |
shouldThrow(); |
446 |
|
} |
447 |
|
catch (IllegalMonitorStateException success) { |
448 |
|
} |
449 |
|
catch (Exception ex) { |
450 |
< |
fail("should throw IMSE"); |
450 |
> |
shouldThrow(); |
451 |
|
} |
452 |
|
} |
453 |
|
|
454 |
+ |
/** |
455 |
+ |
* Calling signal without holding lock throws IllegalMonitorStateException |
456 |
+ |
*/ |
457 |
|
public void testSignal_IllegalMonitor() { |
458 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
459 |
|
final Condition c = lock.writeLock().newCondition(); |
460 |
|
try { |
461 |
|
c.signal(); |
462 |
< |
fail("should throw"); |
462 |
> |
shouldThrow(); |
463 |
|
} |
464 |
|
catch (IllegalMonitorStateException success) { |
465 |
|
} |
466 |
|
catch (Exception ex) { |
467 |
< |
fail("should throw IMSE"); |
467 |
> |
unexpectedException(); |
468 |
|
} |
469 |
|
} |
470 |
|
|
471 |
+ |
/** |
472 |
+ |
* awaitNanos without a signal times out |
473 |
+ |
*/ |
474 |
|
public void testAwaitNanos_Timeout() { |
475 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
476 |
|
final Condition c = lock.writeLock().newCondition(); |
481 |
|
lock.writeLock().unlock(); |
482 |
|
} |
483 |
|
catch (Exception ex) { |
484 |
< |
fail("unexpected exception"); |
484 |
> |
unexpectedException(); |
485 |
|
} |
486 |
|
} |
487 |
|
|
488 |
+ |
|
489 |
+ |
/** |
490 |
+ |
* timed await without a signal times out |
491 |
+ |
*/ |
492 |
|
public void testAwait_Timeout() { |
493 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
494 |
|
final Condition c = lock.writeLock().newCondition(); |
498 |
|
lock.writeLock().unlock(); |
499 |
|
} |
500 |
|
catch (Exception ex) { |
501 |
< |
fail("unexpected exception"); |
501 |
> |
unexpectedException(); |
502 |
|
} |
503 |
|
} |
504 |
|
|
505 |
+ |
/** |
506 |
+ |
* awaitUntil without a signal times out |
507 |
+ |
*/ |
508 |
|
public void testAwaitUntil_Timeout() { |
509 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
510 |
|
final Condition c = lock.writeLock().newCondition(); |
515 |
|
lock.writeLock().unlock(); |
516 |
|
} |
517 |
|
catch (Exception ex) { |
518 |
< |
fail("unexpected exception"); |
518 |
> |
unexpectedException(); |
519 |
|
} |
520 |
|
} |
521 |
|
|
522 |
+ |
/** |
523 |
+ |
* await returns when signalled |
524 |
+ |
*/ |
525 |
|
public void testAwait() { |
526 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
527 |
|
final Condition c = lock.writeLock().newCondition(); |
533 |
|
lock.writeLock().unlock(); |
534 |
|
} |
535 |
|
catch(InterruptedException e) { |
536 |
< |
fail("unexpected exception"); |
536 |
> |
threadUnexpectedException(); |
537 |
|
} |
538 |
|
} |
539 |
|
}); |
548 |
|
assertFalse(t.isAlive()); |
549 |
|
} |
550 |
|
catch (Exception ex) { |
551 |
< |
fail("unexpected exception"); |
551 |
> |
unexpectedException(); |
552 |
|
} |
553 |
|
} |
554 |
|
|
555 |
+ |
/** |
556 |
+ |
* awaitUninterruptibly doesn't abort on interrupt |
557 |
+ |
*/ |
558 |
|
public void testAwaitUninterruptibly() { |
559 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
560 |
|
final Condition c = lock.writeLock().newCondition(); |
573 |
|
lock.writeLock().lock(); |
574 |
|
c.signal(); |
575 |
|
lock.writeLock().unlock(); |
576 |
+ |
assert(t.isInterrupted()); |
577 |
|
t.join(SHORT_DELAY_MS); |
578 |
|
assertFalse(t.isAlive()); |
579 |
|
} |
580 |
|
catch (Exception ex) { |
581 |
< |
fail("unexpected exception"); |
581 |
> |
unexpectedException(); |
582 |
|
} |
583 |
|
} |
584 |
|
|
585 |
+ |
/** |
586 |
+ |
* await is interruptible |
587 |
+ |
*/ |
588 |
|
public void testAwait_Interrupt() { |
589 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
590 |
|
final Condition c = lock.writeLock().newCondition(); |
594 |
|
lock.writeLock().lock(); |
595 |
|
c.await(); |
596 |
|
lock.writeLock().unlock(); |
597 |
< |
fail("should throw"); |
597 |
> |
threadShouldThrow(); |
598 |
|
} |
599 |
|
catch(InterruptedException success) { |
600 |
|
} |
609 |
|
assertFalse(t.isAlive()); |
610 |
|
} |
611 |
|
catch (Exception ex) { |
612 |
< |
fail("unexpected exception"); |
612 |
> |
unexpectedException(); |
613 |
|
} |
614 |
|
} |
615 |
|
|
616 |
+ |
/** |
617 |
+ |
* awaitNanos is interruptible |
618 |
+ |
*/ |
619 |
|
public void testAwaitNanos_Interrupt() { |
620 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
621 |
|
final Condition c = lock.writeLock().newCondition(); |
625 |
|
lock.writeLock().lock(); |
626 |
|
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
627 |
|
lock.writeLock().unlock(); |
628 |
< |
fail("should throw"); |
628 |
> |
threadShouldThrow(); |
629 |
|
} |
630 |
|
catch(InterruptedException success) { |
631 |
|
} |
640 |
|
assertFalse(t.isAlive()); |
641 |
|
} |
642 |
|
catch (Exception ex) { |
643 |
< |
fail("unexpected exception"); |
643 |
> |
unexpectedException(); |
644 |
|
} |
645 |
|
} |
646 |
|
|
647 |
+ |
/** |
648 |
+ |
* awaitUntil is interruptible |
649 |
+ |
*/ |
650 |
|
public void testAwaitUntil_Interrupt() { |
651 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
652 |
|
final Condition c = lock.writeLock().newCondition(); |
657 |
|
java.util.Date d = new java.util.Date(); |
658 |
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
659 |
|
lock.writeLock().unlock(); |
660 |
< |
fail("should throw"); |
660 |
> |
threadShouldThrow(); |
661 |
|
} |
662 |
|
catch(InterruptedException success) { |
663 |
|
} |
672 |
|
assertFalse(t.isAlive()); |
673 |
|
} |
674 |
|
catch (Exception ex) { |
675 |
< |
fail("unexpected exception"); |
675 |
> |
unexpectedException(); |
676 |
|
} |
677 |
|
} |
678 |
|
|
679 |
+ |
/** |
680 |
+ |
* signalAll wakes up all threads |
681 |
+ |
*/ |
682 |
|
public void testSignalAll() { |
683 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
684 |
|
final Condition c = lock.writeLock().newCondition(); |
690 |
|
lock.writeLock().unlock(); |
691 |
|
} |
692 |
|
catch(InterruptedException e) { |
693 |
< |
fail("unexpected exception"); |
693 |
> |
threadUnexpectedException(); |
694 |
|
} |
695 |
|
} |
696 |
|
}); |
703 |
|
lock.writeLock().unlock(); |
704 |
|
} |
705 |
|
catch(InterruptedException e) { |
706 |
< |
fail("unexpected exception"); |
706 |
> |
threadUnexpectedException(); |
707 |
|
} |
708 |
|
} |
709 |
|
}); |
721 |
|
assertFalse(t2.isAlive()); |
722 |
|
} |
723 |
|
catch (Exception ex) { |
724 |
< |
fail("unexpected exception"); |
724 |
> |
unexpectedException(); |
725 |
|
} |
726 |
|
} |
727 |
|
|
728 |
+ |
/** |
729 |
+ |
* A serialized lock deserializes as unlocked |
730 |
+ |
*/ |
731 |
|
public void testSerialization() { |
732 |
|
ReentrantReadWriteLock l = new ReentrantReadWriteLock(); |
733 |
|
l.readLock().lock(); |
746 |
|
r.readLock().unlock(); |
747 |
|
} catch(Exception e){ |
748 |
|
e.printStackTrace(); |
749 |
< |
fail("unexpected exception"); |
749 |
> |
unexpectedException(); |
750 |
|
} |
751 |
|
} |
752 |
|
|