1 21 22 package org.apache.derbyTesting.unitTests.services; 23 24 import org.apache.derbyTesting.unitTests.harness.T_MultiIterations; 25 import org.apache.derbyTesting.unitTests.harness.T_Fail; 26 27 import org.apache.derby.iapi.services.locks.*; 28 29 import org.apache.derby.iapi.services.monitor.Monitor; 30 31 import org.apache.derby.iapi.reference.SQLState; 32 33 import org.apache.derby.iapi.error.StandardException; 34 35 41 42 public class T_LockFactory extends T_MultiIterations 43 { 44 protected final static int ITERATIONS = 100; 46 protected LockFactory lf; 47 48 public T_LockFactory() { 49 super(); 50 } 51 52 55 56 protected String getModuleToTestProtocolName() { 57 58 return org.apache.derby.iapi.reference.Module.LockFactory; 59 } 60 61 67 protected void setupTest() throws T_Fail { 68 69 try { 70 lf = (LockFactory) Monitor.startSystemModule(getModuleToTestProtocolName()); 71 } catch (StandardException mse) { 72 throw T_Fail.exceptionFail(mse); 73 } 74 if (lf == null) { 75 throw T_Fail.testFailMsg(getModuleToTestProtocolName() + " module not started."); 76 } 77 } 78 79 83 protected void runTestSet() throws T_Fail { 84 85 try { 87 88 S001(); 89 S002(); 90 S003(); 91 S004(); 92 S005(); 93 S007(); 94 S008(); 95 96 M001(); 97 M002(); 98 M003(); 99 M004(); 100 101 102 } catch (StandardException se) { 103 104 throw T_Fail.exceptionFail(se); 105 106 } 107 } 108 109 112 113 122 void S001() throws StandardException, T_Fail { 123 124 Object cs = new Object (); Object g0 = new Object (); Lockable l0 = new T_L1(); 128 int count; 129 130 checkLockCount(cs, 0); 132 133 lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER); 135 checkLockCount(cs, 1); 136 count = lf.unlock(cs, g0, l0, null); 137 if (count != 1) 138 throw T_Fail.testFailMsg("invalid unlock count, expected 1, got " + count); 139 140 checkLockCount(cs, 0); 142 143 lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER); 145 lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER); 146 checkLockCount(cs, 2); 147 lf.unlock(cs, g0, l0, null); 148 lf.unlock(cs, g0, l0, null); 149 150 checkLockCount(cs, 0); 152 153 lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER); 155 lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER); 156 lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER); 157 checkLockCount(cs, 3); 158 lf.unlockGroup(cs, g0); 159 160 checkLockCount(cs, 0); 162 163 164 lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER); 166 lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER); 167 lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER); 168 checkLockCount(cs, 3); 169 170 lf.unlock(cs, g0, l0, null); 171 checkLockCount(cs, 2); 172 173 lf.unlock(cs, g0, l0, null); 174 checkLockCount(cs, 1); 175 176 lf.unlock(cs, g0, l0, null); 177 checkLockCount(cs, 0); 178 179 lf.lockObject(cs, g0, l0, null, 1000 ); 181 checkLockCount(cs, 1); 182 count = lf.unlock(cs, g0, l0, null); 183 if (count != 1) 184 throw T_Fail.testFailMsg("invalid unlock count, expected 1, got " + count); 185 186 PASS("S001"); 187 } 188 189 198 199 void S002() throws StandardException, T_Fail { 200 201 Object cs = new Object (); Object g0 = new Object (); Object g1 = new Object (); 204 Lockable l0 = new T_L1(); 206 int count; 207 208 checkLockCount(cs, 0); 210 211 lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER); 213 lf.lockObject(cs, g1, l0, null, C_LockFactory.WAIT_FOREVER); 214 checkLockGroupCount(cs, g0, 1); 215 checkLockGroupCount(cs, g1, 1); 216 checkLockCount(cs, 2); 217 218 count = lf.unlock(cs, g0, l0, null); 219 if (count != 1) 220 throw T_Fail.testFailMsg("invalid unlock count, expected 1, got " + count); 221 checkLockGroupCount(cs, g0, 0); 222 checkLockGroupCount(cs, g1, 1); 223 checkLockCount(cs, 1); 224 225 count = lf.unlock(cs, g1, l0, null); 226 if (count != 1) 227 throw T_Fail.testFailMsg("invalid unlock count, expected 1, got " + count); 228 checkLockGroupCount(cs, g0, 0); 229 checkLockGroupCount(cs, g1, 0); 230 231 232 checkLockCount(cs, 0); 234 235 lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER); 237 lf.lockObject(cs, g1, l0, null, C_LockFactory.WAIT_FOREVER); 238 checkLockCount(cs, 2); 239 240 lf.unlockGroup(cs, g1); 241 checkLockGroupCount(cs, g0, 1); 242 checkLockGroupCount(cs, g1, 0); 243 checkLockCount(cs, 1); 244 245 lf.unlockGroup(cs, g0); 246 checkLockGroupCount(cs, g0, 0); 247 checkLockGroupCount(cs, g1, 0); 248 249 checkLockCount(cs, 0); 251 252 PASS("S002"); 253 } 254 255 265 266 void S003() throws StandardException, T_Fail { 267 268 Object cs = new Object (); Object g0 = new Object (); Object g1 = new Object (); 271 Lockable l0 = new T_L1(); Lockable l1 = new T_L1(); 273 Lockable l2 = new T_L1(); 274 275 int count; 276 277 checkLockCount(cs, 0); 279 280 lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER); 282 lf.lockObject(cs, g1, l0, null, C_LockFactory.WAIT_FOREVER); 283 lf.lockObject(cs, g1, l1, null, C_LockFactory.WAIT_FOREVER); 284 lf.lockObject(cs, g1, l2, null, C_LockFactory.WAIT_FOREVER); 285 286 checkLockGroupCount(cs, g0, 1); 287 checkLockGroupCount(cs, g1, 3); 288 checkLockCount(cs, 4); 289 290 if (lf.anyoneBlocked()) 292 throw T_Fail.testFailMsg("anyoneBlocked() returned true on a set of private locks"); 293 294 lf.unlock(cs, g1, l1, null); 295 checkLockGroupCount(cs, g0, 1); 296 checkLockGroupCount(cs, g1, 2); 297 checkLockCount(cs, 3); 298 299 lf.unlockGroup(cs, g1); 300 checkLockGroupCount(cs, g0, 1); 301 checkLockGroupCount(cs, g1, 0); 302 checkLockCount(cs, 1); 303 304 lf.unlockGroup(cs, g0); 305 checkLockGroupCount(cs, g0, 0); 306 checkLockGroupCount(cs, g1, 0); 307 308 checkLockCount(cs, 0); 310 311 PASS("S003"); 312 } 313 314 323 324 void S004() throws StandardException, T_Fail { 325 326 Object cs = new Object (); Object g0 = new Object (); Object g1 = new Object (); 329 Object g2 = new Object (); 330 Lockable l0 = new T_L1(); Lockable l1 = new T_L1(); 332 Lockable l2 = new T_L1(); 333 334 int count = 0; 335 336 checkLockCount(cs, 0); 338 339 lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER); 341 lf.lockObject(cs, g1, l0, null, C_LockFactory.WAIT_FOREVER); 342 lf.lockObject(cs, g1, l1, null, C_LockFactory.WAIT_FOREVER); 343 lf.lockObject(cs, g1, l2, null, C_LockFactory.WAIT_FOREVER); 344 345 checkLockGroupCount(cs, g0, 1); 346 checkLockGroupCount(cs, g1, 3); 347 checkLockCount(cs, 4); 348 349 lf.transfer(cs, g0, g1); 350 checkLockGroupCount(cs, g0, 0); 351 checkLockGroupCount(cs, g1, 4); 352 checkLockCount(cs, 4); 353 354 lf.transfer(cs, g0, g2); 356 checkLockGroupCount(cs, g0, 0); 357 checkLockGroupCount(cs, g1, 4); 358 checkLockGroupCount(cs, g2, 0); 359 checkLockCount(cs, 4); 360 361 lf.lockObject(cs, g2, l0, null, C_LockFactory.WAIT_FOREVER); 362 checkLockGroupCount(cs, g2, 1); 363 checkLockCount(cs, 5); 364 365 lf.transfer(cs, g1, g2); 366 checkLockGroupCount(cs, g1, 0); 367 checkLockGroupCount(cs, g2, 5); 368 checkLockCount(cs, 5); 369 370 lf.transfer(cs, g2, g1); 371 checkLockGroupCount(cs, g1, 5); 372 checkLockGroupCount(cs, g2, 0); 373 checkLockCount(cs, 5); 374 375 376 lf.unlockGroup(cs, g2); 377 checkLockGroupCount(cs, g1, 5); 378 checkLockGroupCount(cs, g2, 0); 379 checkLockCount(cs, 5); 380 381 lf.unlockGroup(cs, g1); 382 checkLockGroupCount(cs, g1, 0); 383 checkLockGroupCount(cs, g2, 0); 384 385 checkLockCount(cs, 0); 387 388 PASS("S004"); 389 } 390 391 400 void S005() throws StandardException, T_Fail { 401 402 Object cs0 = new Object (); Object cs1 = new Object (); 405 Object g0 = new Object (); Object g1 = new Object (); Lockable l0 = new T_L1(); 408 Lockable l1 = new T_L1(); 409 Lockable l2 = new T_L1(); 410 411 int count; 412 413 checkLockCount(cs0, 0); 415 checkLockCount(cs1, 0); 416 417 lf.lockObject(cs0, g0, l0, null, C_LockFactory.WAIT_FOREVER); 418 lf.lockObject(cs1, g1, l1, null, C_LockFactory.WAIT_FOREVER); 419 checkLockCount(cs0, 1); 420 checkLockCount(cs1, 1); 421 422 lf.lockObject(cs0, g0, l2, null, C_LockFactory.WAIT_FOREVER); 423 checkLockCount(cs0, 2); 424 checkLockCount(cs1, 1); 425 426 try { 428 lf.lockObject(cs1, g1, l2, null, 200 ); 429 throw T_Fail.testFailMsg("lock succeeded on already locked object"); 430 } 431 catch (StandardException lfe) { 432 if (!lfe.getMessageId().equals(SQLState.LOCK_TIMEOUT)) { 434 throw lfe; 435 } 436 checkLockCount(cs0, 2); 437 checkLockCount(cs1, 1); 438 439 } 440 441 lf.unlock(cs0, g0, l2, null); 443 checkLockCount(cs0, 1); 444 checkLockCount(cs1, 1); 445 lf.lockObject(cs1, g1, l2, null, C_LockFactory.WAIT_FOREVER); 446 checkLockCount(cs0, 1); 447 checkLockCount(cs1, 2); 448 449 lf.unlockGroup(cs0, g0); 450 lf.unlockGroup(cs1, g1); 451 checkLockCount(cs0, 0); 452 checkLockCount(cs1, 0); 453 454 PASS("S005"); 455 456 457 458 } 459 460 461 471 void S007() throws StandardException, T_Fail { 472 473 Object cs = new Object (); Object g0 = new Object (); Object g1 = new Object (); Lockable l0 = new T_L1(); 477 478 int count; 479 480 checkLockCount(cs, 0); 482 checkLockGroupCount(cs, g0, 0); 483 484 lf.unlockGroup(cs, g0); 485 lf.unlockGroup(cs, cs); 486 lf.unlock(cs, g0, l0, null); 487 488 lf.transfer(cs, g0, g1); 489 lf.transfer(cs, g1, g0); 490 491 if (lf.anyoneBlocked()) 492 throw T_Fail.testFailMsg("anyoneBlocked() returned true on an empty space"); 493 494 checkLockCount(cs, 0); 496 checkLockGroupCount(cs, g0, 0); 497 checkLockGroupCount(cs, g1, 0); 498 499 PASS("S007"); 500 } 501 502 511 void S008() throws StandardException, T_Fail { 512 513 Object cs0 = new Object (); Object cs1 = new Object (); 516 Object g0 = new Object (); 517 Object g1 = new Object (); 518 519 T_L1 page = new T_L1(); 520 Lockable rA = new T_L1(); 521 Lockable rB = new T_L1(); 522 523 int count; 524 525 lf.latchObject(cs0, page, null, C_LockFactory.WAIT_FOREVER); 527 lf.lockObject(g0, rA, null, C_LockFactory.WAIT_FOREVER, page.latch); lf.unlatch(page.latch); 529 530 lf.latchObject(cs1, page, null, C_LockFactory.WAIT_FOREVER); 531 lf.lockObject(g1, rB, null, C_LockFactory.WAIT_FOREVER, page.latch); 533 checkLockCount(cs0, 1); 534 checkLockCount(cs1, 2); 535 536 try { 539 lf.lockObject(g1, rA, null, 5000, page.latch); 540 throw T_Fail.testFailMsg("lock succeeded on already locked object"); 541 } 542 catch (StandardException lfe) { 543 if (!lfe.getMessageId().equals(SQLState.LOCK_TIMEOUT)) { 545 throw lfe; 546 } 547 checkLockCount(cs0, 1); 548 checkLockCount(cs1, 1); 549 } 550 551 try { 552 lf.latchObject(cs0, page, null, 5000); 554 throw T_Fail.testFailMsg("latch succeeded on already latch object"); 555 } 556 catch (StandardException lfe) { 557 if (!lfe.getMessageId().equals(SQLState.LOCK_TIMEOUT)) { 559 throw lfe; 560 } 561 } 562 563 lf.unlatch(page.latch); 564 565 lf.unlock(cs0, g0, rA, null); 566 lf.unlock(cs1, g0, rB, null); 567 568 PASS("S008"); 569 570 571 } 572 573 574 575 578 579 591 592 void M001() throws StandardException, T_Fail { 593 594 Lockable[] locks = new T_L1[2]; 595 locks[0] = new T_L1(); 596 locks[1] = new T_L1(); 597 598 T_User u1 = new T_User(1, lf, locks, ITERATIONS, 10 * ITERATIONS); 599 T_User u2 = new T_User(1, lf, locks, ITERATIONS, 20 * ITERATIONS); 600 Thread t1 = new Thread (u1); 601 Thread t2 = new Thread (u2); 602 603 t1.start(); 604 t2.start(); 605 606 try { 607 t1.join(); 608 t2.join(); 609 } catch (InterruptedException ie) { 610 throw T_Fail.exceptionFail(ie); 611 } 612 613 if (u1.error != null) 614 throw T_Fail.exceptionFail(u1.error); 615 if (u2.error != null) 616 throw T_Fail.exceptionFail(u2.error); 617 618 PASS("M001"); 619 } 620 621 622 631 632 633 void M002() throws StandardException, T_Fail { 634 635 Lockable[] locks = new T_L1[1]; 636 locks[0] = new T_L1(); 637 638 T_User u1 = new T_User(2, lf, locks, ITERATIONS, 10 * ITERATIONS); 639 T_User u2 = new T_User(2, lf, locks, ITERATIONS, 20 * ITERATIONS); 640 T_User u3 = new T_User(2, lf, locks, ITERATIONS, 30 * ITERATIONS); 641 Thread t1 = new Thread (u1); 642 Thread t2 = new Thread (u2); 643 Thread t3 = new Thread (u3); 644 645 t1.start(); 646 t2.start(); 647 t3.start(); 648 649 try { 650 t1.join(); 651 t2.join(); 652 t3.join(); 653 } catch (InterruptedException ie) { 654 throw T_Fail.exceptionFail(ie); 655 } 656 657 if (u1.error != null) 658 throw T_Fail.exceptionFail(u1.error); 659 if (u2.error != null) 660 throw T_Fail.exceptionFail(u2.error); 661 if (u3.error != null) 662 throw T_Fail.exceptionFail(u3.error); 663 664 665 PASS("M002"); 666 } 667 676 677 678 void M003() throws StandardException, T_Fail { 679 680 Lockable[] locks = new Lockable[1]; 681 locks[0] = new T_L2(2); 682 683 T_User u1 = new T_User(3, lf, locks, ITERATIONS, 0); 684 T_User u2 = new T_User(3, lf, locks, ITERATIONS, 0); 685 T_User u3 = new T_User(3, lf, locks, ITERATIONS, 0); 686 Thread t1 = new Thread (u1); 687 Thread t2 = new Thread (u2); 688 Thread t3 = new Thread (u3); 689 690 t1.start(); 691 t2.start(); 692 t3.start(); 693 694 try { 695 t1.join(); 696 t2.join(); 697 t3.join(); 698 } catch (InterruptedException ie) { 699 throw T_Fail.exceptionFail(ie); 700 } 701 702 if (u1.error != null) 703 throw T_Fail.exceptionFail(u1.error); 704 if (u2.error != null) 705 throw T_Fail.exceptionFail(u2.error); 706 if (u3.error != null) 707 throw T_Fail.exceptionFail(u3.error); 708 709 710 PASS("M003"); 711 } 712 722 723 724 void M004() throws StandardException, T_Fail { 725 726 Lockable[] locks = new Lockable[1]; 727 locks[0] = new T_L2(2); 728 729 T_User u1 = new T_User(4, lf, locks, ITERATIONS, 0); 730 T_User u2 = new T_User(4, lf, locks, ITERATIONS, 0); 731 T_User u3 = new T_User(4, lf, locks, ITERATIONS, 0); 732 Thread t1 = new Thread (u1); 733 Thread t2 = new Thread (u2); 734 Thread t3 = new Thread (u3); 735 736 t1.start(); 737 t2.start(); 738 t3.start(); 739 740 try { 741 t1.join(); 742 t2.join(); 743 t3.join(); 744 } catch (InterruptedException ie) { 745 throw T_Fail.exceptionFail(ie); 746 } 747 748 if (u1.error != null) 749 throw T_Fail.exceptionFail(u1.error); 750 if (u2.error != null) 751 throw T_Fail.exceptionFail(u2.error); 752 if (u3.error != null) 753 throw T_Fail.exceptionFail(u3.error); 754 755 756 PASS("M004"); 757 } 758 759 760 761 764 765 770 void checkLockCount(Object cs, int expected) throws T_Fail { 771 boolean expect = expected != 0; 772 boolean got = lf.areLocksHeld(cs); 773 if (got != expect) 774 throw T_Fail.testFailMsg("Expected lock count (" + expect + "), got (" + got + ")"); 775 } 776 777 782 783 void checkLockGroupCount(Object cs, Object group, int expected) throws T_Fail { 784 boolean expect = expected != 0; 785 boolean got = lf.areLocksHeld(cs, group); 786 if (got != expect) 787 throw T_Fail.testFailMsg("Expected lock count (" + expect + "), got (" + got + ")"); 788 } 789 790 } 791 792 793 | Popular Tags |