📄 t_lockfactory.java
字号:
@exception T_Fail Some behaviour of the LockFactory is incorrect */ void S005() throws StandardException, T_Fail { Object cs0 = new Object(); // create an object for the compatability space Object cs1 = new Object(); // create an object for the compatability space 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 { Object cs = new Object(); // create an object for the compatability space 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"); } /** Single user API test 008. Create two compatability spaces and ensure that locks/latches 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 S008() throws StandardException, T_Fail { Object cs0 = new Object(); // create an object for the compatability space Object cs1 = new Object(); // create an object for the compatability space Object g0 = new Object(); Object g1 = new Object(); T_L1 page = new T_L1(); Lockable rA = new T_L1(); Lockable rB = new T_L1(); int count; // Simulate a page/row lock type access lf.latchObject(cs0, page, null, C_LockFactory.WAIT_FOREVER); lf.lockObject(g0, rA, null, C_LockFactory.WAIT_FOREVER, page.latch); // would release the latch if it had to wait lf.unlatch(page.latch); lf.latchObject(cs1, page, null, C_LockFactory.WAIT_FOREVER); lf.lockObject(g1, rB, null, C_LockFactory.WAIT_FOREVER, page.latch); // would release the latch if it had to wait checkLockCount(cs0, 1); checkLockCount(cs1, 2); // this wait should release the latch, while waiting // on lock, but then re-get the latch after the timeout. try { lf.lockObject(g1, rA, null, 5000, page.latch); throw T_Fail.testFailMsg("lock succeeded on already locked object"); } catch (StandardException lfe) { // we are expecting the timoeut exception, anything else is an error if (!lfe.getMessageId().equals(SQLState.LOCK_TIMEOUT)) { throw lfe; } checkLockCount(cs0, 1); checkLockCount(cs1, 1); } try { // make sure latch is held lf.latchObject(cs0, page, null, 5000); throw T_Fail.testFailMsg("latch succeeded on already latch object"); } catch (StandardException lfe) { // we are expecting timoeut exception, anything else is an error if (!lfe.getMessageId().equals(SQLState.LOCK_TIMEOUT)) { throw lfe; } } lf.unlatch(page.latch); lf.unlock(cs0, g0, rA, null); lf.unlock(cs1, g0, rB, null); PASS("S008"); } /* ** 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 & 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(Object 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(Object 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 + ")"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -