KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > test > generic > IdentityLockUnitTestCase


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

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

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

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