1 24 25 package org.objectweb.cjdbc.scenario.standalone.util; 26 27 import java.util.Random ; 28 29 import org.objectweb.cjdbc.common.util.ReadPrioritaryFIFOWriteLock; 30 import org.objectweb.cjdbc.scenario.templates.NoTemplate; 31 32 38 public class ReadPrioritaryFIFOWriteLockTest extends NoTemplate 39 { 40 static final long TIMEOUT = 1000; 42 private ReadPrioritaryFIFOWriteLock lock; 43 private boolean done = false; 46 private int randomTest; 47 private Random r = new Random (); 48 49 52 protected void setUp() 53 { 54 lock = new ReadPrioritaryFIFOWriteLock(); 55 } 56 57 61 public void testNonBlocking() 62 { 63 done = false; 64 65 Thread ti[] = new Thread [4]; 66 67 for (int i = 0; i < 4; i++) 69 { 70 ti[i] = acquireReadDoneTrue(); 71 ti[i].start(); 72 } 73 try 74 { 75 for (int i = 0; i < 4; i++) 76 ti[i].join(TIMEOUT); 77 } 78 catch (InterruptedException ignore) 79 { 80 } 81 assertEquals("Timeout read lock in NonBlocking test", done, true); 82 for (int i = 0; i < 4; i++) 84 lock.releaseRead(); 85 86 done = false; 88 Thread t = acquireWriteDoneTrue(); 89 t.start(); 90 try 91 { 92 t.join(TIMEOUT); 93 } 94 catch (InterruptedException ignore) 95 { 96 } 97 assertEquals("Timeout write lock 1 in NonBlocking test", done, true); 98 lock.releaseWrite(); 100 101 done = false; 103 t = acquireWriteDoneTrue(); 104 t.start(); 105 try 106 { 107 t.join(TIMEOUT); 108 } 109 catch (InterruptedException ignore) 110 { 111 } 112 assertEquals("Timeout write lock 2 in NonBlocking test", done, true); 113 lock.releaseWrite(); 115 116 done = false; 117 t = acquireReadDoneTrue(); 118 t.start(); 119 try 120 { 121 t.join(TIMEOUT); 122 } 123 catch (InterruptedException ignore) 124 { 125 } 126 assertEquals("Timeout read lock 2 in NonBlocking test", done, true); 127 lock.releaseRead(); 129 } 130 131 136 public void testWriteAfterRead() 137 { 138 done = true; 139 Thread ti[] = new Thread [4]; 141 142 for (int i = 0; i < 4; i++) 144 { 145 ti[i] = acquireReadDoneFalse(); 146 ti[i].start(); 147 } 148 try 149 { 150 for (int i = 0; i < 4; i++) 151 ti[i].join(TIMEOUT); 152 } 153 catch (InterruptedException ignore) 154 { 155 } 156 assertEquals("Timeout read lock in NonBlocking test", done, false); 157 158 Thread t = acquireWriteDoneTrue(); 160 t.start(); 161 try 162 { 163 t.join(TIMEOUT); 164 } 165 catch (InterruptedException ignore) 166 { 167 } 168 assertEquals("Timeout write lock in NonBlocking test", done, false); 169 170 for (int i = 0; i < 4; i++) 172 { 173 try 174 { 175 t.join(TIMEOUT); 176 } 177 catch (InterruptedException success) 178 { 179 } 180 assertEquals("Write lock should be blocked after (" + i + " release)", 181 done, false); 182 lock.releaseRead(); 183 } 184 185 try 187 { 188 t.join(TIMEOUT); 189 } 190 catch (InterruptedException e) 191 { 192 throw new RuntimeException (e.getMessage()); 193 } 194 assertEquals("Timeout in WriteAfterRead test", done, true); 195 } 196 197 202 public void testReadAfterWrite() 203 { 204 done = false; 205 try 207 { 208 lock.acquireWrite(); 209 } 210 catch (InterruptedException e) 211 { 212 throw new RuntimeException (e.getMessage()); 213 } 214 215 Thread t1 = acquireReadDoneFalse(); 216 Thread t2 = acquireReadDoneFalse(); 217 Thread t3 = acquireReadDoneTrue(); 218 219 t1.start(); 220 try 221 { 222 t1.join(TIMEOUT); 223 } 224 catch (InterruptedException success) 225 { 226 } 227 assertEquals("Read lock 1 should be blocked", done, false); 228 229 t2.start(); 230 try 231 { 232 t2.join(TIMEOUT); 233 } 234 catch (InterruptedException success) 235 { 236 } 237 assertEquals("Read lock 2 should be blocked", done, false); 238 239 t3.start(); 240 try 241 { 242 t3.join(TIMEOUT); 243 } 244 catch (InterruptedException success) 245 { 246 } 247 assertEquals("Read lock 3 should be blocked", done, false); 248 249 lock.releaseWrite(); 251 252 try 254 { 255 t1.join(TIMEOUT); 256 t2.join(TIMEOUT); 257 t3.join(TIMEOUT); 258 } 259 catch (InterruptedException e) 260 { 261 throw new RuntimeException (e.getMessage()); 262 } 263 assertEquals("Timeout in ReadAfterWrite test", done, true); 264 } 265 266 269 public void testRandom() 270 { 271 int threadNb = 100; 272 Thread t[] = new Thread [threadNb]; 273 274 randomTest = threadNb; 275 276 for (int i = 0; i < threadNb; i++) 277 { 278 if (r.nextGaussian() > 0.5) 279 t[i] = randomAcquireReleaseWrite(); 280 else 281 t[i] = randomAcquireReleaseRead(); 282 } 283 284 for (int i = 0; i < threadNb; i++) 285 t[i].start(); 286 287 try 288 { 289 Thread.sleep(10 * threadNb); 290 } 291 catch (InterruptedException ignore) 292 { 293 } 294 295 try 296 { 297 for (int i = 0; i < threadNb; i++) 298 t[i].join(TIMEOUT); 299 } 300 catch (InterruptedException ignore) 301 { 302 } 303 assertEquals("Bad random test state (" + randomTest 304 + " threads did not finish", randomTest, 0); 305 System.out.println(); 306 } 307 308 private synchronized void randomTestComplete() 309 { 310 randomTest--; 311 } 313 314 317 318 private Thread acquireReadDoneTrue() 319 { 320 return new Thread (new Runnable () 321 { 322 public void run() 323 { 324 try 325 { 326 lock.acquireRead(); 327 } 328 catch (InterruptedException e) 329 { 330 throw new RuntimeException (e.getMessage()); 331 } 332 done = true; 333 } 334 }); 335 } 336 337 private Thread acquireReadDoneFalse() 338 { 339 return new Thread (new Runnable () 340 { 341 public void run() 342 { 343 try 344 { 345 lock.acquireRead(); 346 } 347 catch (InterruptedException e) 348 { 349 throw new RuntimeException (e.getMessage()); 350 } 351 done = false; 352 } 353 }); 354 } 355 356 private Thread acquireWriteDoneTrue() 357 { 358 return new Thread (new Runnable () 359 { 360 public void run() 361 { 362 try 363 { 364 lock.acquireWrite(); 365 } 366 catch (InterruptedException e) 367 { 368 throw new RuntimeException (e.getMessage()); 369 } 370 done = true; 371 } 372 }); 373 } 374 375 private Thread randomAcquireReleaseRead() 376 { 377 return new Thread (new Runnable () 378 { 379 public void run() 380 { 381 try 382 { 383 long timeout = r.nextLong() % TIMEOUT; 384 if (timeout < 0) 385 timeout = -timeout; 386 else if (timeout == 0) 387 timeout = TIMEOUT; 388 Thread.sleep(timeout); 389 390 lock.acquireRead(); 391 assertEquals("Acquired read lock but lock not held by a reader", lock 392 .isReadLocked(), true); 393 assertEquals("Acquired read lock but lock held by a writer", lock 394 .isWriteLocked(), false); 395 System.out.print("R"); 396 lock.releaseRead(); 397 randomTestComplete(); 398 } 399 catch (InterruptedException e) 400 { 401 throw new RuntimeException (e.getMessage()); 402 } 403 } 404 }); 405 } 406 407 private Thread randomAcquireReleaseWrite() 408 { 409 return new Thread (new Runnable () 410 { 411 public void run() 412 { 413 try 414 { 415 long timeout = r.nextLong() % TIMEOUT; 416 if (timeout < 0) 417 timeout = -timeout; 418 else if (timeout == 0) 419 timeout = TIMEOUT; 420 Thread.sleep(timeout); 421 422 lock.acquireWrite(); 423 assertEquals("Acquired write lock but lock not held by a writer", 424 lock.isWriteLocked(), true); 425 assertEquals("Acquired write lock but lock held by a reader", lock 426 .isReadLocked(), false); 427 System.out.print("W"); 428 lock.releaseWrite(); 429 randomTestComplete(); 430 } 431 catch (InterruptedException e) 432 { 433 throw new RuntimeException (e.getMessage()); 434 } 435 } 436 }); 437 } 438 } | Popular Tags |