KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.cache.tests;
8
9 import EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock;
10 import EDU.oswego.cs.dl.util.concurrent.Sync;
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14 import org.jboss.cache.lock.SimpleReadWriteLock;
15
16 /**
17  * Tests ReentrantWriterPreferenceReadWriteLock
18  * @author Bela Ban
19  * @version $Id: ReentrantWriterPreferenceReadWriteLockTest.java,v 1.3.2.2 2005/04/06 21:07:04 starksm Exp $
20  */

21 public class ReentrantWriterPreferenceReadWriteLockTest extends TestCase {
22    // ReentrantWriterPreferenceReadWriteLock lock;
23
SimpleReadWriteLock lock;
24    Sync rl, wl;
25    Exception JavaDoc thread_ex=null;
26
27    protected void setUp() throws Exception JavaDoc {
28       super.setUp();
29       // lock=new ReentrantWriterPreferenceReadWriteLock();
30
lock=new SimpleReadWriteLock();
31       rl=lock.readLock();
32       wl=lock.writeLock();
33       thread_ex=null;
34    }
35
36    protected void tearDown() throws Exception JavaDoc {
37       super.tearDown();
38       lock=null;
39       if(thread_ex != null)
40          throw thread_ex;
41    }
42
43    public void testMultipleReadLockAcquisitions() throws InterruptedException JavaDoc {
44       rl.acquire();
45       rl.acquire();
46    }
47
48    public void testMultipleWriteLockAcquisitions() throws InterruptedException JavaDoc {
49       wl.acquire();
50       wl.acquire();
51    }
52
53    public void testMultipleReadLockReleases() throws InterruptedException JavaDoc {
54       rl.acquire();
55       rl.release();
56       try {
57          rl.release();
58          fail("we should not get here, cannot acquire RL once but release twice");
59       }
60       catch(IllegalStateException JavaDoc illegalState) {
61          // this is as expected
62
}
63    }
64
65
66    public void acquireReadAndWriteLocks() throws InterruptedException JavaDoc {
67       rl.acquire();
68       rl.acquire();
69       boolean fl=wl.attempt(4000);
70       assertTrue(fl);
71    }
72
73
74    public void acquireWriteThenReadLock() throws InterruptedException JavaDoc {
75       wl.acquire();
76       rl.acquire();
77       wl.release();
78       rl.release();
79    }
80
81    public void testMultipleWriteLockReleases() throws InterruptedException JavaDoc {
82       wl.acquire();
83       wl.release();
84       wl.release();
85    }
86
87    public void testAcquireWriteLockAfterReadLock() throws InterruptedException JavaDoc {
88       rl.acquire();
89       wl.acquire();
90    }
91
92
93    public void testAcquiringReadLockedLockWithRead() throws InterruptedException JavaDoc {
94       new Thread JavaDoc() {
95          public void run() {
96             try {rl.acquire();}
97             catch(InterruptedException JavaDoc e) {}
98          }
99       }.start();
100
101       sleep(500);
102
103       // now we have a RL by another thread
104

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