📄 t_lockfactory.java
字号:
/* Derby - Class org.apache.derbyTesting.unitTests.services.T_LockFactory Copyright 1997, 2005 The Apache Software Foundation or its licensors, as applicable. Licensed 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 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) Monitor.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(); S008(); 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 { Object cs = new Object(); // create an object for the compatability space 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 { 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(); 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 { 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(); 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 { 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(); 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -