10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
< |
public class ReentrantLockTest extends TestCase { |
13 |
> |
public class ReentrantLockTest extends JSR166TestCase { |
14 |
|
static int HOLD_COUNT_TEST_LIMIT = 20; |
15 |
|
|
16 |
|
public static void main(String[] args) { |
21 |
|
return new TestSuite(ReentrantLockTest.class); |
22 |
|
} |
23 |
|
|
24 |
– |
private static long SHORT_DELAY_MS = 100; |
25 |
– |
private static long MEDIUM_DELAY_MS = 1000; |
26 |
– |
private static long LONG_DELAY_MS = 10000; |
27 |
– |
|
24 |
|
/* |
25 |
|
* Unlocks an unlocked lock, throws Illegal Monitor State |
26 |
|
* |
27 |
|
*/ |
32 |
– |
|
28 |
|
public void testIllegalMonitorStateException(){ |
29 |
|
ReentrantLock rl = new ReentrantLock(); |
30 |
|
try{ |
49 |
|
public void run(){ |
50 |
|
try{ |
51 |
|
lock.lockInterruptibly(); |
52 |
< |
fail("should throw"); |
52 |
> |
threadFail("should throw"); |
53 |
|
} catch(InterruptedException success){} |
54 |
|
} |
55 |
|
}); |
56 |
< |
t.start(); |
57 |
< |
t.interrupt(); |
58 |
< |
lock.unlock(); |
56 |
> |
try { |
57 |
> |
t.start(); |
58 |
> |
t.interrupt(); |
59 |
> |
lock.unlock(); |
60 |
> |
t.join(); |
61 |
> |
} catch(Exception e){ |
62 |
> |
fail("unexpected exception"); |
63 |
> |
} |
64 |
|
} |
65 |
|
|
66 |
|
/* |
75 |
|
Thread t = new Thread(new Runnable() { |
76 |
|
public void run(){ |
77 |
|
try{ |
78 |
< |
lock.tryLock(1000,TimeUnit.MILLISECONDS); |
79 |
< |
fail("should throw"); |
78 |
> |
lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS); |
79 |
> |
threadFail("should throw"); |
80 |
|
} catch(InterruptedException success){} |
81 |
|
} |
82 |
|
}); |
83 |
< |
t.start(); |
84 |
< |
t.interrupt(); |
83 |
> |
try { |
84 |
> |
t.start(); |
85 |
> |
t.interrupt(); |
86 |
> |
} catch(Exception e){ |
87 |
> |
fail("unexpected exception"); |
88 |
> |
} |
89 |
|
} |
90 |
|
|
91 |
|
|
94 |
|
lock.lock(); |
95 |
|
Thread t = new Thread(new Runnable() { |
96 |
|
public void run(){ |
97 |
< |
assertFalse(lock.tryLock()); |
97 |
> |
threadAssertFalse(lock.tryLock()); |
98 |
|
} |
99 |
|
}); |
100 |
|
try { |
112 |
|
Thread t = new Thread(new Runnable() { |
113 |
|
public void run(){ |
114 |
|
try { |
115 |
< |
assertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); |
115 |
> |
threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); |
116 |
|
} catch (Exception ex) { |
117 |
< |
fail("unexpected exception"); |
117 |
> |
threadFail("unexpected exception"); |
118 |
|
} |
119 |
|
} |
120 |
|
}); |
152 |
|
public void run() { |
153 |
|
lock.lock(); |
154 |
|
try { |
155 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
155 |
> |
Thread.sleep(SMALL_DELAY_MS); |
156 |
|
} |
157 |
< |
catch(Exception e) {} |
157 |
> |
catch(Exception e) { |
158 |
> |
threadFail("unexpected exception"); |
159 |
> |
} |
160 |
|
lock.unlock(); |
161 |
|
} |
162 |
|
}); |
183 |
|
public void run() { |
184 |
|
try { |
185 |
|
lock.lockInterruptibly(); |
186 |
< |
fail("should throw"); |
186 |
> |
threadFail("should throw"); |
187 |
|
} |
188 |
|
catch(InterruptedException e) {} |
189 |
|
} |
246 |
|
final Condition c = lock.newCondition(); |
247 |
|
try { |
248 |
|
lock.lock(); |
249 |
< |
assertFalse(c.await(10, TimeUnit.MILLISECONDS)); |
249 |
> |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
250 |
|
lock.unlock(); |
251 |
|
} |
252 |
|
catch (Exception ex) { |
279 |
|
lock.unlock(); |
280 |
|
} |
281 |
|
catch(InterruptedException e) { |
282 |
< |
fail("unexpected exception"); |
282 |
> |
threadFail("unexpected exception"); |
283 |
|
} |
284 |
|
} |
285 |
|
}); |
333 |
|
lock.lock(); |
334 |
|
c.await(); |
335 |
|
lock.unlock(); |
336 |
< |
fail("should throw"); |
336 |
> |
threadFail("should throw"); |
337 |
|
} |
338 |
|
catch(InterruptedException success) { |
339 |
|
} |
361 |
|
lock.lock(); |
362 |
|
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
363 |
|
lock.unlock(); |
364 |
< |
fail("should throw"); |
364 |
> |
threadFail("should throw"); |
365 |
|
} |
366 |
|
catch(InterruptedException success) { |
367 |
|
} |
390 |
|
java.util.Date d = new java.util.Date(); |
391 |
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
392 |
|
lock.unlock(); |
393 |
< |
fail("should throw"); |
393 |
> |
threadFail("should throw"); |
394 |
|
} |
395 |
|
catch(InterruptedException success) { |
396 |
|
} |
420 |
|
lock.unlock(); |
421 |
|
} |
422 |
|
catch(InterruptedException e) { |
423 |
< |
fail("unexpected exception"); |
423 |
> |
threadFail("unexpected exception"); |
424 |
|
} |
425 |
|
} |
426 |
|
}); |
433 |
|
lock.unlock(); |
434 |
|
} |
435 |
|
catch(InterruptedException e) { |
436 |
< |
fail("unexpected exception"); |
436 |
> |
threadFail("unexpected exception"); |
437 |
|
} |
438 |
|
} |
439 |
|
}); |