KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > lock > IdentityLockTest


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.lock;
8
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.jboss.cache.Fqn;
16 import org.jboss.cache.GlobalTransaction;
17 import org.jboss.cache.lock.*;
18
19
20 /** Testing of different locking semantics.
21  * @author Bela Ban
22  * @author Ben Wang
23  * @version $Revision: 1.2 $
24  */

25 public class IdentityLockTest extends TestCase
26 {
27    IdentityLock lock_;
28    Object JavaDoc other_ = new Object JavaDoc();
29    Log logger_=LogFactory.getLog(IdentityLockTest.class);
30    static Throwable JavaDoc thread_ex=null;
31    final Fqn FQN=Fqn.fromString("/dummyfqn");
32
33
34    public IdentityLockTest(String JavaDoc name)
35    {
36       super(name);
37    }
38
39    protected void setUp() throws Exception JavaDoc
40    {
41       super.setUp();
42       lock_ = new IdentityLock(null, FQN);
43
44 // try { Thread.sleep(10000); } catch (Exception e) {
45
// }
46
}
47
48    protected void tearDown() throws Exception JavaDoc
49    {
50       super.tearDown();
51       lock_.releaseAll();
52       lock_ = null;
53       thread_ex=null;
54    }
55
56    protected void setLevelRW()
57    {
58       log("set lock level to RWUpgrade ...");
59       LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
60       lock_ = new IdentityLock(null, FQN);
61    }
62
63    protected void setLevelSerial()
64    {
65       log("set lock level to SimpleLock ...");
66       LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE);
67       lock_ = new IdentityLock(null, FQN);
68    }
69
70    protected GlobalTransaction getGlobalTransactionFromThread()
71    {
72       GlobalTransaction gtx = null;
73       if (gtx == null) {
74          gtx = GlobalTransaction.create(null);
75       }
76
77       return gtx;
78    }
79
80    public void testNullOwner_RWLock() throws InterruptedException JavaDoc {
81       setLevelRW();
82       nullOwner();
83    }
84
85    public void testNullOwner_SimpleLock() throws InterruptedException JavaDoc {
86       setLevelSerial();
87       nullOwner();
88    }
89
90    protected void nullOwner() throws InterruptedException JavaDoc {
91       log("testNullOwner ...");
92       try {
93          GlobalTransaction gtx = getGlobalTransactionFromThread();
94          lock_.acquireWriteLock(gtx, 50);
95          lock_.release(gtx);
96
97          lock_.acquireReadLock(gtx, 50);
98          lock_.release(gtx);
99       } catch (LockingException e) {
100          fail(e.toString());
101       } catch (TimeoutException e) {
102          fail(e.toString());
103       }
104    }
105
106    public void testNullOwner2_RWLock() throws InterruptedException JavaDoc {
107       setLevelRW();
108       nullOwner2();
109    }
110
111    public void testNullOwner2_SimpleLock() throws InterruptedException JavaDoc {
112       setLevelSerial();
113       nullOwner2();
114    }
115
116    protected void nullOwner2() throws InterruptedException JavaDoc {
117       log("testNullOwner2 ...");
118       try {
119          GlobalTransaction gtx = getGlobalTransactionFromThread();
120          lock_.acquireReadLock(gtx, 50);
121          lock_.acquireWriteLock(gtx, 50); // this should succeed
122
lock_.release(gtx);
123       } catch (LockingException e) {
124          fail(e.toString());
125       } catch (TimeoutException e2) {
126          fail(e2.toString());
127       }
128    }
129
130    public void testNullOwner3_RWLock() throws InterruptedException JavaDoc {
131       setLevelRW();
132       nullOwner3();
133    }
134
135    public void testNullOwner3_SimpleLock() throws InterruptedException JavaDoc {
136       setLevelSerial();
137       nullOwner3();
138    }
139
140    public void nullOwner3() throws InterruptedException JavaDoc {
141       log("testNullOwner3 ...");
142       try {
143          GlobalTransaction gtx = getGlobalTransactionFromThread();
144          lock_.acquireWriteLock(gtx, 50);
145          lock_.acquireReadLock(gtx, 50); // this should succeed
146
lock_.release(gtx);
147       } catch (LockingException e) {
148          fail(e.toString());
149       } catch (TimeoutException e2) {
150          fail(e2.toString());
151       }
152    }
153
154    public void testAcquireAndRelease_RWLock() throws InterruptedException JavaDoc {
155       setLevelRW();
156       acquireAndRelease();
157    }
158
159    public void testAcquireAndRelease_SimpleLock() throws InterruptedException JavaDoc {
160       setLevelSerial();
161       acquireAndRelease();
162    }
163
164    public void acquireAndRelease() throws InterruptedException JavaDoc {
165       log("testAcquireAndRelease ...");
166       try {
167          lock_.acquireReadLock(this, 50);
168          assertTrue("Is the lock owner", lock_.isOwner(this));
169          assertTrue(lock_.getReaderOwners().contains(this));
170
171          lock_.acquireReadLock(this, 50); // this should succeed
172
assertTrue("Is the lock owner", lock_.isOwner(this));
173          assertTrue(lock_.getReaderOwners().contains(this));
174
175          lock_.acquireWriteLock(this, 50); // this should succeed
176
assertTrue("Is the lock owner", lock_.isOwner(this));
177          assertTrue(!lock_.getReaderOwners().contains(this));
178          assertTrue(lock_.getWriterOwner().equals(this));
179
180          lock_.release(this);
181          assertTrue(!lock_.isOwner(this));
182       } catch (LockingException e) {
183          fail(e.toString());
184       } catch (TimeoutException e2) {
185          fail(e2.toString());
186       }
187    }
188
189    public void acquireAndRelease2_RWLock() throws InterruptedException JavaDoc {
190       setLevelRW();
191       log("testAcquireAndRelease2 ...");
192       try {
193          lock_.acquireReadLock(this, 10);
194       } catch (LockingException e) {
195          fail(e.toString());
196       } catch (TimeoutException e2) {
197          fail(e2.toString());
198       }
199
200       try {
201          lock_.acquireReadLock(other_, 10); // should succeed
202
} catch (LockingException e) {
203          fail(e.toString());
204       } catch (TimeoutException e2) {
205          fail(e2.toString());
206       }
207
208       try {
209          lock_.acquireWriteLock(other_, 50); // should fail
210
} catch (LockingException e) {
211          fail(e.toString());
212       } catch (TimeoutException e2) {
213          assertTrue(true);
214       }
215
216       lock_.release(this);
217
218       try {
219          lock_.acquireWriteLock(other_, 10); // should succeed
220
} catch (LockingException e) {
221          fail(e.toString());
222       } catch (TimeoutException e2) {
223          fail(e2.toString());
224       }
225
226       lock_.releaseAll();
227    }
228
229
230    public void acquireAndRelease2_SimpleLock() throws InterruptedException JavaDoc {
231       setLevelSerial();
232       log("testAcquireAndRelease2 ...");
233       try {
234          lock_.acquireReadLock(this, 10);
235       } catch (LockingException e) {
236          fail(e.toString());
237       } catch (TimeoutException e2) {
238          fail(e2.toString());
239       }
240
241       try {
242          lock_.acquireReadLock(other_, 10); // should fail
243
fail("Acquire read lock for other. Should fail.");
244       } catch (LockingException e) {
245          fail(e.toString());
246       } catch (TimeoutException e2) {
247          // Expected
248
assertTrue(true);
249       }
250
251       try {
252          lock_.acquireWriteLock(other_, 50); // should fail
253
fail("Acquire read lock for other. Should fail.");
254       } catch (LockingException e) {
255          fail(e.toString());
256       } catch (TimeoutException e2) {
257          assertTrue(true);
258       }
259
260       lock_.release(this);
261
262       try {
263          lock_.acquireWriteLock(other_, 10); // should succeed
264
} catch (LockingException e) {
265          fail(e.toString());
266       } catch (TimeoutException e2) {
267          fail(e2.toString());
268       }
269
270       lock_.releaseAll();
271    }
272
273    public void testThreadedAccess_RWLock() throws Throwable JavaDoc {
274       setLevelRW();
275       log("testThreadedAccess_RWLock ...");
276       final Object JavaDoc o1 = new Object JavaDoc();
277       final Object JavaDoc o2 = new Object JavaDoc();
278
279       System.out.println("");
280       // 1. o1 acquires the lock -- succeeds
281
Thread JavaDoc t1 = new Thread JavaDoc()
282       {
283          public void run()
284          {
285             try {
286                log("o1 acquiring lock");
287                lock_.acquireReadLock(o1, 50);
288                log("o1: OK");
289             } catch (Throwable JavaDoc e) {
290                log("o1: FAIL");
291                thread_ex=e;
292             }
293          }
294       };
295
296       // 2. o2 wants to acquire the lock -- this will fail and o2 will block for 2 secs
297
Thread JavaDoc t2 = new Thread JavaDoc()
298       {
299          public void run()
300          {
301             try {
302                log("o2 acquiring lock");
303                lock_.acquireWriteLock(o2, 2000);
304                log("o2: OK");
305             } catch (Throwable JavaDoc e) {
306                log("o2: FAIL");
307                thread_ex=e;
308             }
309          }
310       };
311
312       // 3. o1 acquires the lock a second time -- succeeds
313
Thread JavaDoc t3 = new Thread JavaDoc()
314       {
315          public void run()
316          {
317             try {
318                log("o1 acquiring lock");
319                lock_.acquireWriteLock(o1, 10);
320                log("o1: OK");
321             } catch (Throwable JavaDoc e) {
322                log("o1: FAIL");
323                thread_ex=e;
324             }
325          }
326       };
327
328       t1.start();
329       t2.start();
330       sleep(1000);
331
332       // o1 must be the owner of the lock
333
assertTrue(lock_.isOwner(o1));
334       sleep(100);
335       // o1 must still be the owner of the lock
336
assertTrue(lock_.isOwner(o1));
337
338       t3.start();
339       sleep(100);
340       // o1 must still be the owner of the lock
341
assertTrue(lock_.isOwner(o1));
342
343       // 4. o1 releases the lock; now o2 will succeed in acquiring the lock
344
log("o1 releasing lock");
345       lock_.release(o1);
346       log("o1: OK");
347
348       sleep(200);
349       //log("o2: " + o2.hashCode() + ", lock_.getOwner()=" + lock_.getOwner());
350
// assertTrue(lock_.isOwner(o2));
351
// lock_.release(o2);
352

353       t1.join(20000);
354       t2.join(20000);
355       t3.join(20000);
356       if(thread_ex != null)
357          throw thread_ex;
358    }
359
360
361    public void testThreadedAccess_SimpleLock() throws Throwable JavaDoc {
362       setLevelSerial();
363       log("testThreadedAccess_SimpleLock() ...");
364       final Object JavaDoc o1 = new Object JavaDoc();
365       final Object JavaDoc o2 = new Object JavaDoc();
366
367       System.out.println("");
368       // 1. o1 acquires the lock -- succeeds
369
Thread JavaDoc t1 = new Thread JavaDoc()
370       {
371          public void run()
372          {
373             try {
374                log("o1 acquiring lock");
375                lock_.acquireReadLock(o1, 50);
376                log("o1: OK");
377             } catch (Throwable JavaDoc e) {
378                log("o1: FAIL");
379                thread_ex=e;
380             }
381          }
382       };
383
384       // 2. o2 wants to acquire the lock -- this will fail and o2 will block for 2 secs
385
Thread JavaDoc t2 = new Thread JavaDoc()
386       {
387          public void run()
388          {
389             try {
390                log("o2 acquiring lock");
391                lock_.acquireWriteLock(o2, 2000);
392                log("o2: OK");
393             } catch (Throwable JavaDoc e) {
394                log("o2: FAIL");
395                thread_ex=e;
396             }
397          }
398       };
399
400       // 3. o1 acquires the lock a second time -- succeeds
401
Thread JavaDoc t3 = new Thread JavaDoc()
402       {
403          public void run()
404          {
405             try {
406                log("o1 acquiring lock");
407                lock_.acquireWriteLock(o1, 10);
408                log("o1: OK");
409             } catch (Throwable JavaDoc e) {
410                log("o1: FAIL");
411                thread_ex=e;
412             }
413          }
414       };
415
416       t1.start();
417       t2.start();
418       sleep(1000);
419
420       // o1 must be the owner of the lock
421
assertTrue(lock_.isOwner(o1));
422       sleep(100);
423       // o1 must still be the owner of the lock
424
assertTrue(lock_.isOwner(o1));
425
426       t3.start();
427       sleep(100);
428       // o1 must still be the owner of the lock
429
assertTrue(lock_.isOwner(o1));
430
431       // 4. o1 releases the lock; now o2 will succeed in acquiring the lock
432
log("o1 releasing lock");
433       lock_.release(o1);
434       log("o1: OK");
435
436       sleep(200);
437       //log("o2: " + o2.hashCode() + ", lock_.getOwner()=" + lock_.getOwner());
438
// assertTrue(lock_.isOwner(o2));
439
// lock_.release(o2);
440

441       t1.join(20000);
442       t2.join(20000);
443       t3.join(20000);
444       if(thread_ex != null)
445          throw thread_ex;
446    }
447
448
449    void sleep(long timeout)
450    {
451       try {
452          Thread.sleep(timeout);
453       } catch (InterruptedException JavaDoc e) {
454       }
455    }
456
457    void log(String JavaDoc msg)
458    {
459 // System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
460
logger_.info("-- [" + Thread.currentThread() + "]: " + msg);
461    }
462
463    public static Test suite()
464    {
465       TestSuite s = new TestSuite(IdentityLockTest.class);
466       return s;
467    }
468
469    public static void main(String JavaDoc[] args)
470    {
471       junit.textui.TestRunner.run(suite());
472    }
473
474 }
475
Popular Tags