ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.3
Committed: Sun Sep 7 20:39:11 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.2: +80 -18 lines
Log Message:
Added serialization and lock tests

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8     import junit.framework.*;
9     import java.util.concurrent.locks.*;
10     import java.util.concurrent.*;
11 dl 1.3 import java.io.*;
12 dl 1.1
13     public class ReentrantLockTest extends TestCase {
14     static int HOLD_COUNT_TEST_LIMIT = 20;
15    
16     public static void main(String[] args) {
17     junit.textui.TestRunner.run (suite());
18     }
19    
20     public static Test suite() {
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    
28     /*
29     * Unlocks an unlocked lock, throws Illegal Monitor State
30     *
31     */
32    
33     public void testIllegalMonitorStateException(){
34     ReentrantLock rl = new ReentrantLock();
35     try{
36     rl.unlock();
37     fail("Should of thown Illegal Monitor State Exception");
38    
39 dl 1.3 } catch(IllegalMonitorStateException success){}
40 dl 1.1
41    
42     }
43    
44     /*
45     * makes a lock, locks it, tries to aquire the lock in another thread
46     * interrupts that thread and waits for an interrupted Exception to
47     * be thrown.
48     */
49    
50     public void testInterruptedException(){
51     final ReentrantLock lock = new ReentrantLock();
52     lock.lock();
53     Thread t = new Thread(new Runnable() {
54     public void run(){
55     try{
56     lock.lockInterruptibly();
57     fail("should throw");
58 dl 1.3 } catch(InterruptedException success){}
59 dl 1.1 }
60     });
61     t.start();
62     t.interrupt();
63     lock.unlock();
64     }
65    
66     /*
67     * tests for interrupted exception on a timed wait
68     *
69     */
70    
71    
72     public void testInterruptedException2(){
73     final ReentrantLock lock = new ReentrantLock();
74     lock.lock();
75     Thread t = new Thread(new Runnable() {
76     public void run(){
77     try{
78     lock.tryLock(1000,TimeUnit.MILLISECONDS);
79     fail("should throw");
80 dl 1.3 } catch(InterruptedException success){}
81 dl 1.1 }
82     });
83     t.start();
84     t.interrupt();
85     }
86    
87 dl 1.3
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 dl 1.1
126     public void testGetHoldCount() {
127     ReentrantLock lock = new ReentrantLock();
128     for(int i = 1; i <= ReentrantLockTest.HOLD_COUNT_TEST_LIMIT;i++) {
129     lock.lock();
130     assertEquals(i,lock.getHoldCount());
131     }
132     for(int i = ReentrantLockTest.HOLD_COUNT_TEST_LIMIT; i > 0; i--) {
133     lock.unlock();
134     assertEquals(i-1,lock.getHoldCount());
135     }
136     }
137    
138    
139    
140    
141     public void testIsLocked() {
142     final ReentrantLock lock = new ReentrantLock();
143     lock.lock();
144     assertTrue(lock.isLocked());
145     lock.unlock();
146     assertFalse(lock.isLocked());
147     Thread t = new Thread(new Runnable() {
148     public void run() {
149     lock.lock();
150     try {
151     Thread.sleep(SHORT_DELAY_MS * 2);
152     }
153     catch(Exception e) {}
154     lock.unlock();
155     }
156     });
157     try{
158     t.start();
159     Thread.sleep(SHORT_DELAY_MS);
160     assertTrue(lock.isLocked());
161     t.join();
162     assertFalse(lock.isLocked());
163     } catch(Exception e){
164     fail("unexpected exception");
165     }
166     }
167    
168    
169 dl 1.3 public void testLockInterruptibly() {
170 dl 1.1 final ReentrantLock lock = new ReentrantLock();
171 dl 1.3 try {
172     lock.lockInterruptibly();
173     } catch(Exception e) {
174     fail("unexpected exception");
175     }
176 dl 1.1 Thread t = new Thread(new Runnable() {
177     public void run() {
178     try {
179     lock.lockInterruptibly();
180 dl 1.3 fail("should throw");
181 dl 1.1 }
182     catch(InterruptedException e) {}
183     }
184     });
185 dl 1.3 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 dl 1.1 }
195 dl 1.2
196     public void testAwait_IllegalMonitor() {
197     final ReentrantLock lock = new ReentrantLock();
198     final Condition c = lock.newCondition();
199     try {
200     c.await();
201     fail("should throw");
202     }
203     catch (IllegalMonitorStateException success) {
204     }
205     catch (Exception ex) {
206     fail("should throw IMSE");
207     }
208     }
209    
210     public void testSignal_IllegalMonitor() {
211     final ReentrantLock lock = new ReentrantLock();
212     final Condition c = lock.newCondition();
213     try {
214     c.signal();
215     fail("should throw");
216     }
217     catch (IllegalMonitorStateException success) {
218     }
219     catch (Exception ex) {
220     fail("should throw IMSE");
221     }
222     }
223    
224     public void testAwaitNanos_Timeout() {
225     final ReentrantLock lock = new ReentrantLock();
226     final Condition c = lock.newCondition();
227     try {
228     lock.lock();
229     long t = c.awaitNanos(100);
230     assertTrue(t <= 0);
231     lock.unlock();
232     }
233     catch (Exception ex) {
234     fail("unexpected exception");
235     }
236     }
237    
238     public void testAwait_Timeout() {
239     final ReentrantLock lock = new ReentrantLock();
240     final Condition c = lock.newCondition();
241     try {
242     lock.lock();
243     assertFalse(c.await(10, TimeUnit.MILLISECONDS));
244     lock.unlock();
245     }
246     catch (Exception ex) {
247     fail("unexpected exception");
248     }
249     }
250    
251     public void testAwaitUntil_Timeout() {
252     final ReentrantLock lock = new ReentrantLock();
253     final Condition c = lock.newCondition();
254     try {
255     lock.lock();
256     java.util.Date d = new java.util.Date();
257     assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
258     lock.unlock();
259     }
260     catch (Exception ex) {
261     fail("unexpected exception");
262     }
263     }
264    
265     public void testAwait() {
266     final ReentrantLock lock = new ReentrantLock();
267     final Condition c = lock.newCondition();
268     Thread t = new Thread(new Runnable() {
269     public void run() {
270     try {
271     lock.lock();
272     c.await();
273     lock.unlock();
274     }
275     catch(InterruptedException e) {
276     fail("unexpected exception");
277     }
278     }
279     });
280    
281     try {
282     t.start();
283     Thread.sleep(SHORT_DELAY_MS);
284     lock.lock();
285     c.signal();
286     lock.unlock();
287     t.join(SHORT_DELAY_MS);
288     assertFalse(t.isAlive());
289     }
290     catch (Exception ex) {
291     fail("unexpected exception");
292     }
293     }
294    
295     public void testAwaitUninterruptibly() {
296     final ReentrantLock lock = new ReentrantLock();
297     final Condition c = lock.newCondition();
298     Thread t = new Thread(new Runnable() {
299     public void run() {
300     lock.lock();
301     c.awaitUninterruptibly();
302     lock.unlock();
303     }
304     });
305    
306     try {
307     t.start();
308     Thread.sleep(SHORT_DELAY_MS);
309     t.interrupt();
310     lock.lock();
311     c.signal();
312     lock.unlock();
313     t.join(SHORT_DELAY_MS);
314     assertFalse(t.isAlive());
315     }
316     catch (Exception ex) {
317     fail("unexpected exception");
318     }
319     }
320    
321     public void testAwait_Interrupt() {
322     final ReentrantLock lock = new ReentrantLock();
323     final Condition c = lock.newCondition();
324     Thread t = new Thread(new Runnable() {
325     public void run() {
326     try {
327     lock.lock();
328     c.await();
329     lock.unlock();
330     fail("should throw");
331     }
332     catch(InterruptedException success) {
333     }
334     }
335     });
336    
337     try {
338     t.start();
339     Thread.sleep(SHORT_DELAY_MS);
340     t.interrupt();
341     t.join(SHORT_DELAY_MS);
342     assertFalse(t.isAlive());
343     }
344     catch (Exception ex) {
345     fail("unexpected exception");
346     }
347     }
348    
349     public void testAwaitNanos_Interrupt() {
350     final ReentrantLock lock = new ReentrantLock();
351     final Condition c = lock.newCondition();
352     Thread t = new Thread(new Runnable() {
353     public void run() {
354     try {
355     lock.lock();
356     c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
357     lock.unlock();
358     fail("should throw");
359     }
360     catch(InterruptedException success) {
361     }
362     }
363     });
364    
365     try {
366     t.start();
367     Thread.sleep(SHORT_DELAY_MS);
368     t.interrupt();
369     t.join(SHORT_DELAY_MS);
370     assertFalse(t.isAlive());
371     }
372     catch (Exception ex) {
373     fail("unexpected exception");
374     }
375     }
376    
377     public void testAwaitUntil_Interrupt() {
378     final ReentrantLock lock = new ReentrantLock();
379     final Condition c = lock.newCondition();
380     Thread t = new Thread(new Runnable() {
381     public void run() {
382     try {
383     lock.lock();
384     java.util.Date d = new java.util.Date();
385     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
386     lock.unlock();
387     fail("should throw");
388     }
389     catch(InterruptedException success) {
390     }
391     }
392     });
393    
394     try {
395     t.start();
396     Thread.sleep(SHORT_DELAY_MS);
397     t.interrupt();
398     t.join(SHORT_DELAY_MS);
399     assertFalse(t.isAlive());
400     }
401     catch (Exception ex) {
402     fail("unexpected exception");
403     }
404     }
405    
406     public void testSignalAll() {
407     final ReentrantLock lock = new ReentrantLock();
408     final Condition c = lock.newCondition();
409     Thread t1 = new Thread(new Runnable() {
410     public void run() {
411     try {
412     lock.lock();
413     c.await();
414     lock.unlock();
415     }
416     catch(InterruptedException e) {
417     fail("unexpected exception");
418     }
419     }
420     });
421    
422     Thread t2 = new Thread(new Runnable() {
423     public void run() {
424     try {
425     lock.lock();
426     c.await();
427     lock.unlock();
428     }
429     catch(InterruptedException e) {
430     fail("unexpected exception");
431     }
432     }
433     });
434    
435     try {
436     t1.start();
437     t2.start();
438     Thread.sleep(SHORT_DELAY_MS);
439     lock.lock();
440     c.signalAll();
441     lock.unlock();
442     t1.join(SHORT_DELAY_MS);
443     t2.join(SHORT_DELAY_MS);
444     assertFalse(t1.isAlive());
445     assertFalse(t2.isAlive());
446     }
447     catch (Exception ex) {
448 dl 1.3 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 dl 1.2 fail("unexpected exception");
471     }
472     }
473 dl 1.1
474     }