KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > ReentrantReadWriteLockTest


1 /*
2  * ReentrantReadWriteLockTest.java
3  * JUnit based test
4  *
5  * Created on 25. Juli 2006, 20:39
6  */

7 /*
8  * Copyright 2006 Schlichtherle IT Services
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

22
23 package de.schlichtherle.io;
24
25 import junit.framework.*;
26
27 /**
28  *
29  * @author Christian Schlichtherle
30  */

31 public class ReentrantReadWriteLockTest extends TestCase {
32     
33     public ReentrantReadWriteLockTest(String JavaDoc testName) {
34         super(testName);
35     }
36
37     private ReentrantReadWriteLock instance;
38
39     protected void setUp() throws Exception JavaDoc {
40         instance = new ReentrantReadWriteLock();
41     }
42
43     protected void tearDown() throws Exception JavaDoc {
44         instance = null;
45     }
46
47     /**
48      * Test of readLock method, of class de.schlichtherle.io.ReentrantReadWriteLock.
49      */

50     public void testReadLock() {
51         ReentrantLock rl = instance.readLock();
52         assertNotNull(rl);
53
54         rl.lock();
55         try {
56             runWithTimeout(1000, new InterruptibleRunnable() {
57                 public void run() throws InterruptedException JavaDoc {
58                     ReentrantLock wl = instance.writeLock();
59                     // Upgrading a read lock blocks until interrupted.
60
wl.lockInterruptibly();
61                 }
62             });
63             fail("Upgrading a read lock should dead lock!");
64         } catch (InterruptedException JavaDoc expected) {
65         }
66     }
67
68     /**
69      * Test of writeLock method, of class de.schlichtherle.io.ReentrantReadWriteLock.
70      */

71     public void testWriteLock() throws InterruptedException JavaDoc {
72         ReentrantLock wl = instance.writeLock();
73         assertNotNull(wl);
74
75         wl.lock();
76         runWithTimeout(1000, new InterruptibleRunnable() {
77             public void run() throws InterruptedException JavaDoc {
78                 ReentrantLock rl = instance.readLock();
79                 // Downgrading a write lock returns immediately.
80
rl.lockInterruptibly();
81             }
82         });
83     }
84
85     private void runWithTimeout(
86             final long timeout,
87             final InterruptibleRunnable runnable)
88     throws InterruptedException JavaDoc {
89         final Thread JavaDoc target = Thread.currentThread();
90         final Thread JavaDoc observer = new Thread JavaDoc(new Runnable JavaDoc() {
91             public void run() {
92                 try {
93                     Thread.sleep(timeout);
94                 } catch (InterruptedException JavaDoc ex) {
95                     ex.printStackTrace();
96                 }
97                 target.interrupt();
98             }
99         }, "Timeout thread");
100         observer.setDaemon(true);
101         observer.start();
102         runnable.run();
103     }
104     
105     private interface InterruptibleRunnable {
106         void run() throws InterruptedException JavaDoc;
107     }
108 }
109
Popular Tags