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