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.
17 package org.wamblee.concurrency;
19 import org.wamblee.test.EventTracker;
20 import org.wamblee.test.TimingUtils;
22 import junit.framework.TestCase;
25 * Tests for the JVMLock.
27 * @author Erik Brakkee
29 public abstract class AbstractLockTestCase extends TestCase {
31 protected static final int SLEEP_TIME = 1000;
33 protected static final String STARTED = "started";
35 protected static final String ACQUIRED = "acquired";
37 protected static final String RELEASED = "released";
39 private EventTracker<String> _tracker;
44 * @see junit.framework.TestCase#setUp()
47 protected void setUp() throws Exception {
48 _tracker = new EventTracker<String>();
51 protected EventTracker<String> getTracker() {
56 * Must be implemented to generate the events
57 * {@link #STARTED}, {@link #ACQUIRED}, and {@link #RELEASED} in
58 * that order. The lock should be acquired for
59 * the time specified by {@link #SLEEP_TIME}.
60 * @return Thread which does the work.
62 protected abstract Thread runThread();
65 * Tests the operation of the lock.
67 public void testLock() throws InterruptedException {
68 Thread t1 = runThread();
69 Thread t2 = runThread();
70 TimingUtils.sleep(SLEEP_TIME / 10); // give threads a chance to start
72 assertEquals(2, _tracker.getEventCount(STARTED)); // both threads
75 assertEquals(1, _tracker.getEventCount(ACQUIRED)); // one thread has
78 TimingUtils.sleep(SLEEP_TIME);
79 assertEquals(2, _tracker.getEventCount(ACQUIRED)); // now the other
82 assertEquals(1, _tracker.getEventCount(RELEASED)); // and the first
85 TimingUtils.sleep(SLEEP_TIME);
86 assertEquals(2, _tracker.getEventCount(RELEASED)); // both threads