01: package org.columba.core.util;
02:
03: import junit.framework.TestCase;
04:
05: import org.columba.core.base.Lock;
06:
07: public class LockTest extends TestCase {
08:
09: public boolean testBool;
10: public int testInt;
11:
12: public void test1() throws InterruptedException {
13: final Lock testLock = new Lock();
14: testBool = false;
15:
16: assertTrue(testLock.tryToGetLock(this ));
17:
18: Thread t = new Thread() {
19: public void run() {
20: testLock.getLock(this );
21: assertTrue(testBool);
22: }
23: };
24:
25: t.start();
26:
27: Thread.sleep(100);
28:
29: testBool = true;
30: testLock.release(this );
31:
32: t.join(100);
33: assertFalse(t.isAlive());
34:
35: assertFalse(testLock.tryToGetLock(this ));
36: testLock.release(t);
37: assertTrue(testLock.tryToGetLock(this ));
38: }
39:
40: public void test2() throws InterruptedException {
41: final Lock testLock = new Lock();
42: testInt = 0;
43:
44: assertTrue(testLock.tryToGetLock(this ));
45:
46: Thread t1 = new Thread() {
47: public void run() {
48: testLock.getLock(this );
49: testInt++;
50: testLock.release(this );
51: }
52: };
53:
54: Thread t2 = new Thread() {
55: public void run() {
56: testLock.getLock(this );
57: testInt++;
58: testLock.release(this );
59: }
60: };
61:
62: t1.start();
63: t2.start();
64:
65: Thread.sleep(100);
66:
67: testLock.release(this );
68:
69: t1.join(100);
70: t2.join(100);
71:
72: assertEquals(2, testInt);
73: }
74:
75: }
|