(no commit message)
[utils] / support / general / src / test / java / org / wamblee / concurrency / AbstractLockTestCase.java
1 /*
2  * Copyright 2005-2010 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 package org.wamblee.concurrency;
17
18 import junit.framework.TestCase;
19
20 import org.wamblee.test.EventTracker;
21 import org.wamblee.test.TimingUtils;
22
23 /**
24  * Tests for the JVMLock.
25  * 
26  * @author Erik Brakkee
27  */
28 public abstract class AbstractLockTestCase extends TestCase {
29     protected static final int SLEEP_TIME = 1000;
30     protected static final String STARTED = "started";
31     protected static final String ACQUIRED = "acquired";
32     protected static final String RELEASED = "released";
33     private EventTracker<String> tracker;
34
35     /*
36      * (non-Javadoc)
37      * 
38      * @see junit.framework.TestCase#setUp()
39      */
40     @Override
41     protected void setUp() throws Exception {
42         tracker = new EventTracker<String>();
43     }
44
45     protected EventTracker<String> getTracker() {
46         return tracker;
47     }
48
49     /**
50      * Must be implemented to generate the events {@link #STARTED},
51      * {@link #ACQUIRED}, and {@link #RELEASED} in that order. The lock should
52      * be acquired for the time specified by {@link #SLEEP_TIME}.
53      * 
54      * @return Thread which does the work.
55      */
56     protected abstract Thread runThread();
57
58     /**
59      * Tests the operation of the lock.
60      */
61     public void testLock() throws InterruptedException {
62         Thread t1 = runThread();
63         Thread t2 = runThread();
64         TimingUtils.sleep(SLEEP_TIME / 2); // give threads a chance to start
65         // up.
66
67         assertEquals(2, tracker.getEventCount(STARTED)); // both threads
68         // should have
69         // started.
70
71         assertEquals(1, tracker.getEventCount(ACQUIRED)); // one thread has
72         // acquired the
73         // lock.
74
75         TimingUtils.sleep(SLEEP_TIME);
76         assertEquals(2, tracker.getEventCount(ACQUIRED)); // now the other
77         // thread could also
78         // acquire the lock
79
80         assertEquals(1, tracker.getEventCount(RELEASED)); // and the first
81         // thread has
82         // released it.
83
84         TimingUtils.sleep(SLEEP_TIME);
85         assertEquals(2, tracker.getEventCount(RELEASED)); // both threads
86         // should be
87         // finished.
88
89         t1.join();
90         t2.join();
91     }
92 }