KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > 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.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.GlobalTransaction;
16 import org.jboss.cache.NodeSPI;
17 import org.jboss.cache.misc.TestingUtil;
18
19
20 /**
21  * Testing of different locking semantics.
22  *
23  * @author Bela Ban
24  * @author Ben Wang
25  * @version $Revision: 1.8 $
26  */

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

434       t1.join(20000);
435       t2.join(20000);
436       t3.join(20000);
437       if (thread_ex != null)
438       {
439          throw thread_ex;
440       }
441    }
442
443
444    public void testThreadedAccess_SimpleLock() throws Throwable JavaDoc
445    {
446       setLevelSerial();
447       log("testThreadedAccess_SimpleLock() ...");
448       final Object JavaDoc o1 = new Object JavaDoc();
449       final Object JavaDoc o2 = new Object JavaDoc();
450
451       System.out.println("");
452       // 1. o1 acquires the lock -- succeeds
453
Thread JavaDoc t1 = new Thread JavaDoc()
454       {
455          public void run()
456          {
457             try
458             {
459                log("o1 acquiring lock");
460                lock_.acquireReadLock(o1, 50);
461                log("o1: OK");
462             }
463             catch (Throwable JavaDoc e)
464             {
465                log("o1: FAIL");
466                thread_ex = e;
467             }
468          }
469       };
470
471       // 2. o2 wants to acquire the lock -- this will fail and o2 will block for 2 secs
472
Thread JavaDoc t2 = new Thread JavaDoc()
473       {
474          public void run()
475          {
476             try
477             {
478                log("o2 acquiring lock");
479                lock_.acquireWriteLock(o2, 2000);
480                log("o2: OK");
481             }
482             catch (Throwable JavaDoc e)
483             {
484                log("o2: FAIL");
485                thread_ex = e;
486             }
487          }
488       };
489
490       // 3. o1 acquires the lock a second time -- succeeds
491
Thread JavaDoc t3 = new Thread JavaDoc()
492       {
493          public void run()
494          {
495             try
496             {
497                log("o1 acquiring lock");
498                lock_.acquireWriteLock(o1, 10);
499                log("o1: OK");
500             }
501             catch (Throwable JavaDoc e)
502             {
503                log("o1: FAIL");
504                thread_ex = e;
505             }
506          }
507       };
508
509       t1.start();
510       t2.start();
511       TestingUtil.sleepThread(1000);
512
513       // o1 must be the owner of the lock
514
assertTrue(lock_.isOwner(o1));
515       TestingUtil.sleepThread(100);
516       // o1 must still be the owner of the lock
517
assertTrue(lock_.isOwner(o1));
518
519       t3.start();
520       TestingUtil.sleepThread(100);
521       // o1 must still be the owner of the lock
522
assertTrue(lock_.isOwner(o1));
523
524       // 4. o1 releases the lock; now o2 will succeed in acquiring the lock
525
log("o1 releasing lock");
526       lock_.release(o1);
527       log("o1: OK");
528
529       TestingUtil.sleepThread(200);
530       //log("o2: " + o2.hashCode() + ", lock_.getOwner()=" + lock_.getOwner());
531
// assertTrue(lock_.isOwner(o2));
532
// lock_.release(o2);
533

534       t1.join(20000);
535       t2.join(20000);
536       t3.join(20000);
537       if (thread_ex != null)
538       {
539          throw thread_ex;
540       }
541    }
542
543    public void testReadAndReleaseAll()
544    {
545       setLevelRW();
546       log("testReadAndReleaseAll() ...");
547       final Object JavaDoc o1 = new Object JavaDoc();
548       final Object JavaDoc o2 = new Object JavaDoc();
549       System.out.println("");
550
551       // 1. o1 acquires the lock -- succeeds
552
try
553       {
554          log("o1: acquiring");
555          lock_.acquireReadLock(o1, 50);
556          log("o1: OK");
557          log("o2: acquiring");
558          lock_.acquireReadLock(o2, 50);
559          log("o2: OK");
560       }
561       catch (Throwable JavaDoc t)
562       {
563          log("read lock: FAIL");
564          fail(t.getMessage());
565       }
566
567       Thread JavaDoc t1 = new Thread JavaDoc()
568       {
569          public void run()
570          {
571             try
572             {
573                log("calling releaseAll()");
574                lock_.releaseAll();
575                log("releaseAll(): OK");
576             }
577             catch (Throwable JavaDoc e)
578             {
579                log("releaseAll(): FAIL");
580                thread_ex = e;
581             }
582          }
583       };
584
585       try
586       {
587          t1.setDaemon(true);
588          t1.start();
589          TestingUtil.sleepThread(1000);
590
591          assertFalse("Lock map cleared", lock_.isReadLocked());
592       }
593       finally
594       {
595          // Manually release the locks so tearDown() will not fail
596
// if there is a problem with releaseAll()
597
lock_.release(o1);
598          lock_.release(o2);
599       }
600    }
601
602    public void testWriteAndReleaseAll()
603    {
604       setLevelSerial();
605       log("testWriteAndReleaseAll() ...");
606       final Object JavaDoc o1 = new Object JavaDoc();
607       System.out.println("");
608
609       // 1. o1 acquires the lock -- succeeds
610
try
611       {
612          log("o1: acquiring");
613          lock_.acquireWriteLock(o1, 50);
614          log("o1: OK");
615       }
616       catch (Throwable JavaDoc t)
617       {
618          log("write lock: FAIL");
619          fail(t.getMessage());
620       }
621
622       Thread JavaDoc t1 = new Thread JavaDoc()
623       {
624          public void run()
625          {
626             try
627             {
628                log("calling releaseAll()");
629                lock_.releaseAll();
630                log("releaseAll(): OK");
631             }
632             catch (Throwable JavaDoc e)
633             {
634                log("releaseAll(): FAIL");
635                thread_ex = e;
636             }
637          }
638       };
639
640       try
641       {
642          t1.setDaemon(true);
643          t1.start();
644          TestingUtil.sleepThread(1000);
645
646          assertFalse("Lock map cleared", lock_.isReadLocked());
647       }
648       finally
649       {
650          // Manually release the lock so tearDown() will not fail
651
// if there is a problem with releaseAll()
652
lock_.release(o1);
653       }
654    }
655
656    void log(String JavaDoc msg)
657    {
658       // System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
659
logger_.info("-- [" + Thread.currentThread() + "]: " + msg);
660    }
661
662    public static Test suite()
663    {
664       TestSuite s = new TestSuite(IdentityLockTest.class);
665       return s;
666    }
667
668    public static void main(String JavaDoc[] args)
669    {
670       junit.textui.TestRunner.run(suite());
671    }
672
673 }
674
Popular Tags