2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.concurrency;
18 import junit.framework.TestCase;
20 import org.wamblee.test.EventTracker;
21 import org.wamblee.test.TimingUtils;
24 * Tests for the JVMLock.
26 * @author Erik Brakkee
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;
38 * @see junit.framework.TestCase#setUp()
41 protected void setUp() throws Exception {
42 tracker = new EventTracker<String>();
45 protected EventTracker<String> getTracker() {
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}.
54 * @return Thread which does the work.
56 protected abstract Thread runThread();
59 * Tests the operation of the lock.
61 public void testLock() throws InterruptedException {
62 Thread t1 = runThread();
63 Thread t2 = runThread();
64 TimingUtils.sleep(SLEEP_TIME / 10); // give threads a chance to start
67 assertEquals(2, tracker.getEventCount(STARTED)); // both threads
71 assertEquals(1, tracker.getEventCount(ACQUIRED)); // one thread has
75 TimingUtils.sleep(SLEEP_TIME);
76 assertEquals(2, tracker.getEventCount(ACQUIRED)); // now the other
80 assertEquals(1, tracker.getEventCount(RELEASED)); // and the first
84 TimingUtils.sleep(SLEEP_TIME);
85 assertEquals(2, tracker.getEventCount(RELEASED)); // both threads