1 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 { 19 lm = new TestLockManager(); 20 tlm = new ThreadLockManagerImpl(lm); 21 } 22 23 public void testLockIDFor() { 24 String lockName = "fo0"; 25 tlm.lockIDFor(lockName); 26 assertEquals(lockName, lm.lockIDForCalls.get(0)); 27 } 28 29 public void testLock() throws Exception { 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 [] args = getLockCallArgs(); 35 36 verifyLockArgs(lockID, new ThreadID(1), lockLevel, args); 37 38 tlm.lock(lockID, lockLevel); 39 args = getLockCallArgs(); 40 verifyLockArgs(lockID, new ThreadID(1), lockLevel, args); 42 43 final CyclicBarrier barrier = new CyclicBarrier(2); 44 45 Runnable getter = new Runnable () { 46 public void run() { 47 tlm.lock(lockID, lockLevel); 48 try { 49 barrier.barrier(); 50 } catch (InterruptedException e) { 51 e.printStackTrace(); 52 } 53 } 54 }; 55 56 new Thread (getter).start(); 57 barrier.barrier(); 58 59 args = getLockCallArgs(); 60 verifyLockArgs(lockID, new ThreadID(2), lockLevel, args); 61 62 new Thread (getter).start(); 63 barrier.barrier(); 64 65 args = getLockCallArgs(); 66 verifyLockArgs(lockID, new ThreadID(3), lockLevel, args); 67 } 68 69 private Object [] getLockCallArgs() { 70 assertEquals(1, lm.locks.size()); 71 Object [] args = (Object []) lm.locks.get(0); 72 lm.locks.clear(); 73 return args; 74 } 75 76 private void verifyLockArgs(LockID lockID, ThreadID threadID, int lockLevel, Object [] args) { 77 assertEquals(lockID, args[0]); 78 assertEquals(threadID, args[1]); 79 assertEquals(new Integer (lockLevel), args[2]); 80 } 81 82 public void testWait() throws Exception { 83 final LockID lockID = new LockID("lock"); 84 final WaitInvocation call = new WaitInvocation(); 85 final Object lockObject = new Object (); 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 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 waiter = new Runnable () { 98 public void run() { 99 try { 100 tlm.wait(lockID, call, lockObject, waitListener); 101 barrier.barrier(); 102 } catch (InterruptedException e) { 103 e.printStackTrace(); 104 } 105 } 106 }; 107 108 new Thread (waiter).start(); 110 barrier.barrier(); 111 verifyWaitArgs(lockID, new ThreadID(2), call, lockObject, waitListener, getWaitCallArgs()); 112 113 new Thread (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 lockObject, 120 WaitListener waitListener, Object [] 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 [] getWaitCallArgs() { 129 assertEquals(1, lm.waitCalls.size()); 130 Object [] args = (Object []) lm.waitCalls.get(0); 131 lm.waitCalls.clear(); 132 return args; 133 } 134 135 public void testNotify() throws Exception { 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 tlm.notify(lockID, notifyAll); 143 verifyNotifyArgs(lockID, new ThreadID(1), notifyAll, getNotifyCallArgs()); 144 145 final CyclicBarrier barrier = new CyclicBarrier(2); 146 Runnable notifier = new Runnable () { 147 148 public void run() { 149 tlm.notify(lockID, notifyAll); 150 try { 151 barrier.barrier(); 152 } catch (InterruptedException e) { 153 e.printStackTrace(); 154 } 155 } 156 157 }; 158 159 new Thread (notifier).start(); 160 barrier.barrier(); 161 verifyNotifyArgs(lockID, new ThreadID(2), notifyAll, getNotifyCallArgs()); 162 163 new Thread (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 [] args) { 171 assertEquals(lockID, args[0]); 172 assertEquals(threadID, args[1]); 173 assertEquals(new Boolean (notifyAll), args[2]); 174 } 175 176 private Object [] getNotifyCallArgs() { 177 assertEquals(1, lm.notifyCalls.size()); 178 Object [] args = (Object []) lm.notifyCalls.get(0); 179 lm.notifyCalls.clear(); 180 return args; 181 } 182 183 public void testUnlock() throws Exception { 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 unlocker = new Runnable () { 193 public void run() { 194 try { 195 tlm.unlock(lockID); 196 barrier.barrier(); 197 } catch (InterruptedException e) { 198 e.printStackTrace(); 199 } 200 } 201 }; 202 203 new Thread (unlocker).start(); 204 barrier.barrier(); 205 verifyUnlockArgs(lockID, new ThreadID(2), getUnlockArgs()); 206 207 new Thread (unlocker).start(); 208 barrier.barrier(); 209 verifyUnlockArgs(lockID, new ThreadID(3), getUnlockArgs()); 210 } 211 212 private void verifyUnlockArgs(LockID lockID, ThreadID threadID, Object [] args) { 213 assertEquals(lockID, args[0]); 214 assertEquals(threadID, args[1]); 215 } 216 217 private Object [] getUnlockArgs() { 218 assertEquals(1, lm.unlockCalls.size()); 219 Object [] args = (Object []) lm.unlockCalls.get(0); 220 lm.unlockCalls.clear(); 221 return args; 222 } 223 224 } 225 | Popular Tags |