(no commit message)
[utils] / support / test / org / wamblee / concurrency / AbstractLockTestCase.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.concurrency;
18
19 import org.wamblee.test.EventTracker;
20 import org.wamblee.test.TimingUtils;
21
22 import junit.framework.TestCase;
23
24 /**
25  * Tests for the JVMLock.
26  */
27 public abstract class AbstractLockTestCase extends TestCase {
28
29     protected static final int SLEEP_TIME = 1000;
30
31     protected static final String STARTED = "started";
32
33     protected static final String ACQUIRED = "acquired";
34
35     protected static final String RELEASED = "released";
36
37     private EventTracker<String> _tracker;
38
39     /*
40      * (non-Javadoc)
41      * 
42      * @see junit.framework.TestCase#setUp()
43      */
44     @Override
45     protected void setUp() throws Exception {
46         _tracker = new EventTracker<String>();
47     }
48     
49     protected EventTracker<String> getTracker() { 
50         return _tracker;
51     }
52
53     /**
54      * Must be implemented to generate the events 
55      * {@link #STARTED}, {@link #ACQUIRED}, and {@link #RELEASED} in 
56      * that order. The lock should be acquired for
57      * the time specified by {@link #SLEEP_TIME}.
58      * @return Thread which does the work. 
59      */
60     protected abstract Thread runThread();
61
62     /**
63      * Tests the operation of the lock.
64      */
65     public void testLock() throws InterruptedException {
66         Thread t1 = runThread();
67         Thread t2 = runThread();
68         TimingUtils.sleep(SLEEP_TIME / 10); // give threads a chance to start
69         // up.
70         assertEquals(2, _tracker.getEventCount(STARTED)); // both threads
71         // should have
72         // started.
73         assertEquals(1, _tracker.getEventCount(ACQUIRED)); // one thread has
74                                                             // acquired the
75                                                             // lock.
76         TimingUtils.sleep(SLEEP_TIME);
77         assertEquals(2, _tracker.getEventCount(ACQUIRED)); // now the other
78                                                             // thread could also
79                                                             // acquire the lock
80         assertEquals(1, _tracker.getEventCount(RELEASED)); // and the first
81                                                             // thread has
82                                                             // released it.
83         TimingUtils.sleep(SLEEP_TIME);
84         assertEquals(2, _tracker.getEventCount(RELEASED)); // both threads
85                                                             // should be
86                                                             // finished.
87         t1.join();
88         t2.join();
89     }
90 }