001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.services.T_User
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.services;
023:
024: import org.apache.derbyTesting.unitTests.harness.T_Fail;
025:
026: import org.apache.derby.iapi.error.StandardException;
027: import org.apache.derby.iapi.services.sanity.SanityManager;
028: import org.apache.derby.iapi.services.locks.*;
029:
030: class T_User implements Runnable {
031:
032: private LockFactory lf;
033: private Lockable[] refs;
034: private long iterations;
035: private long offset;
036: private int test;
037:
038: Throwable error = null;
039:
040: T_User(int test, LockFactory lf, Lockable[] refs, long iterations,
041: long offset) {
042:
043: this .lf = lf;
044: this .refs = refs;
045: this .iterations = iterations;
046: this .test = test;
047: this .offset = offset;
048: }
049:
050: public void run() {
051:
052: try {
053: switch (test) {
054: case 1:
055: T001();
056: break;
057: case 2:
058: T002();
059: break;
060: case 3:
061: T003();
062: break;
063: }
064: } catch (Throwable t) {
065: error = t;
066: }
067: }
068:
069: private void T001() throws StandardException, T_Fail {
070:
071: Object cs = new Object(); // create an object for the compatability space
072: Integer g0 = new Integer(1); // create an object for a lock group
073:
074: // check we have no locks held
075: checkLockCount(cs, 0);
076:
077: T_L1 ref;
078:
079: while (--iterations > 0) {
080: long value = offset + iterations;
081:
082: lf.lockObject(cs, g0, refs[0], null,
083: C_LockFactory.WAIT_FOREVER);
084: ref = (T_L1) refs[0];
085: ref.value = value;
086: checkLockCount(cs, 1);
087: Thread.yield();
088: checkValue(ref, value);
089:
090: lf.lockObject(cs, g0, refs[1], null,
091: C_LockFactory.WAIT_FOREVER);
092: ref = (T_L1) refs[1];
093: ref.value = value;
094: Thread.yield();
095:
096: checkValue((T_L1) refs[0], value);
097: checkValue((T_L1) refs[1], value);
098:
099: lf.unlock(cs, g0, refs[0], null);
100: checkValue((T_L1) refs[1], value);
101:
102: Thread.yield();
103:
104: lf.unlock(cs, g0, refs[1], null);
105:
106: // check we have no locks held
107: checkLockCount(cs, 0);
108:
109: Thread.yield();
110:
111: }
112: }
113:
114: private void T002() throws StandardException, T_Fail {
115:
116: Object cs = new Object(); // create an object for the compatability space
117: Integer g0 = new Integer(1); // create an object for a lock group
118:
119: // check we have no locks held
120: checkLockCount(cs, 0);
121:
122: while (--iterations > 0) {
123: long value = offset + iterations;
124: T_L1 ref = (T_L1) refs[0];
125:
126: lf.lockObject(cs, g0, refs[0], null,
127: C_LockFactory.WAIT_FOREVER);
128: ref.value = value;
129: checkLockCount(cs, 1);
130: Thread.yield();
131: checkValue(ref, value);
132:
133: lf.unlock(cs, g0, refs[0], null);
134:
135: // check we have no locks held
136: checkLockCount(cs, 0);
137: }
138: }
139:
140: private void T003() throws StandardException, T_Fail {
141:
142: Object cs = new Object(); // create an object for the compatability space
143: Integer g0 = new Integer(1); // create an object for a lock group
144:
145: // check we have no locks held
146: checkLockCount(cs, 0);
147:
148: while (--iterations > 0) {
149:
150: lf.lockObject(cs, g0, refs[0], null,
151: C_LockFactory.WAIT_FOREVER);
152: checkLockCount(cs, 1);
153: Thread.yield();
154: lf.unlock(cs, g0, refs[0], null);
155:
156: // check we have no locks held
157: checkLockCount(cs, 0);
158: }
159: }
160:
161: private void T004() throws StandardException, T_Fail {
162:
163: Object cs = new Object(); // create an object for the compatability space
164: Integer g0 = new Integer(1); // create an object for a lock group
165:
166: // check we have no locks held
167: checkLockCount(cs, 0);
168:
169: while (--iterations > 0) {
170:
171: lf.lockObject(cs, g0, refs[0], null,
172: C_LockFactory.WAIT_FOREVER);
173: checkLockCount(cs, 1);
174: Thread.yield();
175:
176: lf.lockObject(cs, g0, refs[0], null,
177: C_LockFactory.WAIT_FOREVER);
178: checkLockCount(cs, 2);
179: Thread.yield();
180:
181: lf.unlockGroup(cs, g0);
182:
183: // check we have no locks held
184: checkLockCount(cs, 0);
185: }
186: }
187:
188: private void checkValue(T_L1 item, long value) throws T_Fail {
189: if (item.value != value)
190: throw T_Fail
191: .testFailMsg("value corrupted in multi-user test, exapected "
192: + value + ", got " + item.value);
193: }
194:
195: void checkLockCount(Object cs, int expected) throws T_Fail {
196: boolean expect = expected != 0;
197: boolean got = lf.areLocksHeld(cs);
198: if (got != expect)
199: throw T_Fail.testFailMsg("Expected lock count (" + expect
200: + "), got (" + got + ")");
201: }
202:
203: }
|