1 21 22 package com.rift.coad.util.lock; 23 24 import junit.framework.*; 25 import java.util.Map ; 26 import java.util.concurrent.ConcurrentHashMap ; 27 import org.apache.log4j.Logger; 28 import org.apache.log4j.BasicConfigurator; 29 30 35 public class ObjectLockFactoryTest extends TestCase { 36 37 private String lockObject = "lock object"; 38 private String lockObject2 = "lock object2"; 39 private boolean hasLock = false; 40 private long lockCount = 0; 41 42 public ObjectLockFactoryTest(String testName) { 43 super(testName); 44 BasicConfigurator.configure(); 45 } 46 47 protected void setUp() throws Exception { 48 } 49 50 protected void tearDown() throws Exception { 51 } 52 53 56 public void testObjectLockFactory() throws Exception { 57 System.out.println("getInstance"); 58 59 try { 60 ObjectLockFactory.getInstance(); 61 fail("Could retrieve an reference without calling init"); 62 } catch (LockException ex) { 63 System.out.println(ex.getMessage()); 64 } 65 66 67 ObjectLockFactory.init(); 68 69 ObjectLockFactory expResult = ObjectLockFactory.getInstance(); 70 ObjectLockFactory result = ObjectLockFactory.getInstance(); 71 assertEquals(expResult, result); 72 73 System.out.println("Aquire write lock"); 74 LockRef ref = result.acquireWriteLock(lockObject); 75 System.out.println("Test lock"); 76 assertEquals(ref.getKey(),lockObject); 77 System.out.println("Test thread id"); 78 assertEquals(ref.getThreadId(),Thread.currentThread().getId()); 79 80 System.out.println("Aquire lock"); 81 ref = result.acquireWriteLock(lockObject,"test"); 82 83 System.out.println("Test the lock"); 84 assertEquals(ref.getKey(),lockObject); 85 assertEquals(ref.getThreadId(),Thread.currentThread().getId()); 86 assertEquals(ref.getLockName(),"test"); 87 88 Thread testThread = new Thread (new Runnable () { 89 public void run() { 90 try { 91 LockRef ref2 = ObjectLockFactory.getInstance(). 92 acquireWriteLock(lockObject,"test2"); 93 hasLock = true; 94 ref2.release(); 95 } catch (Exception ex) { 96 System.out.println("Failed to aquire the lock : " + 97 ex.getMessage()); 98 ex.printStackTrace(System.out); 99 } 100 } 101 }); 102 testThread.start(); 103 104 System.out.println("Test a seperate thread"); 105 Thread.sleep(1000); 106 if (hasLock == true) { 107 fail("Was able to get lock"); 108 } 109 110 System.out.println("Attempt to release"); 111 ref.release(); 112 ref.release(); 113 114 Thread.sleep(500); 115 if (hasLock == false) { 116 fail("Was not able to get lock"); 117 } 118 119 System.out.println("Aquire new lock"); 120 ref = result.acquireWriteLock(lockObject,"test2"); 121 hasLock = false; 122 System.out.println("Create second thread"); 123 testThread = new Thread (new Runnable () { 124 public void run() { 125 try { 126 LockRef ref2 = ObjectLockFactory.getInstance(). 127 acquireWriteLock(lockObject, 128 ObjectLockFactory.WAIT_ON_THREAD); 129 hasLock = true; 130 ref2.release(); 131 } catch (Exception ex) { 132 System.out.println("Failed to aquire the lock : " + 133 ex.getMessage()); 134 ex.printStackTrace(System.out); 135 } 136 } 137 }); 138 testThread.start(); 139 System.out.println("Sleep"); 140 Thread.sleep(1000); 141 if (hasLock == true) { 142 fail("Was able to get lock"); 143 } 144 145 System.out.println("Release"); 146 ref.release(); 147 148 Thread.sleep(500); 149 if (hasLock == true) { 150 fail("Was able to get lock"); 151 } 152 153 ref = result.acquireWriteLock(lockObject,"test2"); 155 hasLock = false; 156 testThread = new Thread (new Runnable () { 157 public void run() { 158 try { 159 LockRef ref2 = ObjectLockFactory.getInstance(). 160 acquireWriteLock(lockObject,"test2", 161 ObjectLockFactory.WAIT_ON_NAMED); 162 hasLock = true; 163 ref2.release(); 164 } catch (Exception ex) { 165 System.out.println("Failed to aquire the lock : " + 166 ex.getMessage()); 167 ex.printStackTrace(System.out); 168 } 169 } 170 }); 171 testThread.start(); 172 173 Thread.sleep(1000); 174 if (hasLock == false) { 175 fail("Was not able to acquire the lock"); 176 } 177 178 ref.release(); 179 180 System.out.println("Aquire two different locks"); 182 ref = result.acquireWriteLock(lockObject,"test2"); 183 hasLock = false; 184 testThread = new Thread (new Runnable () { 185 public void run() { 186 try { 187 LockRef ref2 = ObjectLockFactory.getInstance(). 188 acquireWriteLock(lockObject2,"test2", 189 ObjectLockFactory.WAIT_ON_NAMED); 190 hasLock = true; 191 ref2.release(); 192 } catch (Exception ex) { 193 System.out.println("Failed to aquire the lock : " + 194 ex.getMessage()); 195 ex.printStackTrace(System.out); 196 } 197 } 198 }); 199 testThread.start(); 200 201 Thread.sleep(1000); 202 if (hasLock == false) { 203 fail("Was not able to acquire the lock"); 204 } 205 206 ref.release(); 207 208 Thread readThread1 = new Thread (new Runnable () { 210 public void run() { 211 try { 212 LockRef ref2 = ObjectLockFactory.getInstance(). 213 acquireReadLock(lockObject); 214 lockCount++; 215 Thread.sleep(1000); 216 System.out.println("Read1: Release lock"); 217 ref2.release(); 218 System.out.println("Read1: Released lock"); 219 } catch (Exception ex) { 220 System.out.println("Failed to aquire the lock : " + 221 ex.getMessage()); 222 ex.printStackTrace(System.out); 223 } 224 } 225 }); 226 227 Thread readThread2 = new Thread (new Runnable () { 228 public void run() { 229 try { 230 LockRef ref2 = ObjectLockFactory.getInstance(). 231 acquireReadLock(lockObject); 232 lockCount++; 233 Thread.sleep(1000); 234 System.out.println("Read2: Release lock"); 235 ref2.release(); 236 System.out.println("Read2: Released lock"); 237 } catch (Exception ex) { 238 System.out.println("Failed to aquire the lock : " + 239 ex.getMessage()); 240 ex.printStackTrace(System.out); 241 } 242 } 243 }); 244 245 readThread1.start(); 246 readThread2.start(); 247 248 Thread.sleep(300); 249 if (lockCount != 2) { 250 fail("Aquire the lock"); 251 } 252 253 System.out.println("Wait for lock"); 254 ref = result.acquireWriteLock(lockObject); 255 System.out.println("1: Acquired lock"); 256 257 readThread1 = new Thread (new Runnable () { 259 public void run() { 260 try { 261 System.out.println("Read1: Acquired lock"); 262 LockRef ref2 = ObjectLockFactory.getInstance(). 263 acquireReadLock(lockObject); 264 System.out.println("Read1: Acquired lock"); 265 lockCount++; 266 Thread.sleep(1000); 267 ref2.release(); 268 System.out.println("Released lock"); 269 } catch (Exception ex) { 270 System.out.println("Failed to aquire the lock : " + 271 ex.getMessage()); 272 ex.printStackTrace(System.out); 273 } 274 } 275 }); 276 277 readThread2 = new Thread (new Runnable () { 278 public void run() { 279 try { 280 System.out.println("Read2: Acquire lock"); 281 LockRef ref2 = ObjectLockFactory.getInstance(). 282 acquireReadLock(lockObject); 283 System.out.println("Read2: Acquired lock"); 284 lockCount++; 285 Thread.sleep(1000); 286 ref2.release(); 287 System.out.println("Released lock"); 288 } catch (Exception ex) { 289 System.out.println("Failed to aquire the lock : " + 290 ex.getMessage()); 291 ex.printStackTrace(System.out); 292 } 293 } 294 }); 295 296 297 lockCount = 0; 298 readThread1.start(); 299 readThread2.start(); 300 301 Thread.sleep(300); 302 if (lockCount != 0) { 303 fail("The could aquire the lock"); 304 } 305 306 ref.release(); 307 308 Thread.sleep(2000); 309 if (lockCount != 2) { 310 fail("The could not acquire the lock : " + lockCount); 311 } 312 313 readThread1 = new Thread (new Runnable () { 315 public void run() { 316 try { 317 LockRef ref2 = ObjectLockFactory.getInstance(). 318 acquireReadLock(lockObject,"test"); 319 lockCount++; 320 Thread.sleep(1000); 321 ref2.release(); 322 System.out.println("Released lock"); 323 } catch (Exception ex) { 324 System.out.println("Failed to aquire the lock : " + 325 ex.getMessage()); 326 ex.printStackTrace(System.out); 327 } 328 } 329 }); 330 331 readThread2 = new Thread (new Runnable () { 332 public void run() { 333 try { 334 LockRef ref2 = ObjectLockFactory.getInstance(). 335 acquireReadLock(lockObject,"test"); 336 lockCount++; 337 Thread.sleep(1000); 338 ref2.release(); 339 System.out.println("Released lock"); 340 } catch (Exception ex) { 341 System.out.println("Failed to aquire the lock : " + 342 ex.getMessage()); 343 ex.printStackTrace(System.out); 344 } 345 } 346 }); 347 348 lockCount = 0; 349 readThread1.start(); 350 readThread2.start(); 351 352 Thread.sleep(300); 353 if (lockCount != 2) { 354 fail("The could not aquire the lock"); 355 } 356 357 System.out.println("Wait for lock"); 358 ref = result.acquireWriteLock(lockObject); 359 System.out.println("Acquired lock"); 360 361 readThread1 = new Thread (new Runnable () { 363 public void run() { 364 try { 365 LockRef ref2 = ObjectLockFactory.getInstance(). 366 acquireReadLock(lockObject,"test"); 367 lockCount++; 368 Thread.sleep(1000); 369 ref2.release(); 370 System.out.println("Released lock"); 371 } catch (Exception ex) { 372 System.out.println("Failed to aquire the lock : " + 373 ex.getMessage()); 374 ex.printStackTrace(System.out); 375 } 376 } 377 }); 378 379 readThread2 = new Thread (new Runnable () { 380 public void run() { 381 try { 382 LockRef ref2 = ObjectLockFactory.getInstance(). 383 acquireReadLock(lockObject,"test"); 384 lockCount++; 385 Thread.sleep(1000); 386 ref2.release(); 387 System.out.println("Released lock"); 388 } catch (Exception ex) { 389 System.out.println("Failed to aquire the lock : " + 390 ex.getMessage()); 391 ex.printStackTrace(System.out); 392 } 393 } 394 }); 395 396 lockCount = 0; 397 readThread1.start(); 398 readThread2.start(); 399 400 Thread.sleep(300); 401 if (lockCount != 0) { 402 fail("The could aquire the lock"); 403 } 404 405 ref.release(); 406 407 Thread.sleep(2000); 408 if (lockCount != 2) { 409 fail("The could not acquire the lock"); 410 } 411 412 413 ObjectLockFactory.fin(); 414 try { 415 ObjectLockFactory.getInstance(); 416 fail("Could retrieve an reference without after calling fin"); 417 } catch (LockException ex) { 418 System.out.println(ex.getMessage()); 419 } 420 } 421 422 423 } 424 | Popular Tags |