KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > lockmanager > api > ThreadLockManagerTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.lockmanager.api;
5
6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
7
8 import com.tc.object.lockmanager.impl.ThreadLockManagerImpl;
9 import com.tc.object.tx.WaitInvocation;
10
11 import junit.framework.TestCase;
12
13 public class ThreadLockManagerTest extends TestCase {
14
15   private TestLockManager lm;
16   private ThreadLockManager tlm;
17
18   public void setUp() throws Exception JavaDoc {
19     lm = new TestLockManager();
20     tlm = new ThreadLockManagerImpl(lm);
21   }
22
23   public void testLockIDFor() {
24     String JavaDoc lockName = "fo0";
25     tlm.lockIDFor(lockName);
26     assertEquals(lockName, lm.lockIDForCalls.get(0));
27   }
28
29   public void testLock() throws Exception JavaDoc {
30     final LockID lockID = new LockID("lock");
31     final int lockLevel = LockLevel.WRITE;
32     assertEquals(0, lm.locks.size());
33     tlm.lock(lockID, lockLevel);
34     Object JavaDoc[] args = getLockCallArgs();
35
36     verifyLockArgs(lockID, new ThreadID(1), lockLevel, args);
37
38     tlm.lock(lockID, lockLevel);
39     args = getLockCallArgs();
40     // calling lock in the same thread should result in the same thread id being used to call lock with.
41
verifyLockArgs(lockID, new ThreadID(1), lockLevel, args);
42
43     final CyclicBarrier barrier = new CyclicBarrier(2);
44
45     Runnable JavaDoc getter = new Runnable JavaDoc() {
46       public void run() {
47         tlm.lock(lockID, lockLevel);
48         try {
49           barrier.barrier();
50         } catch (InterruptedException JavaDoc e) {
51           e.printStackTrace();
52         }
53       }
54     };
55
56     new Thread JavaDoc(getter).start();
57     barrier.barrier();
58
59     args = getLockCallArgs();
60     verifyLockArgs(lockID, new ThreadID(2), lockLevel, args);
61
62     new Thread JavaDoc(getter).start();
63     barrier.barrier();
64
65     args = getLockCallArgs();
66     verifyLockArgs(lockID, new ThreadID(3), lockLevel, args);
67   }
68
69   private Object JavaDoc[] getLockCallArgs() {
70     assertEquals(1, lm.locks.size());
71     Object JavaDoc[] args = (Object JavaDoc[]) lm.locks.get(0);
72     lm.locks.clear();
73     return args;
74   }
75
76   private void verifyLockArgs(LockID lockID, ThreadID threadID, int lockLevel, Object JavaDoc[] args) {
77     assertEquals(lockID, args[0]);
78     assertEquals(threadID, args[1]);
79     assertEquals(new Integer JavaDoc(lockLevel), args[2]);
80   }
81
82   public void testWait() throws Exception JavaDoc {
83     final LockID lockID = new LockID("lock");
84     final WaitInvocation call = new WaitInvocation();
85     final Object JavaDoc lockObject = new Object JavaDoc();
86     final WaitListener waitListener = null;
87     tlm.wait(lockID, call, lockObject, waitListener);
88
89     verifyWaitArgs(lockID, new ThreadID(1), call, lockObject, waitListener, getWaitCallArgs());
90
91     // try it again in the same thread. The thread id should remain the same.
92
tlm.wait(lockID, call, lockObject, waitListener);
93     verifyWaitArgs(lockID, new ThreadID(1), call, lockObject, waitListener, getWaitCallArgs());
94
95     final CyclicBarrier barrier = new CyclicBarrier(2);
96
97     Runnable JavaDoc waiter = new Runnable JavaDoc() {
98       public void run() {
99         try {
100           tlm.wait(lockID, call, lockObject, waitListener);
101           barrier.barrier();
102         } catch (InterruptedException JavaDoc e) {
103           e.printStackTrace();
104         }
105       }
106     };
107
108     // try it in a different thread; the thread id should increment.
109
new Thread JavaDoc(waiter).start();
110     barrier.barrier();
111     verifyWaitArgs(lockID, new ThreadID(2), call, lockObject, waitListener, getWaitCallArgs());
112
113     new Thread JavaDoc(waiter).start();
114     barrier.barrier();
115     verifyWaitArgs(lockID, new ThreadID(3), call, lockObject, waitListener, getWaitCallArgs());
116
117   }
118
119   private void verifyWaitArgs(LockID lockID, ThreadID threadID, WaitInvocation call, Object JavaDoc lockObject,
120                               WaitListener waitListener, Object JavaDoc[] args) {
121     assertEquals(lockID, args[0]);
122     assertEquals(threadID, args[1]);
123     assertEquals(call, args[2]);
124     assertEquals(lockObject, args[3]);
125     assertEquals(waitListener, args[4]);
126   }
127
128   private Object JavaDoc[] getWaitCallArgs() {
129     assertEquals(1, lm.waitCalls.size());
130     Object JavaDoc[] args = (Object JavaDoc[]) lm.waitCalls.get(0);
131     lm.waitCalls.clear();
132     return args;
133   }
134
135   public void testNotify() throws Exception JavaDoc {
136     final boolean notifyAll = false;
137     final LockID lockID = new LockID("lock");
138
139     tlm.notify(lockID, notifyAll);
140     verifyNotifyArgs(lockID, new ThreadID(1), notifyAll, getNotifyCallArgs());
141     // the same thread should have the same thread id.
142
tlm.notify(lockID, notifyAll);
143     verifyNotifyArgs(lockID, new ThreadID(1), notifyAll, getNotifyCallArgs());
144
145     final CyclicBarrier barrier = new CyclicBarrier(2);
146     Runnable JavaDoc notifier = new Runnable JavaDoc() {
147
148       public void run() {
149         tlm.notify(lockID, notifyAll);
150         try {
151           barrier.barrier();
152         } catch (InterruptedException JavaDoc e) {
153           e.printStackTrace();
154         }
155       }
156
157     };
158
159     new Thread JavaDoc(notifier).start();
160     barrier.barrier();
161     verifyNotifyArgs(lockID, new ThreadID(2), notifyAll, getNotifyCallArgs());
162
163     // different threads should have different thread ids.
164
new Thread JavaDoc(notifier).start();
165     barrier.barrier();
166     verifyNotifyArgs(lockID, new ThreadID(3), notifyAll, getNotifyCallArgs());
167
168   }
169
170   private void verifyNotifyArgs(LockID lockID, ThreadID threadID, boolean notifyAll, Object JavaDoc[] args) {
171     assertEquals(lockID, args[0]);
172     assertEquals(threadID, args[1]);
173     assertEquals(new Boolean JavaDoc(notifyAll), args[2]);
174   }
175
176   private Object JavaDoc[] getNotifyCallArgs() {
177     assertEquals(1, lm.notifyCalls.size());
178     Object JavaDoc[] args = (Object JavaDoc[]) lm.notifyCalls.get(0);
179     lm.notifyCalls.clear();
180     return args;
181   }
182
183   public void testUnlock() throws Exception JavaDoc {
184     final LockID lockID = new LockID("lock");
185     tlm.unlock(lockID);
186     verifyUnlockArgs(lockID, new ThreadID(1), getUnlockArgs());
187
188     tlm.unlock(lockID);
189     verifyUnlockArgs(lockID, new ThreadID(1), getUnlockArgs());
190
191     final CyclicBarrier barrier = new CyclicBarrier(2);
192     Runnable JavaDoc unlocker = new Runnable JavaDoc() {
193       public void run() {
194         try {
195           tlm.unlock(lockID);
196           barrier.barrier();
197         } catch (InterruptedException JavaDoc e) {
198           e.printStackTrace();
199         }
200       }
201     };
202
203     new Thread JavaDoc(unlocker).start();
204     barrier.barrier();
205     verifyUnlockArgs(lockID, new ThreadID(2), getUnlockArgs());
206
207     new Thread JavaDoc(unlocker).start();
208     barrier.barrier();
209     verifyUnlockArgs(lockID, new ThreadID(3), getUnlockArgs());
210   }
211
212   private void verifyUnlockArgs(LockID lockID, ThreadID threadID, Object JavaDoc[] args) {
213     assertEquals(lockID, args[0]);
214     assertEquals(threadID, args[1]);
215   }
216
217   private Object JavaDoc[] getUnlockArgs() {
218     assertEquals(1, lm.unlockCalls.size());
219     Object JavaDoc[] args = (Object JavaDoc[]) lm.unlockCalls.get(0);
220     lm.unlockCalls.clear();
221     return args;
222   }
223
224 }
225
Popular Tags