8 |
|
import junit.framework.*; |
9 |
|
import java.util.concurrent.locks.*; |
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 |
|
|
23 |
– |
private static long SHORT_DELAY_MS = 100; |
24 |
– |
private static long MEDIUM_DELAY_MS = 1000; |
25 |
– |
private static long LONG_DELAY_MS = 10000; |
26 |
– |
|
24 |
|
/* |
25 |
|
* Unlocks an unlocked lock, throws Illegal Monitor State |
26 |
|
* |
27 |
|
*/ |
31 |
– |
|
28 |
|
public void testIllegalMonitorStateException(){ |
29 |
|
ReentrantLock rl = new ReentrantLock(); |
30 |
|
try{ |
31 |
|
rl.unlock(); |
32 |
|
fail("Should of thown Illegal Monitor State Exception"); |
33 |
|
|
34 |
< |
}catch(IllegalMonitorStateException sucess){} |
34 |
> |
} catch(IllegalMonitorStateException success){} |
35 |
|
|
36 |
|
|
37 |
|
} |
49 |
|
public void run(){ |
50 |
|
try{ |
51 |
|
lock.lockInterruptibly(); |
52 |
< |
fail("should throw"); |
53 |
< |
}catch(InterruptedException sucess){} |
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"); |
80 |
< |
}catch(InterruptedException sucess){} |
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 |
+ |
|
92 |
+ |
public void testTryLockWhenLocked() { |
93 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
94 |
+ |
lock.lock(); |
95 |
+ |
Thread t = new Thread(new Runnable() { |
96 |
+ |
public void run(){ |
97 |
+ |
threadAssertFalse(lock.tryLock()); |
98 |
+ |
} |
99 |
+ |
}); |
100 |
+ |
try { |
101 |
+ |
t.start(); |
102 |
+ |
t.join(); |
103 |
+ |
lock.unlock(); |
104 |
+ |
} catch(Exception e){ |
105 |
+ |
fail("unexpected exception"); |
106 |
+ |
} |
107 |
+ |
} |
108 |
+ |
|
109 |
+ |
public void testTryLock_Timeout(){ |
110 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
111 |
+ |
lock.lock(); |
112 |
+ |
Thread t = new Thread(new Runnable() { |
113 |
+ |
public void run(){ |
114 |
+ |
try { |
115 |
+ |
threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); |
116 |
+ |
} catch (Exception ex) { |
117 |
+ |
threadFail("unexpected exception"); |
118 |
+ |
} |
119 |
+ |
} |
120 |
+ |
}); |
121 |
+ |
try { |
122 |
+ |
t.start(); |
123 |
+ |
t.join(); |
124 |
+ |
lock.unlock(); |
125 |
+ |
} catch(Exception e){ |
126 |
+ |
fail("unexpected exception"); |
127 |
+ |
} |
128 |
+ |
} |
129 |
|
|
130 |
|
public void testGetHoldCount() { |
131 |
|
ReentrantLock lock = new ReentrantLock(); |
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 |
|
}); |
169 |
|
} catch(Exception e){ |
170 |
|
fail("unexpected exception"); |
171 |
|
} |
127 |
– |
|
172 |
|
} |
173 |
|
|
130 |
– |
/* |
131 |
– |
* current thread locks interruptibly the thread |
132 |
– |
* another thread tries to aquire the lock and blocks |
133 |
– |
* on the call. interrupt the attempted aquireLock |
134 |
– |
* assert that the first lock() call actually locked the lock |
135 |
– |
* assert that the current thread is the one holding the lock |
136 |
– |
*/ |
174 |
|
|
175 |
< |
public void testLockedInterruptibly() { |
175 |
> |
public void testLockInterruptibly() { |
176 |
|
final ReentrantLock lock = new ReentrantLock(); |
177 |
< |
try {lock.lockInterruptibly();} catch(Exception e) {} |
177 |
> |
try { |
178 |
> |
lock.lockInterruptibly(); |
179 |
> |
} catch(Exception e) { |
180 |
> |
fail("unexpected exception"); |
181 |
> |
} |
182 |
|
Thread t = new Thread(new Runnable() { |
183 |
|
public void run() { |
184 |
|
try { |
185 |
|
lock.lockInterruptibly(); |
186 |
< |
fail("Failed to generate an Interrupted Exception"); |
186 |
> |
threadFail("should throw"); |
187 |
|
} |
188 |
|
catch(InterruptedException e) {} |
189 |
|
} |
190 |
|
}); |
191 |
< |
t.start(); |
192 |
< |
t.interrupt(); |
193 |
< |
assertTrue(lock.isLocked()); |
194 |
< |
assertTrue(lock.isHeldByCurrentThread()); |
191 |
> |
try { |
192 |
> |
t.start(); |
193 |
> |
t.interrupt(); |
194 |
> |
assertTrue(lock.isLocked()); |
195 |
> |
assertTrue(lock.isHeldByCurrentThread()); |
196 |
> |
t.join(); |
197 |
> |
} catch(Exception e){ |
198 |
> |
fail("unexpected exception"); |
199 |
> |
} |
200 |
|
} |
201 |
|
|
202 |
|
public void testAwait_IllegalMonitor() { |
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 |
|
}); |
454 |
|
fail("unexpected exception"); |
455 |
|
} |
456 |
|
} |
457 |
+ |
|
458 |
+ |
public void testSerialization() { |
459 |
+ |
ReentrantLock l = new ReentrantLock(); |
460 |
+ |
l.lock(); |
461 |
+ |
l.unlock(); |
462 |
+ |
|
463 |
+ |
try { |
464 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
465 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
466 |
+ |
out.writeObject(l); |
467 |
+ |
out.close(); |
468 |
+ |
|
469 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
470 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
471 |
+ |
ReentrantLock r = (ReentrantLock) in.readObject(); |
472 |
+ |
r.lock(); |
473 |
+ |
r.unlock(); |
474 |
+ |
} catch(Exception e){ |
475 |
+ |
e.printStackTrace(); |
476 |
+ |
fail("unexpected exception"); |
477 |
+ |
} |
478 |
+ |
} |
479 |
|
|
480 |
|
} |