1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
|
/*
Derby - Class org.apache.derbyTesting.unitTests.services.T_LockFactory
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.unitTests.services;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import org.apache.derbyTesting.unitTests.harness.T_MultiIterations;
import org.apache.derbyTesting.unitTests.harness.T_Fail;
import org.apache.derby.iapi.services.locks.*;
import org.apache.derby.iapi.services.monitor.Monitor;
import org.apache.derby.iapi.reference.SQLState;
import org.apache.derby.iapi.error.StandardException;
/**
Protocol unit test for the LockManager.
@see LockFactory
@see org.apache.derbyTesting.unitTests.harness.UnitTest
*/
public class T_LockFactory extends T_MultiIterations
{
protected final static int ITERATIONS = 100; // iterations of multi-user tests
protected LockFactory lf;
public T_LockFactory() {
super();
}
/*
** The tests
*/
protected String getModuleToTestProtocolName() {
return org.apache.derby.iapi.reference.Module.LockFactory;
}
/**
Run all the tests, each test that starts with 'S' is a single user
test, each test that starts with 'M' is a multi-user test.
@exception T_Fail The test failed in some way.
*/
protected void setupTest() throws T_Fail {
try {
lf = (LockFactory) startSystemModule(getModuleToTestProtocolName());
} catch (StandardException mse) {
throw T_Fail.exceptionFail(mse);
}
if (lf == null) {
throw T_Fail.testFailMsg(getModuleToTestProtocolName() + " module not started.");
}
}
/**
Run once per-iteration to run the actual test.
@exception T_Fail the test failed in some way.
*/
protected void runTestSet() throws T_Fail {
// Set up the expected error handling
try {
S001();
S002();
S003();
S004();
S005();
S007();
M001();
M002();
M003();
M004();
} catch (StandardException se) {
throw T_Fail.exceptionFail(se);
}
}
/*
** Test functions
*/
/**
Single user API test 001.
Lock an single object in a single group with all lock methods and
then unlock the object with all unlock methods.
@exception StandardException An exception thrown by a method of LockFactory
@exception T_Fail Some behaviour of the LockFactory is incorrect
*/
void S001() throws StandardException, T_Fail {
CompatibilitySpace cs = lf.createCompatibilitySpace(null);
Object g0 = new Object(); // create an object for a lock group
Lockable l0 = new T_L1(); // simple lockable
int count;
// check we have no locks held
checkLockCount(cs, 0);
// lock and unlock specifically (no timeout)
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
checkLockCount(cs, 1);
count = lf.unlock(cs, g0, l0, null);
if (count != 1)
throw T_Fail.testFailMsg("invalid unlock count, expected 1, got " + count);
// check we have no locks held
checkLockCount(cs, 0);
// lock twice and unlock all ...
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
checkLockCount(cs, 2);
lf.unlock(cs, g0, l0, null);
lf.unlock(cs, g0, l0, null);
// check we have no locks held
checkLockCount(cs, 0);
// lock three times and unlock by group
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
checkLockCount(cs, 3);
lf.unlockGroup(cs, g0);
// check we have no locks held
checkLockCount(cs, 0);
// lock three times and unlock explicitly
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
checkLockCount(cs, 3);
lf.unlock(cs, g0, l0, null);
checkLockCount(cs, 2);
lf.unlock(cs, g0, l0, null);
checkLockCount(cs, 1);
lf.unlock(cs, g0, l0, null);
checkLockCount(cs, 0);
// lock and unlock specifically with timeout
lf.lockObject(cs, g0, l0, null, 1000 /*ms*/);
checkLockCount(cs, 1);
count = lf.unlock(cs, g0, l0, null);
if (count != 1)
throw T_Fail.testFailMsg("invalid unlock count, expected 1, got " + count);
PASS("S001");
}
/**
Single user API test 002.
Lock an object in different groups and check unlocks
apply to a single group.
@exception StandardException An exception thrown by a method of LockFactory
@exception T_Fail Some behaviour of the LockFactory is incorrect
*/
void S002() throws StandardException, T_Fail {
CompatibilitySpace cs = lf.createCompatibilitySpace(null);
Object g0 = new Object(); // create an object for a lock group
Object g1 = new Object();
Lockable l0 = new T_L1(); // simple lockable
int count;
// check we have no locks held
checkLockCount(cs, 0);
// lock object in two groups and unlock specifically
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs, g1, l0, null, C_LockFactory.WAIT_FOREVER);
checkLockGroupCount(cs, g0, 1);
checkLockGroupCount(cs, g1, 1);
checkLockCount(cs, 2);
count = lf.unlock(cs, g0, l0, null);
if (count != 1)
throw T_Fail.testFailMsg("invalid unlock count, expected 1, got " + count);
checkLockGroupCount(cs, g0, 0);
checkLockGroupCount(cs, g1, 1);
checkLockCount(cs, 1);
count = lf.unlock(cs, g1, l0, null);
if (count != 1)
throw T_Fail.testFailMsg("invalid unlock count, expected 1, got " + count);
checkLockGroupCount(cs, g0, 0);
checkLockGroupCount(cs, g1, 0);
// check we have no locks held
checkLockCount(cs, 0);
// lock object in two groups and unlock by group
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs, g1, l0, null, C_LockFactory.WAIT_FOREVER);
checkLockCount(cs, 2);
lf.unlockGroup(cs, g1);
checkLockGroupCount(cs, g0, 1);
checkLockGroupCount(cs, g1, 0);
checkLockCount(cs, 1);
lf.unlockGroup(cs, g0);
checkLockGroupCount(cs, g0, 0);
checkLockGroupCount(cs, g1, 0);
// check we have no locks held
checkLockCount(cs, 0);
PASS("S002");
}
/**
Single user API test 003.
Lock multiple objects in different groups and check unlocks
apply to a single group.
@exception StandardException An exception thrown by a method of LockFactory
@exception T_Fail Some behaviour of the LockFactory is incorrect
*/
void S003() throws StandardException, T_Fail {
CompatibilitySpace cs = lf.createCompatibilitySpace(null);
Object g0 = new Object(); // create an object for a lock group
Object g1 = new Object();
Lockable l0 = new T_L1(); // simple lockable
Lockable l1 = new T_L1();
Lockable l2 = new T_L1();
int count;
// check we have no locks held
checkLockCount(cs, 0);
// lock l0 object in two groups and l1,l2 in group l1
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs, g1, l0, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs, g1, l1, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs, g1, l2, null, C_LockFactory.WAIT_FOREVER);
checkLockGroupCount(cs, g0, 1);
checkLockGroupCount(cs, g1, 3);
checkLockCount(cs, 4);
// quick check to see that no one is blocked
if (lf.anyoneBlocked())
throw T_Fail.testFailMsg("anyoneBlocked() returned true on a set of private locks");
lf.unlock(cs, g1, l1, null);
checkLockGroupCount(cs, g0, 1);
checkLockGroupCount(cs, g1, 2);
checkLockCount(cs, 3);
lf.unlockGroup(cs, g1);
checkLockGroupCount(cs, g0, 1);
checkLockGroupCount(cs, g1, 0);
checkLockCount(cs, 1);
lf.unlockGroup(cs, g0);
checkLockGroupCount(cs, g0, 0);
checkLockGroupCount(cs, g1, 0);
// check we have no locks held
checkLockCount(cs, 0);
PASS("S003");
}
/**
Single user API test 004.
Lock multiple objects in different groups and transfer
locks between groups.
@exception StandardException An exception thrown by a method of LockFactory
@exception T_Fail Some behaviour of the LockFactory is incorrect
*/
void S004() throws StandardException, T_Fail {
CompatibilitySpace cs = lf.createCompatibilitySpace(null);
Object g0 = new Object(); // create an object for a lock group
Object g1 = new Object();
Object g2 = new Object();
Lockable l0 = new T_L1(); // simple lockable
Lockable l1 = new T_L1();
Lockable l2 = new T_L1();
int count = 0;
// check we have no locks held
checkLockCount(cs, 0);
// lock l0 object in two groups and l1,l2 in group l1
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs, g1, l0, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs, g1, l1, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs, g1, l2, null, C_LockFactory.WAIT_FOREVER);
checkLockGroupCount(cs, g0, 1);
checkLockGroupCount(cs, g1, 3);
checkLockCount(cs, 4);
lf.transfer(cs, g0, g1);
checkLockGroupCount(cs, g0, 0);
checkLockGroupCount(cs, g1, 4);
checkLockCount(cs, 4);
// transfer an empty to a non-existent one
lf.transfer(cs, g0, g2);
checkLockGroupCount(cs, g0, 0);
checkLockGroupCount(cs, g1, 4);
checkLockGroupCount(cs, g2, 0);
checkLockCount(cs, 4);
lf.lockObject(cs, g2, l0, null, C_LockFactory.WAIT_FOREVER);
checkLockGroupCount(cs, g2, 1);
checkLockCount(cs, 5);
lf.transfer(cs, g1, g2);
checkLockGroupCount(cs, g1, 0);
checkLockGroupCount(cs, g2, 5);
checkLockCount(cs, 5);
lf.transfer(cs, g2, g1);
checkLockGroupCount(cs, g1, 5);
checkLockGroupCount(cs, g2, 0);
checkLockCount(cs, 5);
lf.unlockGroup(cs, g2);
checkLockGroupCount(cs, g1, 5);
checkLockGroupCount(cs, g2, 0);
checkLockCount(cs, 5);
lf.unlockGroup(cs, g1);
checkLockGroupCount(cs, g1, 0);
checkLockGroupCount(cs, g2, 0);
// check we have no locks held
checkLockCount(cs, 0);
PASS("S004");
}
/**
Single user API test 005.
Create two compatability spaces and ensure that locks
block each other out.
@exception StandardException An exception thrown by a method of LockFactory
@exception T_Fail Some behaviour of the LockFactory is incorrect
*/
void S005() throws StandardException, T_Fail {
CompatibilitySpace cs0 = lf.createCompatibilitySpace(null);
CompatibilitySpace cs1 = lf.createCompatibilitySpace(null);
Object g0 = new Object(); // create an object for a lock group
Object g1 = new Object(); // create an object for a lock group
Lockable l0 = new T_L1();
Lockable l1 = new T_L1();
Lockable l2 = new T_L1();
int count;
// check we have no locks held
checkLockCount(cs0, 0);
checkLockCount(cs1, 0);
lf.lockObject(cs0, g0, l0, null, C_LockFactory.WAIT_FOREVER);
lf.lockObject(cs1, g1, l1, null, C_LockFactory.WAIT_FOREVER);
checkLockCount(cs0, 1);
checkLockCount(cs1, 1);
lf.lockObject(cs0, g0, l2, null, C_LockFactory.WAIT_FOREVER);
checkLockCount(cs0, 2);
checkLockCount(cs1, 1);
// now attempt to lock l2 in cs1 with a timeout, should fail
try {
lf.lockObject(cs1, g1, l2, null, 200 /* ms */);
throw T_Fail.testFailMsg("lock succeeded on already locked object");
}
catch (StandardException lfe) {
// we are expecting the timout exception, anything else is an error
if (!lfe.getMessageId().equals(SQLState.LOCK_TIMEOUT)) {
throw lfe;
}
checkLockCount(cs0, 2);
checkLockCount(cs1, 1);
}
// now unlock the object, and re-attempt the lock
lf.unlock(cs0, g0, l2, null);
checkLockCount(cs0, 1);
checkLockCount(cs1, 1);
lf.lockObject(cs1, g1, l2, null, C_LockFactory.WAIT_FOREVER);
checkLockCount(cs0, 1);
checkLockCount(cs1, 2);
lf.unlockGroup(cs0, g0);
lf.unlockGroup(cs1, g1);
checkLockCount(cs0, 0);
checkLockCount(cs1, 0);
PASS("S005");
}
/**
Single user API test 007.
Tests on groups and compatibility spaces
never seen by the lock manager.
@exception StandardException An exception thrown by a method of LockFactory
@exception T_Fail Some behaviour of the LockFactory is incorrect
*/
void S007() throws StandardException, T_Fail {
CompatibilitySpace cs = lf.createCompatibilitySpace(null);
Object g0 = new Object(); // create an object for a lock group
Object g1 = new Object(); // create an object for a lock group
Lockable l0 = new T_L1();
int count;
// check we have no locks held
checkLockCount(cs, 0);
checkLockGroupCount(cs, g0, 0);
lf.unlockGroup(cs, g0);
lf.unlockGroup(cs, cs);
lf.unlock(cs, g0, l0, null);
lf.transfer(cs, g0, g1);
lf.transfer(cs, g1, g0);
if (lf.anyoneBlocked())
throw T_Fail.testFailMsg("anyoneBlocked() returned true on an empty space");
// check we have no locks held
checkLockCount(cs, 0);
checkLockGroupCount(cs, g0, 0);
checkLockGroupCount(cs, g1, 0);
PASS("S007");
}
/*
** Multi-user tests.
*/
/**
Multi-user test 001.
Create two lockable objects and pass them off to two threads.
Each thread will run lock the first object, set its value then lock
the second object and set its value, yield and then release the lock
on one and then on two. Various checks are made to ensure the
values are as expected.
@exception StandardException An exception thrown by a method of LockFactory
@exception T_Fail Some behaviour of the LockFactory is incorrect
*/
void M001() throws StandardException, T_Fail {
Lockable[] locks = new T_L1[2];
locks[0] = new T_L1();
locks[1] = new T_L1();
T_User u1 = new T_User(1, lf, locks, ITERATIONS, 10 * ITERATIONS);
T_User u2 = new T_User(1, lf, locks, ITERATIONS, 20 * ITERATIONS);
Thread t1 = new Thread(u1);
Thread t2 = new Thread(u2);
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException ie) {
throw T_Fail.exceptionFail(ie);
}
if (u1.error != null)
throw T_Fail.exceptionFail(u1.error);
if (u2.error != null)
throw T_Fail.exceptionFail(u2.error);
PASS("M001");
}
/**
Multi-user test 002
Create a single lockable and have three threads lock it, yield and
then release it. The single lockable can only have one locker.
@exception StandardException An exception thrown by a method of LockFactory
@exception T_Fail Some behaviour of the LockFactory is incorrect
*/
void M002() throws StandardException, T_Fail {
Lockable[] locks = new T_L1[1];
locks[0] = new T_L1();
T_User u1 = new T_User(2, lf, locks, ITERATIONS, 10 * ITERATIONS);
T_User u2 = new T_User(2, lf, locks, ITERATIONS, 20 * ITERATIONS);
T_User u3 = new T_User(2, lf, locks, ITERATIONS, 30 * ITERATIONS);
Thread t1 = new Thread(u1);
Thread t2 = new Thread(u2);
Thread t3 = new Thread(u3);
t1.start();
t2.start();
t3.start();
try {
t1.join();
t2.join();
t3.join();
} catch (InterruptedException ie) {
throw T_Fail.exceptionFail(ie);
}
if (u1.error != null)
throw T_Fail.exceptionFail(u1.error);
if (u2.error != null)
throw T_Fail.exceptionFail(u2.error);
if (u3.error != null)
throw T_Fail.exceptionFail(u3.error);
PASS("M002");
}
/**
Multi-user test 003
Create a single lockable and have three threads lock it, yield and
then release it. The single lockable is a semaphore that can have two lockers.
@exception StandardException An exception thrown by a method of LockFactory
@exception T_Fail Some behaviour of the LockFactory is incorrect
*/
void M003() throws StandardException, T_Fail {
Lockable[] locks = new Lockable[1];
locks[0] = new T_L2(2);
T_User u1 = new T_User(3, lf, locks, ITERATIONS, 0);
T_User u2 = new T_User(3, lf, locks, ITERATIONS, 0);
T_User u3 = new T_User(3, lf, locks, ITERATIONS, 0);
Thread t1 = new Thread(u1);
Thread t2 = new Thread(u2);
Thread t3 = new Thread(u3);
t1.start();
t2.start();
t3.start();
try {
t1.join();
t2.join();
t3.join();
} catch (InterruptedException ie) {
throw T_Fail.exceptionFail(ie);
}
if (u1.error != null)
throw T_Fail.exceptionFail(u1.error);
if (u2.error != null)
throw T_Fail.exceptionFail(u2.error);
if (u3.error != null)
throw T_Fail.exceptionFail(u3.error);
PASS("M003");
}
/**
Multi-user test 004
As M003 but each thread will lock the object twice, to ensure that
lock manager grantes the lock when the compatability space and qualifier
match.
@exception StandardException An exception thrown by a method of LockFactory
@exception T_Fail Some behaviour of the LockFactory is incorrect
*/
void M004() throws StandardException, T_Fail {
Lockable[] locks = new Lockable[1];
locks[0] = new T_L2(2);
T_User u1 = new T_User(4, lf, locks, ITERATIONS, 0);
T_User u2 = new T_User(4, lf, locks, ITERATIONS, 0);
T_User u3 = new T_User(4, lf, locks, ITERATIONS, 0);
Thread t1 = new Thread(u1);
Thread t2 = new Thread(u2);
Thread t3 = new Thread(u3);
t1.start();
t2.start();
t3.start();
try {
t1.join();
t2.join();
t3.join();
} catch (InterruptedException ie) {
throw T_Fail.exceptionFail(ie);
}
if (u1.error != null)
throw T_Fail.exceptionFail(u1.error);
if (u2.error != null)
throw T_Fail.exceptionFail(u2.error);
if (u3.error != null)
throw T_Fail.exceptionFail(u3.error);
PASS("M004");
}
/*
** Utility functions
*/
/**
Check to see if the total number of locks we have is as expected.
@exception T_Fail Number of locks is not as expected.
*/
void checkLockCount(CompatibilitySpace cs, int expected) throws T_Fail {
boolean expect = expected != 0;
boolean got = lf.areLocksHeld(cs);
if (got != expect)
throw T_Fail.testFailMsg("Expected lock count (" + expect + "), got (" + got + ")");
}
/**
Check to see if the number of locks in a group we have is as expected.
@exception T_Fail Number of locks is not as expected.
*/
void checkLockGroupCount(CompatibilitySpace cs, Object group, int expected)
throws T_Fail {
boolean expect = expected != 0;
boolean got = lf.areLocksHeld(cs, group);
if (got != expect)
throw T_Fail.testFailMsg("Expected lock count (" + expect + "), got (" + got + ")");
}
/**
* Privileged startup. Must be private so that user code
* can't call this entry point.
*/
private static Object startSystemModule( final String factoryInterface )
throws StandardException
{
try {
return AccessController.doPrivileged
(
new PrivilegedExceptionAction<Object>()
{
public Object run()
throws StandardException
{
return Monitor.startSystemModule( factoryInterface );
}
}
);
} catch (PrivilegedActionException pae)
{
throw StandardException.plainWrapException( pae );
}
}
}
|