ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.2
Committed: Sun Sep 7 20:39:11 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.1: +581 -36 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.2 import java.io.*;
12 dl 1.1
13     public class ReentrantReadWriteLockTest 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(ReentrantReadWriteLockTest.class);
22     }
23    
24    
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    
29     /*
30     * Unlocks an unlocked lock, throws Illegal Monitor State
31     *
32     */
33    
34     public void testIllegalMonitorStateException(){
35     ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
36     try{
37     rl.writeLock().unlock();
38     fail("Should of thown Illegal Monitor State Exception");
39    
40 dl 1.2 }catch(IllegalMonitorStateException success){}
41     }
42 dl 1.1
43    
44    
45     public void testInterruptedException(){
46     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
47     lock.writeLock().lock();
48     Thread t = new Thread(new Runnable() {
49     public void run(){
50     try{
51     lock.writeLock().lockInterruptibly();
52     fail("should throw");
53 dl 1.2 }catch(InterruptedException success){}
54 dl 1.1 }
55     });
56 dl 1.2 try {
57     t.start();
58     t.interrupt();
59     lock.writeLock().unlock();
60     t.join();
61     } catch(Exception e){
62     fail("unexpected exception");
63     }
64 dl 1.1 }
65    
66     public void testInterruptedException2(){
67     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
68     lock.writeLock().lock();
69     Thread t = new Thread(new Runnable() {
70     public void run(){
71     try{
72     lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
73     fail("should throw");
74 dl 1.2 }catch(InterruptedException success){}
75     }
76     });
77     try {
78     t.start();
79     t.interrupt();
80     lock.writeLock().unlock();
81     t.join();
82     } catch(Exception e){
83     fail("unexpected exception");
84     }
85     }
86    
87     public void testInterruptedException3(){
88     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
89     lock.writeLock().lock();
90     Thread t = new Thread(new Runnable() {
91     public void run(){
92     try{
93     lock.readLock().lockInterruptibly();
94     fail("should throw");
95     }catch(InterruptedException success){}
96     }
97     });
98     try {
99     t.start();
100     t.interrupt();
101     lock.writeLock().unlock();
102     t.join();
103     } catch(Exception e){
104     fail("unexpected exception");
105     }
106     }
107    
108     public void testInterruptedException4(){
109     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
110     lock.writeLock().lock();
111     Thread t = new Thread(new Runnable() {
112     public void run(){
113     try{
114     lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
115     fail("should throw");
116     }catch(InterruptedException success){}
117 dl 1.1 }
118     });
119 dl 1.2 try {
120     t.start();
121     t.interrupt();
122     t.join();
123     } catch(Exception e){
124     fail("unexpected exception");
125     }
126 dl 1.1 }
127    
128    
129 dl 1.2 public void testTryLockWhenLocked() {
130     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
131     lock.writeLock().lock();
132     Thread t = new Thread(new Runnable() {
133     public void run(){
134     assertFalse(lock.writeLock().tryLock());
135     }
136     });
137     try {
138     t.start();
139     t.join();
140     lock.writeLock().unlock();
141     } catch(Exception e){
142     fail("unexpected exception");
143     }
144     }
145    
146     public void testTryLockWhenLocked2() {
147     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
148     lock.writeLock().lock();
149     Thread t = new Thread(new Runnable() {
150     public void run(){
151     assertFalse(lock.readLock().tryLock());
152     }
153     });
154     try {
155     t.start();
156     t.join();
157     lock.writeLock().unlock();
158     } catch(Exception e){
159     fail("unexpected exception");
160     }
161     }
162    
163     public void testMultipleReadLocks() {
164     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
165     lock.readLock().lock();
166     Thread t = new Thread(new Runnable() {
167     public void run(){
168     assertTrue(lock.readLock().tryLock());
169     lock.readLock().unlock();
170     }
171     });
172     try {
173     t.start();
174     t.join();
175     lock.readLock().unlock();
176     } catch(Exception e){
177     fail("unexpected exception");
178     }
179     }
180    
181     public void testWriteAfterMultipleReadLocks() {
182     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
183     lock.readLock().lock();
184     Thread t1 = new Thread(new Runnable() {
185     public void run(){
186     lock.readLock().lock();
187     lock.readLock().unlock();
188     }
189     });
190     Thread t2 = new Thread(new Runnable() {
191     public void run(){
192     lock.writeLock().lock();
193     lock.writeLock().unlock();
194     }
195     });
196    
197     try {
198     t1.start();
199     t2.start();
200     Thread.sleep(SHORT_DELAY_MS);
201     lock.readLock().unlock();
202     t1.join(MEDIUM_DELAY_MS);
203     t2.join(MEDIUM_DELAY_MS);
204     assertTrue(!t1.isAlive());
205     assertTrue(!t2.isAlive());
206    
207     } catch(Exception e){
208     fail("unexpected exception");
209     }
210     }
211    
212     public void testReadAfterWriteLock() {
213     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
214     lock.writeLock().lock();
215     Thread t1 = new Thread(new Runnable() {
216     public void run(){
217     lock.readLock().lock();
218     lock.readLock().unlock();
219     }
220     });
221     Thread t2 = new Thread(new Runnable() {
222     public void run(){
223     lock.readLock().lock();
224     lock.readLock().unlock();
225     }
226     });
227    
228     try {
229     t1.start();
230     t2.start();
231     Thread.sleep(SHORT_DELAY_MS);
232     lock.writeLock().unlock();
233     t1.join(MEDIUM_DELAY_MS);
234     t2.join(MEDIUM_DELAY_MS);
235     assertTrue(!t1.isAlive());
236     assertTrue(!t2.isAlive());
237    
238     } catch(Exception e){
239     fail("unexpected exception");
240     }
241     }
242    
243    
244     public void testTryLockWhenReadLocked() {
245     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
246     lock.readLock().lock();
247     Thread t = new Thread(new Runnable() {
248     public void run(){
249     assertTrue(lock.readLock().tryLock());
250     lock.readLock().unlock();
251     }
252     });
253     try {
254     t.start();
255     t.join();
256     lock.readLock().unlock();
257     } catch(Exception e){
258     fail("unexpected exception");
259     }
260     }
261    
262    
263    
264     public void testWriteTryLockWhenReadLocked() {
265     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
266     lock.readLock().lock();
267     Thread t = new Thread(new Runnable() {
268     public void run(){
269     assertFalse(lock.writeLock().tryLock());
270     }
271     });
272     try {
273     t.start();
274     t.join();
275     lock.readLock().unlock();
276     } catch(Exception e){
277     fail("unexpected exception");
278     }
279     }
280    
281    
282    
283     public void testTryLock_Timeout(){
284     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
285     lock.writeLock().lock();
286     Thread t = new Thread(new Runnable() {
287     public void run(){
288     try {
289     assertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
290     } catch (Exception ex) {
291     fail("unexpected exception");
292     }
293     }
294     });
295     try {
296     t.start();
297     t.join();
298     lock.writeLock().unlock();
299     } catch(Exception e){
300     fail("unexpected exception");
301     }
302     }
303    
304     public void testTryLock_Timeout2(){
305     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
306     lock.writeLock().lock();
307     Thread t = new Thread(new Runnable() {
308     public void run(){
309     try {
310     assertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
311     } catch (Exception ex) {
312     fail("unexpected exception");
313     }
314     }
315     });
316     try {
317     t.start();
318     t.join();
319     lock.writeLock().unlock();
320     } catch(Exception e){
321     fail("unexpected exception");
322     }
323     }
324    
325    
326     public void testLockInterruptibly() {
327     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
328     try {
329     lock.writeLock().lockInterruptibly();
330     } catch(Exception e) {
331     fail("unexpected exception");
332     }
333     Thread t = new Thread(new Runnable() {
334     public void run() {
335     try {
336     lock.writeLock().lockInterruptibly();
337     fail("should throw");
338     }
339     catch(InterruptedException success) {
340     }
341     }
342     });
343     try {
344     t.start();
345     t.interrupt();
346     t.join();
347     lock.writeLock().unlock();
348     } catch(Exception e){
349     fail("unexpected exception");
350     }
351     }
352    
353     public void testLockInterruptibly2() {
354     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
355     try {
356     lock.writeLock().lockInterruptibly();
357     } catch(Exception e) {
358     fail("unexpected exception");
359     }
360     Thread t = new Thread(new Runnable() {
361     public void run() {
362     try {
363     lock.readLock().lockInterruptibly();
364     fail("should throw");
365     }
366     catch(InterruptedException success) {
367     }
368     }
369     });
370     try {
371     t.start();
372     t.interrupt();
373     t.join();
374     lock.writeLock().unlock();
375     } catch(Exception e){
376     fail("unexpected exception");
377     }
378     }
379    
380     public void testAwait_IllegalMonitor() {
381     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
382     final Condition c = lock.writeLock().newCondition();
383     try {
384     c.await();
385     fail("should throw");
386     }
387     catch (IllegalMonitorStateException success) {
388     }
389     catch (Exception ex) {
390     fail("should throw IMSE");
391     }
392     }
393    
394     public void testSignal_IllegalMonitor() {
395     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
396     final Condition c = lock.writeLock().newCondition();
397     try {
398     c.signal();
399     fail("should throw");
400     }
401     catch (IllegalMonitorStateException success) {
402     }
403     catch (Exception ex) {
404     fail("should throw IMSE");
405     }
406     }
407    
408     public void testAwaitNanos_Timeout() {
409     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
410     final Condition c = lock.writeLock().newCondition();
411     try {
412     lock.writeLock().lock();
413     long t = c.awaitNanos(100);
414     assertTrue(t <= 0);
415     lock.writeLock().unlock();
416     }
417     catch (Exception ex) {
418     fail("unexpected exception");
419     }
420     }
421    
422     public void testAwait_Timeout() {
423     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
424     final Condition c = lock.writeLock().newCondition();
425     try {
426     lock.writeLock().lock();
427     assertFalse(c.await(10, TimeUnit.MILLISECONDS));
428     lock.writeLock().unlock();
429     }
430     catch (Exception ex) {
431     fail("unexpected exception");
432     }
433     }
434    
435     public void testAwaitUntil_Timeout() {
436     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
437     final Condition c = lock.writeLock().newCondition();
438     try {
439     lock.writeLock().lock();
440     java.util.Date d = new java.util.Date();
441     assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
442     lock.writeLock().unlock();
443     }
444     catch (Exception ex) {
445     fail("unexpected exception");
446     }
447     }
448 dl 1.1
449 dl 1.2 public void testAwait() {
450     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
451     final Condition c = lock.writeLock().newCondition();
452     Thread t = new Thread(new Runnable() {
453     public void run() {
454     try {
455     lock.writeLock().lock();
456     c.await();
457     lock.writeLock().unlock();
458     }
459     catch(InterruptedException e) {
460     fail("unexpected exception");
461     }
462     }
463     });
464    
465     try {
466     t.start();
467     Thread.sleep(SHORT_DELAY_MS);
468     lock.writeLock().lock();
469     c.signal();
470     lock.writeLock().unlock();
471     t.join(SHORT_DELAY_MS);
472     assertFalse(t.isAlive());
473     }
474     catch (Exception ex) {
475     fail("unexpected exception");
476     }
477     }
478    
479     public void testAwaitUninterruptibly() {
480     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
481     final Condition c = lock.writeLock().newCondition();
482     Thread t = new Thread(new Runnable() {
483     public void run() {
484     lock.writeLock().lock();
485     c.awaitUninterruptibly();
486     lock.writeLock().unlock();
487     }
488     });
489    
490     try {
491     t.start();
492     Thread.sleep(SHORT_DELAY_MS);
493     t.interrupt();
494     lock.writeLock().lock();
495     c.signal();
496     lock.writeLock().unlock();
497     t.join(SHORT_DELAY_MS);
498     assertFalse(t.isAlive());
499     }
500     catch (Exception ex) {
501     fail("unexpected exception");
502     }
503     }
504    
505     public void testAwait_Interrupt() {
506     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
507     final Condition c = lock.writeLock().newCondition();
508     Thread t = new Thread(new Runnable() {
509     public void run() {
510     try {
511     lock.writeLock().lock();
512     c.await();
513     lock.writeLock().unlock();
514     fail("should throw");
515     }
516     catch(InterruptedException success) {
517     }
518     }
519     });
520    
521     try {
522     t.start();
523     Thread.sleep(SHORT_DELAY_MS);
524     t.interrupt();
525     t.join(SHORT_DELAY_MS);
526     assertFalse(t.isAlive());
527     }
528     catch (Exception ex) {
529     fail("unexpected exception");
530     }
531     }
532    
533     public void testAwaitNanos_Interrupt() {
534     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
535     final Condition c = lock.writeLock().newCondition();
536     Thread t = new Thread(new Runnable() {
537     public void run() {
538     try {
539     lock.writeLock().lock();
540     c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
541     lock.writeLock().unlock();
542     fail("should throw");
543     }
544     catch(InterruptedException success) {
545     }
546     }
547     });
548    
549     try {
550     t.start();
551     Thread.sleep(SHORT_DELAY_MS);
552     t.interrupt();
553     t.join(SHORT_DELAY_MS);
554     assertFalse(t.isAlive());
555     }
556     catch (Exception ex) {
557     fail("unexpected exception");
558     }
559     }
560 dl 1.1
561 dl 1.2 public void testAwaitUntil_Interrupt() {
562 dl 1.1 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
563 dl 1.2 final Condition c = lock.writeLock().newCondition();
564 dl 1.1 Thread t = new Thread(new Runnable() {
565     public void run() {
566     try {
567 dl 1.2 lock.writeLock().lock();
568     java.util.Date d = new java.util.Date();
569     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
570     lock.writeLock().unlock();
571     fail("should throw");
572     }
573     catch(InterruptedException success) {
574     }
575     }
576     });
577    
578     try {
579     t.start();
580     Thread.sleep(SHORT_DELAY_MS);
581     t.interrupt();
582     t.join(SHORT_DELAY_MS);
583     assertFalse(t.isAlive());
584     }
585     catch (Exception ex) {
586     fail("unexpected exception");
587     }
588     }
589    
590     public void testSignalAll() {
591     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
592     final Condition c = lock.writeLock().newCondition();
593     Thread t1 = new Thread(new Runnable() {
594     public void run() {
595     try {
596     lock.writeLock().lock();
597     c.await();
598     lock.writeLock().unlock();
599     }
600     catch(InterruptedException e) {
601     fail("unexpected exception");
602     }
603     }
604     });
605    
606     Thread t2 = new Thread(new Runnable() {
607     public void run() {
608     try {
609     lock.writeLock().lock();
610     c.await();
611     lock.writeLock().unlock();
612 dl 1.1 }
613 dl 1.2 catch(InterruptedException e) {
614     fail("unexpected exception");
615     }
616 dl 1.1 }
617     });
618 dl 1.2
619     try {
620     t1.start();
621     t2.start();
622     Thread.sleep(SHORT_DELAY_MS);
623     lock.writeLock().lock();
624     c.signalAll();
625     lock.writeLock().unlock();
626     t1.join(SHORT_DELAY_MS);
627     t2.join(SHORT_DELAY_MS);
628     assertFalse(t1.isAlive());
629     assertFalse(t2.isAlive());
630     }
631     catch (Exception ex) {
632     fail("unexpected exception");
633     }
634     }
635    
636     public void testSerialization() {
637     ReentrantReadWriteLock l = new ReentrantReadWriteLock();
638     l.readLock().lock();
639     l.readLock().unlock();
640    
641     try {
642     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
643     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
644     out.writeObject(l);
645     out.close();
646    
647     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
648     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
649     ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject();
650     r.readLock().lock();
651     r.readLock().unlock();
652     } catch(Exception e){
653     e.printStackTrace();
654     fail("unexpected exception");
655     }
656 dl 1.1 }
657 dl 1.2
658 dl 1.1
659     }