KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > lock > ReentrantWriterPreferenceReadWriteLockTest


1 package org.jboss.cache.lock;
2
3 import java.util.concurrent.TimeUnit JavaDoc;
4 import java.util.concurrent.locks.Lock JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestCase;
8 import junit.framework.TestSuite;
9 import org.jboss.cache.misc.TestingUtil;
10
11 /**
12  * Tests ReentrantWriterPreferenceReadWriteLock
13  * @author Bela Ban
14  * @version $Id: ReentrantWriterPreferenceReadWriteLockTest.java,v 1.4 2006/12/08 18:50:49 genman Exp $
15  */

16 public class ReentrantWriterPreferenceReadWriteLockTest extends TestCase {
17    // ReentrantWriterPreferenceReadWriteLock lock;
18
SimpleReadWriteLock lock;
19    Lock JavaDoc rl, wl;
20    Exception JavaDoc thread_ex=null;
21
22    protected void setUp() throws Exception JavaDoc {
23       super.setUp();
24       // lock=new ReentrantWriterPreferenceReadWriteLock();
25
lock=new SimpleReadWriteLock();
26       rl=lock.readLock();
27       wl=lock.writeLock();
28       thread_ex=null;
29    }
30
31    protected void tearDown() throws Exception JavaDoc {
32       super.tearDown();
33       lock=null;
34       if(thread_ex != null)
35          throw thread_ex;
36    }
37
38    public void testMultipleReadLockAcquisitions() throws InterruptedException JavaDoc {
39       rl.lock();
40       rl.lock();
41    }
42
43    public void testInterruptedLockAcquisition() {
44       Thread.currentThread().interrupt();
45       try {
46          rl.lockInterruptibly();
47          fail("thread should be in interrupted status");
48       }
49       catch(InterruptedException JavaDoc e) {
50       }
51       finally {
52          try {
53             rl.unlock();
54             fail("unlock() should throw an IllegalStateException");
55          }
56          catch(IllegalMonitorStateException JavaDoc illegalStateEx) {
57             assertTrue(true);
58          }
59       }
60    }
61
62    public void testMultipleWriteLockAcquisitions() throws InterruptedException JavaDoc {
63       wl.lock();
64       wl.lock();
65    }
66
67    public void testMultipleReadLockReleases() throws InterruptedException JavaDoc {
68       rl.lock();
69       rl.unlock();
70       try {
71          rl.unlock();
72          fail("we should not get here, cannot lock RL once but unlock twice");
73       }
74       catch(IllegalMonitorStateException JavaDoc illegalState) {
75          // this is as expected
76
}
77    }
78
79
80    public void acquireReadAndWriteLocks() throws InterruptedException JavaDoc {
81       rl.lock();
82       rl.lock();
83       boolean fl=wl.tryLock(4000, TimeUnit.MILLISECONDS);
84       assertTrue(fl);
85    }
86
87
88    public void acquireWriteThenReadLock() throws InterruptedException JavaDoc {
89       wl.lock();
90       rl.lock();
91       wl.unlock();
92       rl.unlock();
93    }
94
95    public void testMultipleWriteLockReleases() throws InterruptedException JavaDoc {
96       wl.lock();
97       wl.unlock();
98       try {
99          wl.unlock();
100          fail("expected");
101       } catch (IllegalMonitorStateException JavaDoc e) {}
102    }
103
104    public void testAcquireWriteLockAfterReadLock() throws InterruptedException JavaDoc {
105       rl.lock();
106       rl.unlock();
107       wl.lock();
108    }
109
110
111    public void testAcquiringReadLockedLockWithRead() throws InterruptedException JavaDoc {
112       new Thread JavaDoc() {
113          public void run() {
114             try {rl.lockInterruptibly();}
115             catch(InterruptedException JavaDoc e) {}
116          }
117       }.start();
118
119       TestingUtil.sleepThread(500);
120
121       // now we have a RL by another thread
122

123       boolean flag=rl.tryLock(3000, TimeUnit.MILLISECONDS);
124       assertTrue(flag);
125       flag=wl.tryLock(3000, TimeUnit.MILLISECONDS);
126       assertFalse(flag);
127    }
128
129    public void testAcquiringReadLockedLock() throws InterruptedException JavaDoc {
130       new Thread JavaDoc() {
131          public void run() {
132             try {rl.lockInterruptibly();}
133             catch(InterruptedException JavaDoc e) {}
134          }
135       }.start();
136
137       TestingUtil.sleepThread(500);
138
139       // now we have a RL by another thread
140
boolean flag=wl.tryLock(3000, TimeUnit.MILLISECONDS);
141       assertFalse(flag);
142    }
143
144    public void testWriteThenReadByDifferentTx() throws InterruptedException JavaDoc {
145       Writer writer=new Writer("Writer");
146       Reader reader=new Reader("Reader");
147       writer.start();
148       TestingUtil.sleepThread(500);
149       reader.start();
150       TestingUtil.sleepThread(1000);
151
152       synchronized(writer) {
153          log("terminating Writer");
154          writer.notify();
155       }
156       TestingUtil.sleepThread(500);
157       synchronized(reader) {
158          reader.notify();
159       }
160       writer.join();
161       reader.join();
162    }
163
164    public void testReadThenWriteByDifferentTx() throws InterruptedException JavaDoc {
165       Writer writer=new Writer("Writer");
166       Reader reader=new Reader("Reader");
167
168       reader.start();
169       TestingUtil.sleepThread(500);
170       writer.start();
171       TestingUtil.sleepThread(1000);
172
173       synchronized(reader) {
174          log("terminating Reader");
175          reader.notify();
176       }
177
178       TestingUtil.sleepThread(500);
179       synchronized(writer) {
180          writer.notify();
181       }
182       writer.join();
183       reader.join();
184    }
185
186
187
188    private static void log(String JavaDoc msg) {
189       System.out.println(System.currentTimeMillis() + " " + Thread.currentThread() +
190                          " [" + Thread.currentThread().getName() + "]: " + msg);
191    }
192
193    class Reader extends Thread JavaDoc {
194
195       public Reader(String JavaDoc name) {
196          super(name);
197       }
198
199       public void run() {
200          try {
201             log("acquiring RL");
202             rl.lock();
203             log("acquired RL");
204             synchronized(this) {
205                this.wait();
206             }
207             log("releasing RL");
208             rl.unlock();
209             log("released RL");
210          }
211          catch(InterruptedException JavaDoc e) {
212             ;
213          }
214       }
215    }
216
217
218    class Writer extends Thread JavaDoc {
219
220       public Writer(String JavaDoc name) {
221          super(name);
222       }
223
224       public void run() {
225          try {
226             log("acquiring WL");
227             wl.lock();
228             log("acquired WL");
229             synchronized(this) {
230                this.wait();
231             }
232             log("releasing WL");
233             wl.unlock();
234             log("released WL");
235          }
236          catch(InterruptedException JavaDoc e) {
237             ;
238          }
239       }
240    }
241
242
243    class Upgrader extends Thread JavaDoc {
244       boolean upgradeSuccessful=false;
245       public Upgrader(String JavaDoc name) {
246          super(name);
247       }
248
249       public boolean wasUpgradeSuccessful() {
250          return upgradeSuccessful;
251       }
252
253
254       public void run() {
255          try {
256             log("acquiring RL");
257             rl.lock();
258             log("acquired RL");
259             synchronized(this) {
260                this.wait();
261             }
262             log("attempting to lock WL");
263             // rl.unlock();
264
wl.lock();
265             upgradeSuccessful=true;
266             log("acquired WL");
267             log("releasing WL/RL");
268             wl.unlock();
269             log("released WL/RL");
270          }
271          catch(InterruptedException JavaDoc e) {
272             ;
273          }
274       }
275    }
276
277
278
279    public static Test suite() {
280       return new TestSuite(ReentrantWriterPreferenceReadWriteLockTest.class);
281    }
282
283    public static void main(String JavaDoc[] args) {
284       junit.textui.TestRunner.run(suite());
285    }
286
287 }
288
Popular Tags