KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.services.T_LockFactory
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_MultiIterations;
25 import org.apache.derbyTesting.unitTests.harness.T_Fail;
26
27 import org.apache.derby.iapi.services.locks.*;
28
29 import org.apache.derby.iapi.services.monitor.Monitor;
30
31 import org.apache.derby.iapi.reference.SQLState;
32
33 import org.apache.derby.iapi.error.StandardException;
34
35 /**
36     Protocol unit test for the LockManager.
37
38     @see LockFactory
39     @see org.apache.derbyTesting.unitTests.harness..UnitTest
40 */

41
42 public class T_LockFactory extends T_MultiIterations
43 {
44     protected final static int ITERATIONS = 100; // iterations of multi-user tests
45

46     protected LockFactory lf;
47
48     public T_LockFactory() {
49         super();
50     }
51
52     /*
53     ** The tests
54     */

55
56     protected String JavaDoc getModuleToTestProtocolName() {
57
58         return org.apache.derby.iapi.reference.Module.LockFactory;
59     }
60
61     /**
62         Run all the tests, each test that starts with 'S' is a single user
63         test, each test that starts with 'M' is a multi-user test.
64
65         @exception T_Fail The test failed in some way.
66     */

67     protected void setupTest() throws T_Fail {
68
69         try {
70             lf = (LockFactory) Monitor.startSystemModule(getModuleToTestProtocolName());
71         } catch (StandardException mse) {
72             throw T_Fail.exceptionFail(mse);
73         }
74         if (lf == null) {
75             throw T_Fail.testFailMsg(getModuleToTestProtocolName() + " module not started.");
76         }
77     }
78
79     /**
80         Run once per-iteration to run the actual test.
81         @exception T_Fail the test failed in some way.
82     */

83     protected void runTestSet() throws T_Fail {
84
85         // Set up the expected error handling
86
try {
87             
88             S001();
89             S002();
90             S003();
91             S004();
92             S005();
93             S007();
94             S008();
95
96             M001();
97             M002();
98             M003();
99             M004();
100             
101
102         } catch (StandardException se) {
103
104             throw T_Fail.exceptionFail(se);
105
106         }
107     }
108
109     /*
110     ** Test functions
111     */

112
113     /**
114         Single user API test 001.
115
116         Lock an single object in a single group with all lock methods and
117         then unlock the object with all unlock methods.
118
119         @exception StandardException An exception thrown by a method of LockFactory
120         @exception T_Fail Some behaviour of the LockFactory is incorrect
121     */

122     void S001() throws StandardException, T_Fail {
123
124         Object JavaDoc cs = new Object JavaDoc(); // create an object for the compatability space
125
Object JavaDoc g0 = new Object JavaDoc(); // create an object for a lock group
126
Lockable l0 = new T_L1(); // simple lockable
127

128         int count;
129         
130         // check we have no locks held
131
checkLockCount(cs, 0);
132
133         // lock and unlock specifically (no timeout)
134
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
135         checkLockCount(cs, 1);
136         count = lf.unlock(cs, g0, l0, null);
137         if (count != 1)
138             throw T_Fail.testFailMsg("invalid unlock count, expected 1, got " + count);
139
140         // check we have no locks held
141
checkLockCount(cs, 0);
142
143         // lock twice and unlock all ...
144
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
145         lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
146         checkLockCount(cs, 2);
147         lf.unlock(cs, g0, l0, null);
148         lf.unlock(cs, g0, l0, null);
149
150         // check we have no locks held
151
checkLockCount(cs, 0);
152
153         // lock three times and unlock by group
154
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
155         lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
156         lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
157         checkLockCount(cs, 3);
158         lf.unlockGroup(cs, g0);
159
160         // check we have no locks held
161
checkLockCount(cs, 0);
162
163
164         // lock three times and unlock explicitly
165
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
166         lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
167         lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
168         checkLockCount(cs, 3);
169
170         lf.unlock(cs, g0, l0, null);
171         checkLockCount(cs, 2);
172
173         lf.unlock(cs, g0, l0, null);
174         checkLockCount(cs, 1);
175
176         lf.unlock(cs, g0, l0, null);
177         checkLockCount(cs, 0);
178
179         // lock and unlock specifically with timeout
180
lf.lockObject(cs, g0, l0, null, 1000 /*ms*/);
181         checkLockCount(cs, 1);
182         count = lf.unlock(cs, g0, l0, null);
183         if (count != 1)
184             throw T_Fail.testFailMsg("invalid unlock count, expected 1, got " + count);
185
186         PASS("S001");
187     }
188
189     /**
190         Single user API test 002.
191
192         Lock an object in different groups and check unlocks
193         apply to a single group.
194
195         @exception StandardException An exception thrown by a method of LockFactory
196         @exception T_Fail Some behaviour of the LockFactory is incorrect
197     */

198
199     void S002() throws StandardException, T_Fail {
200
201         Object JavaDoc cs = new Object JavaDoc(); // create an object for the compatability space
202
Object JavaDoc g0 = new Object JavaDoc(); // create an object for a lock group
203
Object JavaDoc g1 = new Object JavaDoc();
204         Lockable l0 = new T_L1(); // simple lockable
205

206         int count;
207         
208         // check we have no locks held
209
checkLockCount(cs, 0);
210
211         // lock object in two groups and unlock specifically
212
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
213         lf.lockObject(cs, g1, l0, null, C_LockFactory.WAIT_FOREVER);
214         checkLockGroupCount(cs, g0, 1);
215         checkLockGroupCount(cs, g1, 1);
216         checkLockCount(cs, 2);
217
218         count = lf.unlock(cs, g0, l0, null);
219         if (count != 1)
220             throw T_Fail.testFailMsg("invalid unlock count, expected 1, got " + count);
221         checkLockGroupCount(cs, g0, 0);
222         checkLockGroupCount(cs, g1, 1);
223         checkLockCount(cs, 1);
224
225         count = lf.unlock(cs, g1, l0, null);
226         if (count != 1)
227             throw T_Fail.testFailMsg("invalid unlock count, expected 1, got " + count);
228         checkLockGroupCount(cs, g0, 0);
229         checkLockGroupCount(cs, g1, 0);
230
231
232         // check we have no locks held
233
checkLockCount(cs, 0);
234
235         // lock object in two groups and unlock by group
236
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
237         lf.lockObject(cs, g1, l0, null, C_LockFactory.WAIT_FOREVER);
238         checkLockCount(cs, 2);
239
240         lf.unlockGroup(cs, g1);
241         checkLockGroupCount(cs, g0, 1);
242         checkLockGroupCount(cs, g1, 0);
243         checkLockCount(cs, 1);
244
245         lf.unlockGroup(cs, g0);
246         checkLockGroupCount(cs, g0, 0);
247         checkLockGroupCount(cs, g1, 0);
248
249         // check we have no locks held
250
checkLockCount(cs, 0);
251
252         PASS("S002");
253     }
254
255     /**
256         Single user API test 003.
257
258         Lock multiple objects in different groups and check unlocks
259         apply to a single group.
260
261         @exception StandardException An exception thrown by a method of LockFactory
262         @exception T_Fail Some behaviour of the LockFactory is incorrect
263
264     */

265
266     void S003() throws StandardException, T_Fail {
267
268         Object JavaDoc cs = new Object JavaDoc(); // create an object for the compatability space
269
Object JavaDoc g0 = new Object JavaDoc(); // create an object for a lock group
270
Object JavaDoc g1 = new Object JavaDoc();
271         Lockable l0 = new T_L1(); // simple lockable
272
Lockable l1 = new T_L1();
273         Lockable l2 = new T_L1();
274
275         int count;
276         
277         // check we have no locks held
278
checkLockCount(cs, 0);
279
280         // lock l0 object in two groups and l1,l2 in group l1
281
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
282         lf.lockObject(cs, g1, l0, null, C_LockFactory.WAIT_FOREVER);
283         lf.lockObject(cs, g1, l1, null, C_LockFactory.WAIT_FOREVER);
284         lf.lockObject(cs, g1, l2, null, C_LockFactory.WAIT_FOREVER);
285
286         checkLockGroupCount(cs, g0, 1);
287         checkLockGroupCount(cs, g1, 3);
288         checkLockCount(cs, 4);
289
290         // quick check to see that no one is blocked
291
if (lf.anyoneBlocked())
292             throw T_Fail.testFailMsg("anyoneBlocked() returned true on a set of private locks");
293
294         lf.unlock(cs, g1, l1, null);
295         checkLockGroupCount(cs, g0, 1);
296         checkLockGroupCount(cs, g1, 2);
297         checkLockCount(cs, 3);
298
299         lf.unlockGroup(cs, g1);
300         checkLockGroupCount(cs, g0, 1);
301         checkLockGroupCount(cs, g1, 0);
302         checkLockCount(cs, 1);
303
304         lf.unlockGroup(cs, g0);
305         checkLockGroupCount(cs, g0, 0);
306         checkLockGroupCount(cs, g1, 0);
307
308         // check we have no locks held
309
checkLockCount(cs, 0);
310
311         PASS("S003");
312     }
313
314     /**
315         Single user API test 004.
316
317         Lock multiple objects in different groups and transfer
318         locks between groups.
319
320         @exception StandardException An exception thrown by a method of LockFactory
321         @exception T_Fail Some behaviour of the LockFactory is incorrect
322     */

323
324     void S004() throws StandardException, T_Fail {
325
326         Object JavaDoc cs = new Object JavaDoc(); // create an object for the compatability space
327
Object JavaDoc g0 = new Object JavaDoc(); // create an object for a lock group
328
Object JavaDoc g1 = new Object JavaDoc();
329         Object JavaDoc g2 = new Object JavaDoc();
330         Lockable l0 = new T_L1(); // simple lockable
331
Lockable l1 = new T_L1();
332         Lockable l2 = new T_L1();
333
334         int count = 0;
335
336         // check we have no locks held
337
checkLockCount(cs, 0);
338
339         // lock l0 object in two groups and l1,l2 in group l1
340
lf.lockObject(cs, g0, l0, null, C_LockFactory.WAIT_FOREVER);
341         lf.lockObject(cs, g1, l0, null, C_LockFactory.WAIT_FOREVER);
342         lf.lockObject(cs, g1, l1, null, C_LockFactory.WAIT_FOREVER);
343         lf.lockObject(cs, g1, l2, null, C_LockFactory.WAIT_FOREVER);
344
345         checkLockGroupCount(cs, g0, 1);
346         checkLockGroupCount(cs, g1, 3);
347         checkLockCount(cs, 4);
348
349         lf.transfer(cs, g0, g1);
350         checkLockGroupCount(cs, g0, 0);
351         checkLockGroupCount(cs, g1, 4);
352         checkLockCount(cs, 4);
353
354         // transfer an empty to a non-existent one
355
lf.transfer(cs, g0, g2);
356         checkLockGroupCount(cs, g0, 0);
357         checkLockGroupCount(cs, g1, 4);
358         checkLockGroupCount(cs, g2, 0);
359         checkLockCount(cs, 4);
360         
361         lf.lockObject(cs, g2, l0, null, C_LockFactory.WAIT_FOREVER);
362         checkLockGroupCount(cs, g2, 1);
363         checkLockCount(cs, 5);
364
365         lf.transfer(cs, g1, g2);
366         checkLockGroupCount(cs, g1, 0);
367         checkLockGroupCount(cs, g2, 5);
368         checkLockCount(cs, 5);
369
370         lf.transfer(cs, g2, g1);
371         checkLockGroupCount(cs, g1, 5);
372         checkLockGroupCount(cs, g2, 0);
373         checkLockCount(cs, 5);
374
375
376         lf.unlockGroup(cs, g2);
377         checkLockGroupCount(cs, g1, 5);
378         checkLockGroupCount(cs, g2, 0);
379         checkLockCount(cs, 5);
380
381         lf.unlockGroup(cs, g1);
382         checkLockGroupCount(cs, g1, 0);
383         checkLockGroupCount(cs, g2, 0);
384
385         // check we have no locks held
386
checkLockCount(cs, 0);
387
388         PASS("S004");
389     }
390
391     /**
392         Single user API test 005.
393
394         Create two compatability spaces and ensure that locks
395         block each other out.
396
397         @exception StandardException An exception thrown by a method of LockFactory
398         @exception T_Fail Some behaviour of the LockFactory is incorrect
399     */

400     void S005() throws StandardException, T_Fail {
401
402         Object JavaDoc cs0 = new Object JavaDoc(); // create an object for the compatability space
403
Object JavaDoc cs1 = new Object JavaDoc(); // create an object for the compatability space
404

405         Object JavaDoc g0 = new Object JavaDoc(); // create an object for a lock group
406
Object JavaDoc g1 = new Object JavaDoc(); // create an object for a lock group
407
Lockable l0 = new T_L1();
408         Lockable l1 = new T_L1();
409         Lockable l2 = new T_L1();
410
411         int count;
412
413         // check we have no locks held
414
checkLockCount(cs0, 0);
415         checkLockCount(cs1, 0);
416
417         lf.lockObject(cs0, g0, l0, null, C_LockFactory.WAIT_FOREVER);
418         lf.lockObject(cs1, g1, l1, null, C_LockFactory.WAIT_FOREVER);
419         checkLockCount(cs0, 1);
420         checkLockCount(cs1, 1);
421
422         lf.lockObject(cs0, g0, l2, null, C_LockFactory.WAIT_FOREVER);
423         checkLockCount(cs0, 2);
424         checkLockCount(cs1, 1);
425
426         // now attempt to lock l2 in cs1 with a timeout, should fail
427
try {
428             lf.lockObject(cs1, g1, l2, null, 200 /* ms */);
429             throw T_Fail.testFailMsg("lock succeeded on already locked object");
430         }
431         catch (StandardException lfe) {
432             // we are expecting the timout exception, anything else is an error
433
if (!lfe.getMessageId().equals(SQLState.LOCK_TIMEOUT)) {
434                 throw lfe;
435             }
436             checkLockCount(cs0, 2);
437             checkLockCount(cs1, 1);
438
439         }
440
441         // now unlock the object, and re-attempt the lock
442
lf.unlock(cs0, g0, l2, null);
443         checkLockCount(cs0, 1);
444         checkLockCount(cs1, 1);
445         lf.lockObject(cs1, g1, l2, null, C_LockFactory.WAIT_FOREVER);
446         checkLockCount(cs0, 1);
447         checkLockCount(cs1, 2);
448
449         lf.unlockGroup(cs0, g0);
450         lf.unlockGroup(cs1, g1);
451         checkLockCount(cs0, 0);
452         checkLockCount(cs1, 0);
453
454         PASS("S005");
455
456
457
458     }
459
460
461     /**
462         Single user API test 007.
463
464         Tests on groups and compatibility spaces
465         never seen by the lock manager.
466         
467
468         @exception StandardException An exception thrown by a method of LockFactory
469         @exception T_Fail Some behaviour of the LockFactory is incorrect
470     */

471     void S007() throws StandardException, T_Fail {
472
473         Object JavaDoc cs = new Object JavaDoc(); // create an object for the compatability space
474
Object JavaDoc g0 = new Object JavaDoc(); // create an object for a lock group
475
Object JavaDoc g1 = new Object JavaDoc(); // create an object for a lock group
476
Lockable l0 = new T_L1();
477
478         int count;
479
480         // check we have no locks held
481
checkLockCount(cs, 0);
482         checkLockGroupCount(cs, g0, 0);
483
484         lf.unlockGroup(cs, g0);
485         lf.unlockGroup(cs, cs);
486         lf.unlock(cs, g0, l0, null);
487
488         lf.transfer(cs, g0, g1);
489         lf.transfer(cs, g1, g0);
490
491         if (lf.anyoneBlocked())
492             throw T_Fail.testFailMsg("anyoneBlocked() returned true on an empty space");
493
494         // check we have no locks held
495
checkLockCount(cs, 0);
496         checkLockGroupCount(cs, g0, 0);
497         checkLockGroupCount(cs, g1, 0);
498
499         PASS("S007");
500     }
501
502     /**
503         Single user API test 008.
504
505         Create two compatability spaces and ensure that locks/latches
506         block each other out.
507
508         @exception StandardException An exception thrown by a method of LockFactory
509         @exception T_Fail Some behaviour of the LockFactory is incorrect
510     */

511     void S008() throws StandardException, T_Fail {
512
513         Object JavaDoc cs0 = new Object JavaDoc(); // create an object for the compatability space
514
Object JavaDoc cs1 = new Object JavaDoc(); // create an object for the compatability space
515

516         Object JavaDoc g0 = new Object JavaDoc();
517         Object JavaDoc g1 = new Object JavaDoc();
518
519         T_L1 page = new T_L1();
520         Lockable rA = new T_L1();
521         Lockable rB = new T_L1();
522
523         int count;
524
525         // Simulate a page/row lock type access
526
lf.latchObject(cs0, page, null, C_LockFactory.WAIT_FOREVER);
527         lf.lockObject(g0, rA, null, C_LockFactory.WAIT_FOREVER, page.latch); // would release the latch if it had to wait
528
lf.unlatch(page.latch);
529
530         lf.latchObject(cs1, page, null, C_LockFactory.WAIT_FOREVER);
531         lf.lockObject(g1, rB, null, C_LockFactory.WAIT_FOREVER, page.latch); // would release the latch if it had to wait
532

533         checkLockCount(cs0, 1);
534         checkLockCount(cs1, 2);
535
536         // this wait should release the latch, while waiting
537
// on lock, but then re-get the latch after the timeout.
538
try {
539             lf.lockObject(g1, rA, null, 5000, page.latch);
540             throw T_Fail.testFailMsg("lock succeeded on already locked object");
541         }
542         catch (StandardException lfe) {
543             // we are expecting the timoeut exception, anything else is an error
544
if (!lfe.getMessageId().equals(SQLState.LOCK_TIMEOUT)) {
545                 throw lfe;
546             }
547             checkLockCount(cs0, 1);
548             checkLockCount(cs1, 1);
549         }
550
551         try {
552             // make sure latch is held
553
lf.latchObject(cs0, page, null, 5000);
554             throw T_Fail.testFailMsg("latch succeeded on already latch object");
555         }
556         catch (StandardException lfe) {
557             // we are expecting timoeut exception, anything else is an error
558
if (!lfe.getMessageId().equals(SQLState.LOCK_TIMEOUT)) {
559                 throw lfe;
560             }
561         }
562
563         lf.unlatch(page.latch);
564
565         lf.unlock(cs0, g0, rA, null);
566         lf.unlock(cs1, g0, rB, null);
567
568         PASS("S008");
569
570
571     }
572
573
574
575     /*
576     ** Multi-user tests.
577     */

578
579     /**
580         Multi-user test 001.
581
582         Create two lockable objects and pass them off to two threads.
583         Each thread will run lock the first object, set its value then lock
584         the second object & set its value, yield and then release the lock
585         on one and then on two. Various checks are made to ensure the
586         values are as expected.
587
588         @exception StandardException An exception thrown by a method of LockFactory
589         @exception T_Fail Some behaviour of the LockFactory is incorrect
590     */

591
592     void M001() throws StandardException, T_Fail {
593
594         Lockable[] locks = new T_L1[2];
595         locks[0] = new T_L1();
596         locks[1] = new T_L1();
597
598         T_User u1 = new T_User(1, lf, locks, ITERATIONS, 10 * ITERATIONS);
599         T_User u2 = new T_User(1, lf, locks, ITERATIONS, 20 * ITERATIONS);
600         Thread JavaDoc t1 = new Thread JavaDoc(u1);
601         Thread JavaDoc t2 = new Thread JavaDoc(u2);
602
603         t1.start();
604         t2.start();
605
606         try {
607             t1.join();
608             t2.join();
609         } catch (InterruptedException JavaDoc ie) {
610             throw T_Fail.exceptionFail(ie);
611         }
612
613         if (u1.error != null)
614             throw T_Fail.exceptionFail(u1.error);
615         if (u2.error != null)
616             throw T_Fail.exceptionFail(u2.error);
617
618         PASS("M001");
619     }
620     
621
622     /**
623         Multi-user test 002
624
625         Create a single lockable and have three threads lock it, yield and
626         then release it. The single lockable can only have one locker.
627
628         @exception StandardException An exception thrown by a method of LockFactory
629         @exception T_Fail Some behaviour of the LockFactory is incorrect
630     */

631
632         
633     void M002() throws StandardException, T_Fail {
634
635         Lockable[] locks = new T_L1[1];
636         locks[0] = new T_L1();
637
638         T_User u1 = new T_User(2, lf, locks, ITERATIONS, 10 * ITERATIONS);
639         T_User u2 = new T_User(2, lf, locks, ITERATIONS, 20 * ITERATIONS);
640         T_User u3 = new T_User(2, lf, locks, ITERATIONS, 30 * ITERATIONS);
641         Thread JavaDoc t1 = new Thread JavaDoc(u1);
642         Thread JavaDoc t2 = new Thread JavaDoc(u2);
643         Thread JavaDoc t3 = new Thread JavaDoc(u3);
644
645         t1.start();
646         t2.start();
647         t3.start();
648
649         try {
650             t1.join();
651             t2.join();
652             t3.join();
653         } catch (InterruptedException JavaDoc ie) {
654             throw T_Fail.exceptionFail(ie);
655         }
656
657         if (u1.error != null)
658             throw T_Fail.exceptionFail(u1.error);
659         if (u2.error != null)
660             throw T_Fail.exceptionFail(u2.error);
661         if (u3.error != null)
662             throw T_Fail.exceptionFail(u3.error);
663
664
665         PASS("M002");
666     }
667     /**
668         Multi-user test 003
669
670         Create a single lockable and have three threads lock it, yield and
671         then release it. The single lockable is a semaphore that can have two lockers.
672
673         @exception StandardException An exception thrown by a method of LockFactory
674         @exception T_Fail Some behaviour of the LockFactory is incorrect
675     */

676
677         
678     void M003() throws StandardException, T_Fail {
679
680         Lockable[] locks = new Lockable[1];
681         locks[0] = new T_L2(2);
682
683         T_User u1 = new T_User(3, lf, locks, ITERATIONS, 0);
684         T_User u2 = new T_User(3, lf, locks, ITERATIONS, 0);
685         T_User u3 = new T_User(3, lf, locks, ITERATIONS, 0);
686         Thread JavaDoc t1 = new Thread JavaDoc(u1);
687         Thread JavaDoc t2 = new Thread JavaDoc(u2);
688         Thread JavaDoc t3 = new Thread JavaDoc(u3);
689
690         t1.start();
691         t2.start();
692         t3.start();
693
694         try {
695             t1.join();
696             t2.join();
697             t3.join();
698         } catch (InterruptedException JavaDoc ie) {
699             throw T_Fail.exceptionFail(ie);
700         }
701
702         if (u1.error != null)
703             throw T_Fail.exceptionFail(u1.error);
704         if (u2.error != null)
705             throw T_Fail.exceptionFail(u2.error);
706         if (u3.error != null)
707             throw T_Fail.exceptionFail(u3.error);
708
709
710         PASS("M003");
711     }
712     /**
713         Multi-user test 004
714
715         As M003 but each thread will lock the object twice, to ensure that
716         lock manager grantes the lock when the compatability space and qualifier
717         match.
718
719         @exception StandardException An exception thrown by a method of LockFactory
720         @exception T_Fail Some behaviour of the LockFactory is incorrect
721     */

722
723         
724     void M004() throws StandardException, T_Fail {
725
726         Lockable[] locks = new Lockable[1];
727         locks[0] = new T_L2(2);
728
729         T_User u1 = new T_User(4, lf, locks, ITERATIONS, 0);
730         T_User u2 = new T_User(4, lf, locks, ITERATIONS, 0);
731         T_User u3 = new T_User(4, lf, locks, ITERATIONS, 0);
732         Thread JavaDoc t1 = new Thread JavaDoc(u1);
733         Thread JavaDoc t2 = new Thread JavaDoc(u2);
734         Thread JavaDoc t3 = new Thread JavaDoc(u3);
735
736         t1.start();
737         t2.start();
738         t3.start();
739
740         try {
741             t1.join();
742             t2.join();
743             t3.join();
744         } catch (InterruptedException JavaDoc ie) {
745             throw T_Fail.exceptionFail(ie);
746         }
747
748         if (u1.error != null)
749             throw T_Fail.exceptionFail(u1.error);
750         if (u2.error != null)
751             throw T_Fail.exceptionFail(u2.error);
752         if (u3.error != null)
753             throw T_Fail.exceptionFail(u3.error);
754
755
756         PASS("M004");
757     }
758
759
760
761     /*
762     ** Utility functions
763     */

764
765     /**
766         Check to see if the total number of locks we have is as expected.
767
768         @exception T_Fail Number of locks is not as expected.
769     */

770     void checkLockCount(Object JavaDoc cs, int expected) throws T_Fail {
771         boolean expect = expected != 0;
772         boolean got = lf.areLocksHeld(cs);
773         if (got != expect)
774             throw T_Fail.testFailMsg("Expected lock count (" + expect + "), got (" + got + ")");
775     }
776
777     /**
778         Check to see if the number of locks in a group we have is as expected.
779
780         @exception T_Fail Number of locks is not as expected.
781     */

782
783     void checkLockGroupCount(Object JavaDoc cs, Object JavaDoc group, int expected) throws T_Fail {
784         boolean expect = expected != 0;
785         boolean got = lf.areLocksHeld(cs, group);
786         if (got != expect)
787             throw T_Fail.testFailMsg("Expected lock count (" + expect + "), got (" + got + ")");
788     }
789
790 }
791
792
793
Popular Tags