(no commit message)
[utils] / support / test / org / wamblee / concurrency / JvmLockTest.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 class JvmLockTest extends TestCase {
28
29     private static final int SLEEP_TIME = 1000;
30
31     private static final String STARTED = "started";
32
33     private static final String ACQUIRED = "acquired";
34
35     private static final String RELEASED = "released";
36
37     private JvmLock _lock;
38
39     private EventTracker<String> _tracker;
40
41     /*
42      * (non-Javadoc)
43      * 
44      * @see junit.framework.TestCase#setUp()
45      */
46     @Override
47     protected void setUp() throws Exception {
48         _lock = new JvmLock();
49         _tracker = new EventTracker<String>();
50     }
51
52     private Thread runThread() {
53         Thread t = new Thread(new Runnable() {
54             public void run() {
55                 _tracker.eventOccurred(STARTED);
56                 _lock.acquire();
57                 _tracker.eventOccurred(ACQUIRED);
58                 TimingUtils.sleep(SLEEP_TIME);
59                 _lock.release();
60                 _tracker.eventOccurred(RELEASED);
61             };
62         });
63         t.start();
64         return t;
65     }
66
67     /**
68      * Tests the operation of the lock.
69      */
70     public void testLock() throws InterruptedException {
71         Thread t1 = runThread();
72         Thread t2 = runThread();
73         TimingUtils.sleep(SLEEP_TIME / 10); // give threads a chance to start
74         // up.
75         assertEquals(2, _tracker.getEventCount(STARTED)); // both threads
76         // should have
77         // started.
78         assertEquals(1, _tracker.getEventCount(ACQUIRED)); // one thread has
79                                                             // acquired the
80                                                             // lock.
81         TimingUtils.sleep(SLEEP_TIME);
82         assertEquals(2, _tracker.getEventCount(ACQUIRED)); // now the other
83                                                             // thread could also
84                                                             // acquire the lock
85         assertEquals(1, _tracker.getEventCount(RELEASED)); // and the first
86                                                             // thread has
87                                                             // released it.
88         TimingUtils.sleep(SLEEP_TIME);
89         assertEquals(2, _tracker.getEventCount(RELEASED)); // both threads
90                                                             // should be
91                                                             // finished.
92         t1.join();
93         t2.join();
94     }
95 }