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.Assert;
19 import junit.framework.TestCase;
23 * Testing the read-write lock class. Note: in case of problems, test cases
28 public class ReadWriteLockTest extends TestCase {
32 private static final int HALF_SECOND = 500;
36 private static final int ONE_SECOND = 1000;
40 private static final int TWO_SECONDS = 2000;
41 private ReadWriteLock _lock;
42 private int _nReaders;
43 private int _nWriters;
46 * Constructor for ReadWriteLockTest.
50 public ReadWriteLockTest(String aName) {
54 private synchronized int getReaderCount() {
58 private synchronized int getWriterCount() {
62 synchronized void incrementReaderCount() {
66 synchronized void incrementWriterCount() {
70 synchronized void decrementReaderCount() {
74 synchronized void decrementWriterCount() {
79 * @see TestCase#setUp()
81 protected void setUp() throws Exception {
83 _lock = new ReadWriteLock();
87 * @see TestCase#tearDown()
89 protected void tearDown() throws Exception {
95 * Acquire and release a read lock.
97 public void testRead() {
103 * Acquire and release a write lock.
105 public void testWrite() {
106 _lock.acquireWrite();
107 _lock.releaseWrite();
111 * Verify concurrent access by multiple readers is possible.
113 * @throws InterruptedException May not occur.
115 public void testMultipleReaders() throws InterruptedException {
116 Runnable runnable = new ReadLocker(_lock, this, TWO_SECONDS);
118 Thread t1 = new Thread(runnable);
121 Thread t2 = new Thread(runnable);
123 Thread.sleep(ONE_SECOND);
124 assertTrue("Not enough readers!", getReaderCount() == 2);
130 * Verify that only one writer at a time can acquire the write lock.
132 * @throws InterruptedException May not occur.
134 public void testSingleWriter() throws InterruptedException {
135 WriteLocker writer = new WriteLocker(_lock, this, ONE_SECOND);
136 Thread t1 = new Thread(writer);
137 Thread t2 = new Thread(writer);
141 Thread.sleep(HALF_SECOND);
142 assertTrue("Wrong writer count: " + getWriterCount(),
143 getWriterCount() == 1);
149 * Verify that multiple writers cannot acquire the write lock concurrently.
151 * @throws InterruptedException May not occur.
153 public void testMultipleWriters() throws InterruptedException {
154 WriteLocker writer1 = new WriteLocker(_lock, this, HALF_SECOND + ONE_SECOND);
155 WriteLocker writer2 = new WriteLocker(_lock, this, ONE_SECOND);
156 Thread t1 = new Thread(writer1);
157 Thread t2 = new Thread(writer2);
160 Thread.sleep(HALF_SECOND);
161 assertTrue(getWriterCount() == 1);
163 Thread.sleep(HALF_SECOND);
164 assertTrue(getWriterCount() == 1); // first writer still
167 Thread.sleep(ONE_SECOND);
169 // at t = 2, the second writer still must have
171 assertTrue(getWriterCount() == 1);
177 * Verify that after the first reader acquires a lock, a subsequent writer
178 * can only acquire the lock after the reader has released it.
180 * @throws InterruptedException May not occur.
182 public void testReadWrite1() throws InterruptedException {
183 ReadLocker readLocker = new ReadLocker(_lock, this, TWO_SECONDS);
184 Thread t1 = new Thread(readLocker);
185 WriteLocker writeLocker = new WriteLocker(_lock, this, TWO_SECONDS);
186 Thread t2 = new Thread(writeLocker);
188 t1.start(); // acquire read lock
189 Thread.sleep(HALF_SECOND);
190 assertTrue(getReaderCount() == 1);
192 Thread.sleep(HALF_SECOND);
194 // 1 second underway, reader still holding the
195 // lock so write lock cannot be acquired.
196 assertTrue(getReaderCount() == 1);
197 assertTrue(getWriterCount() == 0);
198 Thread.sleep(ONE_SECOND + HALF_SECOND);
200 // 2.5 seconds underway, read lock released and
201 // write lock must be acquired.
202 assertTrue("Wrong no. of readers: " + getReaderCount(),
203 getReaderCount() == 0);
204 assertTrue(getWriterCount() == 1);
210 * Verify that when multiple readers have acquired a read lock, the writer
211 * can only acquire the lock after all readers have released it.
213 * @throws InterruptedException May not occur.
215 public void testReadWrite2() throws InterruptedException {
216 ReadLocker readLocker1 = new ReadLocker(_lock, this, TWO_SECONDS + HALF_SECOND);
217 ReadLocker readLocker2 = new ReadLocker(_lock, this, TWO_SECONDS + HALF_SECOND);
218 Thread t1 = new Thread(readLocker1);
219 Thread t2 = new Thread(readLocker2);
220 WriteLocker writeLocker = new WriteLocker(_lock, this, TWO_SECONDS);
221 Thread t3 = new Thread(writeLocker);
223 t1.start(); // acquire read lock
224 Thread.sleep(ONE_SECOND);
225 assertTrue(getReaderCount() == 1);
227 Thread.sleep(HALF_SECOND);
228 assertTrue(getReaderCount() == 2);
230 Thread.sleep(HALF_SECOND);
233 assertTrue(getReaderCount() == 2);
234 assertTrue(getWriterCount() == 0);
235 Thread.sleep(ONE_SECOND);
237 // 3 seconds underway, first read lock must
238 // have been released.
239 assertTrue(getReaderCount() == 1);
240 assertTrue(getWriterCount() == 0);
241 Thread.sleep(HALF_SECOND);
243 // 4 seconds underway, write lock must have
245 assertTrue(getReaderCount() == 0);
246 assertTrue(getWriterCount() == 1);
254 * Verify that after a writer acquires a lock, a subsequent reader can
255 * only acquire the lock after the writer has released it.
257 * @throws InterruptedException May not occur.
259 public void testReadWrite3() throws InterruptedException {
260 ReadLocker readLocker = new ReadLocker(_lock, this, TWO_SECONDS);
261 Thread t1 = new Thread(readLocker);
262 WriteLocker writeLocker = new WriteLocker(_lock, this, TWO_SECONDS);
263 Thread t2 = new Thread(writeLocker);
265 t2.start(); // acquire write lock
266 Thread.sleep(HALF_SECOND);
267 assertTrue(getWriterCount() == 1);
269 Thread.sleep(HALF_SECOND);
271 // 1 second underway, writer still holding the
272 // lock so read lock cannot be acquired.
273 assertTrue(getWriterCount() == 1);
274 assertTrue(getReaderCount() == 0);
275 Thread.sleep(ONE_SECOND + HALF_SECOND);
277 // 2.5 seconds underway, write lock released and
278 // read lock must be acquired.
279 assertTrue("Wrong no. of writers: " + getReaderCount(),
280 getWriterCount() == 0);
281 assertTrue(getReaderCount() == 1);
287 * The following test cases are for testing whether or not
288 * the read write lock checks the locking correctly.
289 * Strictly speaking, these checks wouldn't be necessary
290 * because it involves the contract of the ReadWriteLock which
291 * must be obeyed by users of the ReadWriteLock. Nevertheless,
292 * this is tested anyway to be absolutely sure.
296 * Acquire a read lock from one thread, release it from another. Verify
297 * that a RuntimeException is thrown.
299 * @throws InterruptedException May not occur.
301 public void testReleaseReadFromWrongThread() throws InterruptedException {
305 t1 = new Thread(new Runnable() {
307 ReadWriteLockTest.this._lock.acquireRead();
311 Thread.sleep(ONE_SECOND); // wait until thread is started
312 _lock.releaseRead(); // release lock from wrong thread.
313 } catch (RuntimeException e) {
323 * Acquire a write lock from one thread, release it from another. Verify
324 * that a RuntimeException is thrown.
326 * @throws InterruptedException May not occur.
328 public void testReleaseWriteFromWrongThread() throws InterruptedException {
332 t1 = new Thread(new Runnable() {
334 ReadWriteLockTest.this._lock.acquireWrite();
338 Thread.sleep(ONE_SECOND); // wait until thread is started
339 _lock.releaseWrite(); // release lock from wrong thread.
340 } catch (RuntimeException e) {
350 * Try to acquire a read lock multiple times. Verify that a
351 * RuntimeException is thrown.
353 public void testAcquireReadTwice() {
357 } catch (RuntimeException e) {
366 * Try to acquire a write lock multiple times. Verify that a
367 * RuntimeException is thrown.
369 public void testAcquireWriteTwice() {
371 _lock.acquireWrite();
372 _lock.acquireWrite();
373 } catch (RuntimeException e) {
381 * Acquire the lock for reading and directly afterwards acquire it for
382 * writing. Verify that a RuntimeException is thrown.
384 public void testAcquireReadFollowedByWrite() {
387 _lock.acquireWrite();
388 } catch (RuntimeException e) {
396 * Acquire the lock for writing and directly afterwards acquire it for
397 * reading. Verify that a RuntimeException is thrown.
399 public void testAcquireWriteFollowedByRead() {
401 _lock.acquireWrite();
403 } catch (RuntimeException e) {
411 * Acquire a read lock and release it as a write lock. Verify that a
412 * RuntimeException is thrown.
414 public void testAcquireReadFollowedByReleaseaWrite() {
417 _lock.releaseWrite();
418 } catch (RuntimeException e) {
426 * Acquire a write lock and release it as a read lock. Verify that a
427 * RuntimeException is thrown.
429 public void testAcquireWriteFollowedByReleaseRead() {
431 _lock.acquireWrite();
433 } catch (RuntimeException e) {
443 * ReadLocker acquires a read lock and performs a callback when the lock as
444 * been acquired, sleeps for a designated amount of time, releases the read
445 * lock, and performs a callback after the lock has been released.
447 class ReadLocker implements Runnable {
448 private ReadWriteLock _lock;
449 private ReadWriteLockTest _lockTest;
450 private int _sleepTime;
452 public ReadLocker(ReadWriteLock aLock, ReadWriteLockTest aLockTest,
455 _lockTest = aLockTest;
456 _sleepTime = aSleepTime;
461 _lockTest.incrementReaderCount();
464 Thread.sleep(_sleepTime);
465 } catch (InterruptedException e) {
466 Assert.fail("ReadLocker thread was interrupted."
467 + Thread.currentThread());
471 _lockTest.decrementReaderCount();
477 * WriteLocker acquires a write lock and performs a callback when the lock as
478 * been acquired, sleeps for a designated amount of time, releases the write
479 * lock, and performs a callback after the lock has been released.
481 class WriteLocker implements Runnable {
482 private ReadWriteLock _lock;
483 private ReadWriteLockTest _lockTest;
484 private int _sleepTime;
486 public WriteLocker(ReadWriteLock aLock, ReadWriteLockTest aLockTest,
489 _lockTest = aLockTest;
490 _sleepTime = aSleepTime;
494 _lock.acquireWrite();
495 _lockTest.incrementWriterCount();
498 Thread.sleep(_sleepTime);
499 } catch (InterruptedException e) {
500 Assert.fail("WriteLocker thread was interrupted: "
501 + Thread.currentThread());
504 _lock.releaseWrite();
505 _lockTest.decrementWriterCount();