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 { |
14 |
|
static int HOLD_COUNT_TEST_LIMIT = 20; |
36 |
|
rl.unlock(); |
37 |
|
fail("Should of thown Illegal Monitor State Exception"); |
38 |
|
|
39 |
< |
}catch(IllegalMonitorStateException sucess){} |
39 |
> |
} catch(IllegalMonitorStateException success){} |
40 |
|
|
41 |
|
|
42 |
|
} |
55 |
|
try{ |
56 |
|
lock.lockInterruptibly(); |
57 |
|
fail("should throw"); |
58 |
< |
}catch(InterruptedException sucess){} |
58 |
> |
} catch(InterruptedException success){} |
59 |
|
} |
60 |
|
}); |
61 |
|
t.start(); |
77 |
|
try{ |
78 |
|
lock.tryLock(1000,TimeUnit.MILLISECONDS); |
79 |
|
fail("should throw"); |
80 |
< |
}catch(InterruptedException sucess){} |
80 |
> |
} catch(InterruptedException success){} |
81 |
|
} |
82 |
|
}); |
83 |
|
t.start(); |
84 |
|
t.interrupt(); |
85 |
|
} |
86 |
|
|
87 |
+ |
|
88 |
+ |
public void testTryLockWhenLocked() { |
89 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
90 |
+ |
lock.lock(); |
91 |
+ |
Thread t = new Thread(new Runnable() { |
92 |
+ |
public void run(){ |
93 |
+ |
assertFalse(lock.tryLock()); |
94 |
+ |
} |
95 |
+ |
}); |
96 |
+ |
try { |
97 |
+ |
t.start(); |
98 |
+ |
t.join(); |
99 |
+ |
lock.unlock(); |
100 |
+ |
} catch(Exception e){ |
101 |
+ |
fail("unexpected exception"); |
102 |
+ |
} |
103 |
+ |
} |
104 |
+ |
|
105 |
+ |
public void testTryLock_Timeout(){ |
106 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
107 |
+ |
lock.lock(); |
108 |
+ |
Thread t = new Thread(new Runnable() { |
109 |
+ |
public void run(){ |
110 |
+ |
try { |
111 |
+ |
assertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); |
112 |
+ |
} catch (Exception ex) { |
113 |
+ |
fail("unexpected exception"); |
114 |
+ |
} |
115 |
+ |
} |
116 |
+ |
}); |
117 |
+ |
try { |
118 |
+ |
t.start(); |
119 |
+ |
t.join(); |
120 |
+ |
lock.unlock(); |
121 |
+ |
} catch(Exception e){ |
122 |
+ |
fail("unexpected exception"); |
123 |
+ |
} |
124 |
+ |
} |
125 |
|
|
126 |
|
public void testGetHoldCount() { |
127 |
|
ReentrantLock lock = new ReentrantLock(); |
163 |
|
} catch(Exception e){ |
164 |
|
fail("unexpected exception"); |
165 |
|
} |
127 |
– |
|
166 |
|
} |
167 |
|
|
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 |
– |
*/ |
168 |
|
|
169 |
< |
public void testLockedInterruptibly() { |
169 |
> |
public void testLockInterruptibly() { |
170 |
|
final ReentrantLock lock = new ReentrantLock(); |
171 |
< |
try {lock.lockInterruptibly();} catch(Exception e) {} |
171 |
> |
try { |
172 |
> |
lock.lockInterruptibly(); |
173 |
> |
} catch(Exception e) { |
174 |
> |
fail("unexpected exception"); |
175 |
> |
} |
176 |
|
Thread t = new Thread(new Runnable() { |
177 |
|
public void run() { |
178 |
|
try { |
179 |
|
lock.lockInterruptibly(); |
180 |
< |
fail("Failed to generate an Interrupted Exception"); |
180 |
> |
fail("should throw"); |
181 |
|
} |
182 |
|
catch(InterruptedException e) {} |
183 |
|
} |
184 |
|
}); |
185 |
< |
t.start(); |
186 |
< |
t.interrupt(); |
187 |
< |
assertTrue(lock.isLocked()); |
188 |
< |
assertTrue(lock.isHeldByCurrentThread()); |
185 |
> |
try { |
186 |
> |
t.start(); |
187 |
> |
t.interrupt(); |
188 |
> |
assertTrue(lock.isLocked()); |
189 |
> |
assertTrue(lock.isHeldByCurrentThread()); |
190 |
> |
t.join(); |
191 |
> |
} catch(Exception e){ |
192 |
> |
fail("unexpected exception"); |
193 |
> |
} |
194 |
|
} |
195 |
|
|
196 |
|
public void testAwait_IllegalMonitor() { |
448 |
|
fail("unexpected exception"); |
449 |
|
} |
450 |
|
} |
451 |
+ |
|
452 |
+ |
public void testSerialization() { |
453 |
+ |
ReentrantLock l = new ReentrantLock(); |
454 |
+ |
l.lock(); |
455 |
+ |
l.unlock(); |
456 |
+ |
|
457 |
+ |
try { |
458 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
459 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
460 |
+ |
out.writeObject(l); |
461 |
+ |
out.close(); |
462 |
+ |
|
463 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
464 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
465 |
+ |
ReentrantLock r = (ReentrantLock) in.readObject(); |
466 |
+ |
r.lock(); |
467 |
+ |
r.unlock(); |
468 |
+ |
} catch(Exception e){ |
469 |
+ |
e.printStackTrace(); |
470 |
+ |
fail("unexpected exception"); |
471 |
+ |
} |
472 |
+ |
} |
473 |
|
|
474 |
|
} |