KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > services > T_User


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.services.T_User
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.services;
23
24 import org.apache.derbyTesting.unitTests.harness.T_Fail;
25
26 import org.apache.derby.iapi.error.StandardException;
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28 import org.apache.derby.iapi.services.locks.*;
29
30 class T_User implements Runnable JavaDoc {
31
32     private LockFactory lf;
33     private Lockable[] refs;
34     private long iterations;
35     private long offset;
36     private int test;
37
38     Throwable JavaDoc error = null;
39
40
41
42     T_User(int test, LockFactory lf, Lockable[] refs, long iterations, long offset) {
43
44         this.lf = lf;
45         this.refs = refs;
46         this.iterations = iterations;
47         this.test = test;
48         this.offset = offset;
49     }
50
51     public void run() {
52
53         try {
54             switch (test) {
55             case 1:
56                 T001();
57                 break;
58             case 2:
59                 T002();
60                 break;
61             case 3:
62                 T003();
63                 break;
64             }
65         } catch (Throwable JavaDoc t) {
66             error = t;
67         }
68     }
69
70     private void T001() throws StandardException, T_Fail {
71
72         Object JavaDoc cs = new Object JavaDoc(); // create an object for the compatability space
73
Integer JavaDoc g0 = new Integer JavaDoc(1); // create an object for a lock group
74

75         // check we have no locks held
76
checkLockCount(cs, 0);
77
78         T_L1 ref;
79
80         while (--iterations > 0) {
81             long value = offset + iterations;
82
83             lf.lockObject(cs, g0, refs[0], null, C_LockFactory.WAIT_FOREVER);
84             ref = (T_L1) refs[0];
85             ref.value = value;
86             checkLockCount(cs, 1);
87             Thread.yield();
88             checkValue(ref, value);
89
90             lf.lockObject(cs, g0, refs[1], null, C_LockFactory.WAIT_FOREVER);
91             ref = (T_L1) refs[1];
92             ref.value = value;
93             Thread.yield();
94
95             checkValue((T_L1) refs[0], value);
96             checkValue((T_L1) refs[1], value);
97
98             lf.unlock(cs, g0, refs[0], null);
99             checkValue((T_L1) refs[1], value);
100
101             Thread.yield();
102
103             lf.unlock(cs, g0, refs[1], null);
104
105             // check we have no locks held
106
checkLockCount(cs, 0);
107
108             Thread.yield();
109
110         }
111     }
112
113     private void T002() throws StandardException, T_Fail {
114
115         Object JavaDoc cs = new Object JavaDoc(); // create an object for the compatability space
116
Integer JavaDoc g0 = new Integer JavaDoc(1); // create an object for a lock group
117

118         // check we have no locks held
119
checkLockCount(cs, 0);
120
121         while (--iterations > 0) {
122             long value = offset + iterations;
123             T_L1 ref = (T_L1) refs[0];
124
125             lf.lockObject(cs, g0, refs[0], null, C_LockFactory.WAIT_FOREVER);
126             ref.value = value;
127             checkLockCount(cs, 1);
128             Thread.yield();
129             checkValue(ref, value);
130
131             lf.unlock(cs, g0, refs[0], null);
132
133             // check we have no locks held
134
checkLockCount(cs, 0);
135         }
136     }
137
138     private void T003() throws StandardException, T_Fail {
139
140         Object JavaDoc cs = new Object JavaDoc(); // create an object for the compatability space
141
Integer JavaDoc g0 = new Integer JavaDoc(1); // create an object for a lock group
142

143         // check we have no locks held
144
checkLockCount(cs, 0);
145
146         while (--iterations > 0) {
147
148             lf.lockObject(cs, g0, refs[0], null, C_LockFactory.WAIT_FOREVER);
149             checkLockCount(cs, 1);
150             Thread.yield();
151             lf.unlock(cs, g0, refs[0], null);
152
153             // check we have no locks held
154
checkLockCount(cs, 0);
155         }
156     }
157     private void T004() throws StandardException, T_Fail {
158
159         Object JavaDoc cs = new Object JavaDoc(); // create an object for the compatability space
160
Integer JavaDoc g0 = new Integer JavaDoc(1); // create an object for a lock group
161

162         // check we have no locks held
163
checkLockCount(cs, 0);
164
165         while (--iterations > 0) {
166
167             lf.lockObject(cs, g0, refs[0], null, C_LockFactory.WAIT_FOREVER);
168             checkLockCount(cs, 1);
169             Thread.yield();
170
171
172             lf.lockObject(cs, g0, refs[0], null, C_LockFactory.WAIT_FOREVER);
173             checkLockCount(cs, 2);
174             Thread.yield();
175
176             lf.unlockGroup(cs, g0);
177
178             // check we have no locks held
179
checkLockCount(cs, 0);
180         }
181     }
182
183     private void checkValue(T_L1 item, long value) throws T_Fail {
184         if (item.value != value)
185             throw T_Fail.testFailMsg("value corrupted in multi-user test, exapected " + value + ", got " + item.value);
186     }
187
188     void checkLockCount(Object JavaDoc cs, int expected) throws T_Fail {
189         boolean expect = expected != 0;
190         boolean got = lf.areLocksHeld(cs);
191         if (got != expect)
192             throw T_Fail.testFailMsg("Expected lock count (" + expect + "), got (" + got + ")");
193     }
194
195
196 }
197
Popular Tags