[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/lock ...
Manik Surtani
manik at jboss.org
Wed May 23 06:28:52 EDT 2007
User: msurtani
Date: 07/05/23 06:28:52
Modified: tests/functional/org/jboss/cache/lock
IdentityLockTest.java
ReentrantWriterPreference2Readers1WriterLockTest.java
WriteLockOnParentTest.java
NonBlockingWriterLockTest.java LockTest.java
ReentrantWriterPreferenceReadWriteLockTest.java
Log:
Initiated a bunch of performance fixes, including replacing CopyOnWriteArraySets with org.jboss.cache.util.concurrent.ConcurrentHashSet.
Also ran an imports optimiser on the code base - there were a lot of unused imports floating about.
Revision Changes Path
1.10 +1 -2 JBossCache/tests/functional/org/jboss/cache/lock/IdentityLockTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: IdentityLockTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/lock/IdentityLockTest.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- IdentityLockTest.java 7 Feb 2007 22:06:49 -0000 1.9
+++ IdentityLockTest.java 23 May 2007 10:28:51 -0000 1.10
@@ -10,7 +10,6 @@
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.NodeSPI;
@@ -23,7 +22,7 @@
*
* @author Bela Ban
* @author Ben Wang
- * @version $Revision: 1.9 $
+ * @version $Revision: 1.10 $
*/
public class IdentityLockTest extends TestCase
{
1.7 +96 -61 JBossCache/tests/functional/org/jboss/cache/lock/ReentrantWriterPreference2Readers1WriterLockTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ReentrantWriterPreference2Readers1WriterLockTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/lock/ReentrantWriterPreference2Readers1WriterLockTest.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- ReentrantWriterPreference2Readers1WriterLockTest.java 8 Dec 2006 18:50:49 -0000 1.6
+++ ReentrantWriterPreference2Readers1WriterLockTest.java 23 May 2007 10:28:52 -0000 1.7
@@ -10,111 +10,125 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* Tests ReentrantWriterPreferenceReadWriteLock
+ *
* @author Bela Ban
- * @version $Id: ReentrantWriterPreference2Readers1WriterLockTest.java,v 1.6 2006/12/08 18:50:49 genman Exp $
+ * @version $Id: ReentrantWriterPreference2Readers1WriterLockTest.java,v 1.7 2007/05/23 10:28:52 msurtani Exp $
*/
-public class ReentrantWriterPreference2Readers1WriterLockTest extends TestCase {
+public class ReentrantWriterPreference2Readers1WriterLockTest extends TestCase
+{
ReentrantReadWriteLock lock;
ReentrantReadWriteLock.ReadLock rl;
ReentrantReadWriteLock.WriteLock wl;
- Exception thread_ex=null;
+ Exception thread_ex = null;
- protected void setUp() throws Exception {
+ protected void setUp() throws Exception
+ {
super.setUp();
- lock=new ReentrantReadWriteLock();
- rl=lock.readLock();
- wl=lock.writeLock();
- thread_ex=null;
+ lock = new ReentrantReadWriteLock();
+ rl = lock.readLock();
+ wl = lock.writeLock();
+ thread_ex = null;
}
- protected void tearDown() throws Exception {
+ protected void tearDown() throws Exception
+ {
super.tearDown();
- lock=null;
- if(thread_ex != null)
+ lock = null;
+ if (thread_ex != null)
throw thread_ex;
}
- private void log(String msg) {
+ private void log(String msg)
+ {
System.out.println(System.currentTimeMillis() + " " + Thread.currentThread() +
" [" + Thread.currentThread().getName() + "]: " + msg);
}
- public void testSimpleUpgradeFromReadLockToWriteLock() {
+ public void testSimpleUpgradeFromReadLockToWriteLock()
+ {
int readers, writers;
- try {
+ try
+ {
rl.lock();
- readers=lock.getReadLockCount();
+ readers = lock.getReadLockCount();
assertEquals(1, readers);
- boolean wl_acquired=wl.tryLock(500, TimeUnit.MILLISECONDS);
- if(!wl_acquired) {
+ boolean wl_acquired = wl.tryLock(500, TimeUnit.MILLISECONDS);
+ if (!wl_acquired)
+ {
fail("write lock could not be acquired");
return;
}
- readers=lock.getReadLockCount();
+ readers = lock.getReadLockCount();
assertEquals(1, readers);
- writers=lock.getWriteHoldCount();
+ writers = lock.getWriteHoldCount();
assertEquals(1, writers);
}
- catch(InterruptedException e) {
+ catch (InterruptedException e)
+ {
}
- finally {
+ finally
+ {
rl.unlock();
- if(lock.getWriteHoldCount() > 0)
+ if (lock.getWriteHoldCount() > 0)
wl.unlock();
}
}
- public void test2ReadersAnd1Writer() throws InterruptedException {
+ public void test2ReadersAnd1Writer() throws InterruptedException
+ {
int readers, writers;
- Upgrader upgrader=new Upgrader("Upgrader");
- Reader reader=new Reader("Reader");
+ Upgrader upgrader = new Upgrader("Upgrader");
+ Reader reader = new Reader("Reader");
reader.start();
sleepThread(500);
- readers=lock.getReadLockCount();
+ readers = lock.getReadLockCount();
assertEquals(1, readers);
upgrader.start();
sleepThread(500);
- readers=lock.getReadLockCount();
+ readers = lock.getReadLockCount();
assertEquals(2, readers);
- synchronized(upgrader) { // writer upgrades from RL to WL, this should fail
+ synchronized (upgrader)
+ { // writer upgrades from RL to WL, this should fail
upgrader.notify();
}
sleepThread(500);
- readers=lock.getReadLockCount();
+ readers = lock.getReadLockCount();
assertEquals(2, readers);
- writers=lock.getWriteHoldCount();
+ writers = lock.getWriteHoldCount();
assertEquals(0, writers);
- synchronized(reader) { // reader unlocks its RL, now writer should be able to upgrade to a WL
+ synchronized (reader)
+ { // reader unlocks its RL, now writer should be able to upgrade to a WL
reader.notify();
}
reader.join();
- readers=lock.getReadLockCount();
+ readers = lock.getReadLockCount();
assertEquals(1, readers);
- writers=lock.getWriteHoldCount();
+ writers = lock.getWriteHoldCount();
assertEquals(1, writers);
- synchronized(upgrader) { // writer releases WL
+ synchronized (upgrader)
+ { // writer releases WL
upgrader.notify();
}
sleepThread(500);
- readers=lock.getReadLockCount();
+ readers = lock.getReadLockCount();
assertEquals(0, readers);
- writers=lock.getWriteHoldCount();
+ writers = lock.getWriteHoldCount();
assertEquals(0, writers);
upgrader.join(3000);
@@ -123,24 +137,31 @@
}
- private class Reader extends Thread {
+ private class Reader extends Thread
+ {
- public Reader(String name) {
+ public Reader(String name)
+ {
super(name);
}
- public void run() {
- try {
+ public void run()
+ {
+ try
+ {
log("acquiring RL");
rl.lock();
log("acquired RL");
- synchronized(this) {
+ synchronized (this)
+ {
this.wait();
}
}
- catch(InterruptedException e) {
+ catch (InterruptedException e)
+ {
}
- finally {
+ finally
+ {
log("releasing RL");
rl.unlock();
log("released RL");
@@ -149,43 +170,52 @@
}
- private class Upgrader extends Thread {
- boolean upgradeSuccessful=false;
+ private class Upgrader extends Thread
+ {
+ boolean upgradeSuccessful = false;
- public Upgrader(String name) {
+ public Upgrader(String name)
+ {
super(name);
}
- public boolean wasUpgradeSuccessful() {
+ public boolean wasUpgradeSuccessful()
+ {
return upgradeSuccessful;
}
- public void run() {
- try {
+ public void run()
+ {
+ try
+ {
log("acquiring RL");
rl.lock();
log("acquired RL");
- synchronized(this) {
+ synchronized (this)
+ {
this.wait();
}
log("attempting to acquire WL");
wl.lock();
- upgradeSuccessful=true;
+ upgradeSuccessful = true;
log("acquired WL");
- synchronized(this) {
+ synchronized (this)
+ {
this.wait();
}
log("releasing WL");
rl.unlock();
log("released WL");
}
- catch(InterruptedException e) {
+ catch (InterruptedException e)
+ {
;
}
- finally {
+ finally
+ {
wl.unlock();
rl.unlock();
}
@@ -193,19 +223,24 @@
}
- static void sleepThread(long timeout) {
- try {
+ static void sleepThread(long timeout)
+ {
+ try
+ {
Thread.sleep(timeout);
}
- catch(InterruptedException e) {
+ catch (InterruptedException e)
+ {
}
}
- public static Test suite() {
+ public static Test suite()
+ {
return new TestSuite(ReentrantWriterPreference2Readers1WriterLockTest.class);
}
- public static void main(String[] args) {
+ public static void main(String[] args)
+ {
junit.textui.TestRunner.run(ReentrantWriterPreference2Readers1WriterLockTest.suite());
}
1.4 +3 -5 JBossCache/tests/functional/org/jboss/cache/lock/WriteLockOnParentTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: WriteLockOnParentTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/lock/WriteLockOnParentTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- WriteLockOnParentTest.java 7 Feb 2007 22:06:49 -0000 1.3
+++ WriteLockOnParentTest.java 23 May 2007 10:28:52 -0000 1.4
@@ -1,16 +1,14 @@
package org.jboss.cache.lock;
import junit.framework.TestCase;
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Fqn;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import java.util.Collections;
-import org.jboss.cache.DefaultCacheFactory;
-import org.jboss.cache.Cache;
-import org.jboss.cache.CacheSPI;
-import org.jboss.cache.Fqn;
-
public class WriteLockOnParentTest extends TestCase
{
private CacheSPI cache;
1.4 +241 -190 JBossCache/tests/functional/org/jboss/cache/lock/NonBlockingWriterLockTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: NonBlockingWriterLockTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/lock/NonBlockingWriterLockTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- NonBlockingWriterLockTest.java 8 Dec 2006 18:50:49 -0000 1.3
+++ NonBlockingWriterLockTest.java 23 May 2007 10:28:52 -0000 1.4
@@ -1,7 +1,5 @@
package org.jboss.cache.lock;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.Lock;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@@ -11,6 +9,8 @@
import java.io.IOException;
import java.io.Writer;
import java.util.Vector;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
/**
* NonBlockingWriterLock is a read/write lock (with upgrade) that has
@@ -59,13 +59,15 @@
return suite;
}
- public void setUp() throws Exception {
+ public void setUp() throws Exception
+ {
super.setUp();
logX("\n");
log("Setting up test case ...");
}
- public void tearDown() throws Exception {
+ public void tearDown() throws Exception
+ {
super.tearDown();
log("Tearing down test case ...");
}
@@ -116,22 +118,23 @@
* <DD> 0 - indicating the locking request failed.
* <DD> 1 - indicating the locking request succeeded.
* </DL>
- * <p>
+ * <p/>
* After all threads in each test case terminate, the test case
* should make the following call to verify the test result:
* <DL>
* <DD> asssertTrue(checkLockingResult(expected-result-string);
* </DL>
- * <p>
+ * <p/>
* 'expected-result-string' is the locking result string
* described above. For example, "8-t1-RL-0" means that thread
* t1 in test case #8 doing a Read Lock request expects the
* operation to fail. If the expected result string can't be
* found then the test case is considered FAILED (ie, either
* the read lock request was successful or did not complete).
- * <p>
+ * <p/>
* Each test case should also call cleanLockingResult() to reset
* result vector for the next test cases.
+ *
* @param caseNum Arbitrary string for the test case number.
* @param name Arbitrary string for the calling thread name.
* @param msecs Milliseconds that the thread should sleep after
@@ -152,15 +155,17 @@
public void run()
{
Lock rlock = lock_.readLock();
- try {
- if (! rlock.tryLock(msecs, TimeUnit.MILLISECONDS)) {
- logX(caseNum+"-"+name+" requesting read lock failed!\n");
+ try
+ {
+ if (!rlock.tryLock(msecs, TimeUnit.MILLISECONDS))
+ {
+ logX(caseNum + "-" + name + " requesting read lock failed!\n");
String str = caseNum + "-" + name + "-RL-0";
postLockingResult(str);
return;
}
// OK, read lock obtained, sleep and release it.
- logX(caseNum+"-"+name+" requesting read lock succeeded!\n");
+ logX(caseNum + "-" + name + " requesting read lock succeeded!\n");
String str = caseNum + "-" + name + "-RL-1";
postLockingResult(str);
TestingUtil.sleepThread(sleepSecs);
@@ -173,8 +178,10 @@
acquireUpgradeLock(caseNum, name, msecs, errMsg);
rlock.unlock();
- logX(caseNum+"-"+name+" releasing read lock.\n");
- } catch (Exception ex) {
+ logX(caseNum + "-" + name + " releasing read lock.\n");
+ }
+ catch (Exception ex)
+ {
}
}
};
@@ -184,6 +191,7 @@
* Creates a new thread and acquires a write lock with a timeout
* value specified by the caller. Similar to {@link #readThread readThread()}
* except it's used for write locks.
+ *
* @see #readThread readThread()
*/
protected Thread writeThread(final String caseNum, final String name,
@@ -194,16 +202,18 @@
{
public void run()
{
- try {
+ try
+ {
Lock wlock = lock_.writeLock();
- if (! wlock.tryLock(msecs, TimeUnit.MILLISECONDS)) {
- logX(caseNum+"-"+name+" requesting write lock failed!\n");
+ if (!wlock.tryLock(msecs, TimeUnit.MILLISECONDS))
+ {
+ logX(caseNum + "-" + name + " requesting write lock failed!\n");
String str = caseNum + "-" + name + "-WL-0";
postLockingResult(str);
return;
}
// OK, write lock obtained, sleep and release it.
- logX(caseNum+"-"+name+" requesting write lock succeeded!\n");
+ logX(caseNum + "-" + name + " requesting write lock succeeded!\n");
String str = caseNum + "-" + name + "-WL-1";
postLockingResult(str);
TestingUtil.sleepThread(sleepSecs);
@@ -216,8 +226,10 @@
acquireUpgradeLock(caseNum, name, msecs, errMsg);
wlock.unlock();
- logX(caseNum+"-"+name+" releasing write lock.\n");
- } catch (Exception ex) {
+ logX(caseNum + "-" + name + " releasing write lock.\n");
+ }
+ catch (Exception ex)
+ {
}
}
};
@@ -228,6 +240,7 @@
* and then tries to upgrade the read lock to a write one. Similar
* to {@link #readThread readThread()} except it's used for upgrading
* locks.
+ *
* @see #readThread readThread()
*/
protected Thread upgradeThread(final String caseNum, final String name,
@@ -237,27 +250,29 @@
{
public void run()
{
- try {
+ try
+ {
Lock rlock = lock_.readLock();
Lock wlock = null;
- if (! rlock.tryLock(msecs, TimeUnit.MILLISECONDS)) {
- logX(caseNum+"-"+name+" requesting read lock failed!\n");
+ if (!rlock.tryLock(msecs, TimeUnit.MILLISECONDS))
+ {
+ logX(caseNum + "-" + name + " requesting read lock failed!\n");
String str = caseNum + "-" + name + "-RL-0";
postLockingResult(str);
return;
}
// OK, read lock obtained, sleep and upgrade it later.
- logX(caseNum+"-"+name+" requesting read lock succeeded (upgrade later)!\n");
- TestingUtil.sleepThread(SLEEP_MSECS/2);
+ logX(caseNum + "-" + name + " requesting read lock succeeded (upgrade later)!\n");
+ TestingUtil.sleepThread(SLEEP_MSECS / 2);
String str = caseNum + "-" + name + "-UL-";
if ((wlock = lock_.upgradeLockAttempt(msecs)) == null)
{
- logX(caseNum+"-"+name+" requesting upgrade lock failed!\n");
+ logX(caseNum + "-" + name + " requesting upgrade lock failed!\n");
str += "0";
}
else
{
- logX(caseNum+"-"+name+" requesting upgrade lock succeeded!\n");
+ logX(caseNum + "-" + name + " requesting upgrade lock succeeded!\n");
str += "1";
}
postLockingResult(str);
@@ -266,10 +281,12 @@
if (wlock != null)
{
wlock.unlock();
- logX(caseNum+"-"+name+" releasing upgrade lock.\n");
+ logX(caseNum + "-" + name + " releasing upgrade lock.\n");
}
rlock.unlock();
- } catch (Exception ex) {
+ }
+ catch (Exception ex)
+ {
}
}
};
@@ -288,22 +305,26 @@
protected void acquireReadLock(final String caseNum, final String name,
final long msecs, final String errMsg)
{
- try {
+ try
+ {
Lock rlock = lock_.readLock();
- if (! rlock.tryLock(msecs, TimeUnit.MILLISECONDS)) {
- logX(caseNum+"-"+name+" requesting read lock failed!\n");
+ if (!rlock.tryLock(msecs, TimeUnit.MILLISECONDS))
+ {
+ logX(caseNum + "-" + name + " requesting read lock failed!\n");
String str = caseNum + "-" + name + "-RL-0";
postLockingResult(str);
return;
}
// OK, read lock obtained, sleep and release it.
- logX(caseNum+"-"+name+" requesting read lock succeeded!\n");
+ logX(caseNum + "-" + name + " requesting read lock succeeded!\n");
String str = caseNum + "-" + name + "-RL-1";
postLockingResult(str);
TestingUtil.sleepThread(SLEEP_MSECS);
rlock.unlock();
- logX(caseNum+"-"+name+" releasing read lock.\n");
- } catch (Exception ex) {
+ logX(caseNum + "-" + name + " releasing read lock.\n");
+ }
+ catch (Exception ex)
+ {
}
}
@@ -314,22 +335,26 @@
protected void acquireWriteLock(final String caseNum, final String name,
final long msecs, final String errMsg)
{
- try {
+ try
+ {
Lock wlock = lock_.writeLock();
- if (! wlock.tryLock(msecs, TimeUnit.MILLISECONDS)) {
- logX(caseNum+"-"+name+" requesting write lock failed!\n");
+ if (!wlock.tryLock(msecs, TimeUnit.MILLISECONDS))
+ {
+ logX(caseNum + "-" + name + " requesting write lock failed!\n");
String str = caseNum + "-" + name + "-WL-0";
postLockingResult(str);
return;
}
// OK, write lock obtained, sleep and release it.
- logX(caseNum+"-"+name+" requesting write lock succeeded!\n");
+ logX(caseNum + "-" + name + " requesting write lock succeeded!\n");
String str = caseNum + "-" + name + "-WL-1";
postLockingResult(str);
TestingUtil.sleepThread(SLEEP_MSECS);
wlock.unlock();
- logX(caseNum+"-"+name+" releasing write lock.\n");
- } catch (Exception ex) {
+ logX(caseNum + "-" + name + " releasing write lock.\n");
+ }
+ catch (Exception ex)
+ {
}
}
@@ -340,22 +365,26 @@
protected void acquireUpgradeLock(final String caseNum, final String name,
final long msecs, final String errMsg)
{
- try {
+ try
+ {
Lock ulock = null;
- if ((ulock = lock_.upgradeLockAttempt(msecs)) == null) {
- logX(caseNum+"-"+name+" requesting upgrade lock failed!\n");
+ if ((ulock = lock_.upgradeLockAttempt(msecs)) == null)
+ {
+ logX(caseNum + "-" + name + " requesting upgrade lock failed!\n");
String str = caseNum + "-" + name + "-UL-0";
postLockingResult(str);
return;
}
// OK, write lock obtained, sleep and release it.
- logX(caseNum+"-"+name+" requesting upgrade lock succeeded!\n");
+ logX(caseNum + "-" + name + " requesting upgrade lock succeeded!\n");
String str = caseNum + "-" + name + "-UL-1";
postLockingResult(str);
TestingUtil.sleepThread(SLEEP_MSECS);
ulock.unlock();
- logX(caseNum+"-"+name+" releasing upgrade lock.\n");
- } catch (Exception ex) {
+ logX(caseNum + "-" + name + " releasing upgrade lock.\n");
+ }
+ catch (Exception ex)
+ {
}
}
@@ -387,10 +416,10 @@
protected synchronized boolean checkLockingResult(String expected)
{
boolean rc = false;
- for (int i=0; i<lockResult.size(); i++)
+ for (int i = 0; i < lockResult.size(); i++)
{
Object ele = lockResult.elementAt(i);
- String str = (String)ele;
+ String str = (String) ele;
if (expected.equals(str))
{
rc = true;
@@ -407,211 +436,233 @@
/***************************************************************/
/* T e s t C a s e s */
/***************************************************************/
- /** Case #10 - T1 acquires RL, T2 acquires RL followed by WL. */
+ /**
+ * Case #10 - T1 acquires RL, T2 acquires RL followed by WL.
+ */
public void testWriteWithMultipleReaders() throws Exception
{
String caseNum = "10";
- Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS*2,
+ Thread t1 = readThread(caseNum, "t1", 0, SLEEP_MSECS * 2,
"1st read lock attempt failed", NO_MORE_OP);
- Thread t2=readThread(caseNum, "t2", 0, SLEEP_MSECS,
+ Thread t2 = readThread(caseNum, "t2", 0, SLEEP_MSECS,
"2nd read lock attempt failed", INVOKE_WRITE);
t1.start();
t2.start();
t1.join(3000);
t2.join(3000);
- assertTrue(checkLockingResult(caseNum+"-t1-RL-1") &&
- checkLockingResult(caseNum+"-t2-RL-1") &&
- checkLockingResult(caseNum+"-t2-WL-1"));
+ assertTrue(checkLockingResult(caseNum + "-t1-RL-1") &&
+ checkLockingResult(caseNum + "-t2-RL-1") &&
+ checkLockingResult(caseNum + "-t2-WL-1"));
cleanLockingResult();
// possilbe deadlock check
if (t1.isAlive() || t2.isAlive())
fail("Possible deadlock resulted in testRead.");
}
- /** Case #11 - T1 acquires RL followed by WL, T2 acquires RL. */
+ /**
+ * Case #11 - T1 acquires RL followed by WL, T2 acquires RL.
+ */
public void testUpgradeWithMultipleReadersOn1() throws Exception
{
String caseNum = "11";
- Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS,
+ Thread t1 = readThread(caseNum, "t1", 0, SLEEP_MSECS,
"1st read lock attempt failed", INVOKE_WRITE);
- Thread t2=readThread(caseNum, "t2", 0, SLEEP_MSECS*2,
+ Thread t2 = readThread(caseNum, "t2", 0, SLEEP_MSECS * 2,
"2nd read lock attempt failed", NO_MORE_OP);
t1.start();
t2.start();
t1.join(3000);
t2.join(3000);
- assertTrue(checkLockingResult(caseNum+"-t1-RL-1") &&
- checkLockingResult(caseNum+"-t2-RL-1") &&
- checkLockingResult(caseNum+"-t1-WL-1"));
+ assertTrue(checkLockingResult(caseNum + "-t1-RL-1") &&
+ checkLockingResult(caseNum + "-t2-RL-1") &&
+ checkLockingResult(caseNum + "-t1-WL-1"));
cleanLockingResult();
// possilbe deadlock check
if (t1.isAlive() || t2.isAlive())
fail("Possible deadlock resulted in testRead.");
}
- /** Case #2 - T1 acquires RL followed by UL. */
+ /**
+ * Case #2 - T1 acquires RL followed by UL.
+ */
public void testUpgradeReadLock() throws Exception
{
String caseNum = "2";
- Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS,
+ Thread t1 = readThread(caseNum, "t1", 0, SLEEP_MSECS,
"1st read lock attempt failed", INVOKE_UPGRADE);
t1.start();
t1.join(3000);
- assertTrue(checkLockingResult(caseNum+"-t1-RL-1") &&
- checkLockingResult(caseNum+"-t1-UL-1"));
+ assertTrue(checkLockingResult(caseNum + "-t1-RL-1") &&
+ checkLockingResult(caseNum + "-t1-UL-1"));
cleanLockingResult();
}
- /** Case #3 - T1 acquires RL followed by WL. */
+ /**
+ * Case #3 - T1 acquires RL followed by WL.
+ */
public void testReadThenWrite() throws Exception
{
String caseNum = "3";
acquireReadLock(caseNum, "t1", 0, "1st read lock attempt failed");
acquireWriteLock(caseNum, "t1.1", 0, "2nd write lock attempt failed");
- assertTrue(checkLockingResult(caseNum+"-t1-RL-1") &&
- checkLockingResult(caseNum+"-t1.1-WL-1"));
+ assertTrue(checkLockingResult(caseNum + "-t1-RL-1") &&
+ checkLockingResult(caseNum + "-t1.1-WL-1"));
cleanLockingResult();
}
- /** Case #5 - T1 acquires WL followed by RL.*/
+ /**
+ * Case #5 - T1 acquires WL followed by RL.
+ */
public void testWriteThenRead() throws Exception
{
String caseNum = "5";
acquireWriteLock(caseNum, "t1", 0, "1st write lock attempt failed");
acquireReadLock(caseNum, "t1.1", 0, "2nd read lock attempt failed");
- assertTrue(checkLockingResult(caseNum+"-t1-WL-1") &&
- checkLockingResult(caseNum+"-t1.1-RL-1"));
+ assertTrue(checkLockingResult(caseNum + "-t1-WL-1") &&
+ checkLockingResult(caseNum + "-t1.1-RL-1"));
cleanLockingResult();
}
- /** Case #6 - T1 acquires RL, T2 acquires RL.*/
+ /**
+ * Case #6 - T1 acquires RL, T2 acquires RL.
+ */
public void testMultipleReadlock() throws Exception
{
String caseNum = "6";
- Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS,
+ Thread t1 = readThread(caseNum, "t1", 0, SLEEP_MSECS,
"1st read lock attempt failed", NO_MORE_OP);
- Thread t2=readThread(caseNum, "t2", 0, SLEEP_MSECS,
+ Thread t2 = readThread(caseNum, "t2", 0, SLEEP_MSECS,
"2nd read lock attempt failed", NO_MORE_OP);
t1.start();
t2.start();
t1.join(3000);
t2.join(3000);
- assertTrue(checkLockingResult(caseNum+"-t1-RL-1") &&
- checkLockingResult(caseNum+"-t2-RL-1"));
+ assertTrue(checkLockingResult(caseNum + "-t1-RL-1") &&
+ checkLockingResult(caseNum + "-t2-RL-1"));
cleanLockingResult();
// possilbe deadlock check
if (t1.isAlive() || t2.isAlive())
fail("Possible deadlock resulted in testRead.");
}
- /** Case #8 - T1 acquires RL, T2 acquires WL.*/
+ /**
+ * Case #8 - T1 acquires RL, T2 acquires WL.
+ */
public void testWriteWithExistingReader() throws Exception
{
String caseNum = "8";
- Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS,
+ Thread t1 = readThread(caseNum, "t1", 0, SLEEP_MSECS,
"1st write lock attempt failed", NO_MORE_OP);
- Thread t2=writeThread(caseNum, "t2", 0, SLEEP_MSECS,
+ Thread t2 = writeThread(caseNum, "t2", 0, SLEEP_MSECS,
"2nd read lock attempt failed", NO_MORE_OP);
t1.start();
t2.start();
t1.join(3000);
t2.join(3000);
- assertTrue(checkLockingResult(caseNum+"-t1-RL-1") &&
- checkLockingResult(caseNum+"-t2-WL-1"));
+ assertTrue(checkLockingResult(caseNum + "-t1-RL-1") &&
+ checkLockingResult(caseNum + "-t2-WL-1"));
cleanLockingResult();
// possilbe deadlock check
if (t1.isAlive() || t2.isAlive())
fail("Possible deadlock resulted in testRead.");
}
- /** Case #13 - T1 acquires RL, T2 acquires WL.*/
+ /**
+ * Case #13 - T1 acquires RL, T2 acquires WL.
+ */
public void testReadWithExistingWriter() throws Exception
{
String caseNum = "13";
- Thread t1=writeThread(caseNum, "t1", 0, SLEEP_MSECS,
+ Thread t1 = writeThread(caseNum, "t1", 0, SLEEP_MSECS,
"1st write lock attempt failed", NO_MORE_OP);
- Thread t2=readThread(caseNum, "t2", 0, SLEEP_MSECS,
+ Thread t2 = readThread(caseNum, "t2", 0, SLEEP_MSECS,
"2nd read lock attempt failed", NO_MORE_OP);
t1.start();
t2.start();
t1.join(3000);
t2.join(3000);
- assertTrue(checkLockingResult(caseNum+"-t1-WL-1") &&
- checkLockingResult(caseNum+"-t2-RL-0"));
+ assertTrue(checkLockingResult(caseNum + "-t1-WL-1") &&
+ checkLockingResult(caseNum + "-t2-RL-0"));
cleanLockingResult();
// possilbe deadlock check
if (t1.isAlive() || t2.isAlive())
fail("Possible deadlock resulted in testRead.");
}
- /** Case #14 - T1 acquires WL, T2 acquires WL.*/
+ /**
+ * Case #14 - T1 acquires WL, T2 acquires WL.
+ */
public void testMultipleWritelocks() throws Exception
{
String caseNum = "14";
- Thread t1=writeThread(caseNum, "t1", 0, SLEEP_MSECS,
+ Thread t1 = writeThread(caseNum, "t1", 0, SLEEP_MSECS,
"1st write lock attempt failed", NO_MORE_OP);
- Thread t2=writeThread(caseNum, "t2", 0, SLEEP_MSECS,
+ Thread t2 = writeThread(caseNum, "t2", 0, SLEEP_MSECS,
"2nd write lock attempt failed", NO_MORE_OP);
t1.start();
t2.start();
t1.join(3000);
t2.join(3000);
- assertTrue(checkLockingResult(caseNum+"-t1-WL-1") &&
- checkLockingResult(caseNum+"-t2-WL-0"));
+ assertTrue(checkLockingResult(caseNum + "-t1-WL-1") &&
+ checkLockingResult(caseNum + "-t2-WL-0"));
cleanLockingResult();
// possilbe deadlock check
if (t1.isAlive() || t2.isAlive())
fail("Possible deadlock resulted in testRead.");
}
- /** Case #7 - T1 acquires RL, T2 acquires UL.*/
+ /**
+ * Case #7 - T1 acquires RL, T2 acquires UL.
+ */
public void testUpgradeWithExistingReader() throws Exception
{
String caseNum = "7";
- Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS,
+ Thread t1 = readThread(caseNum, "t1", 0, SLEEP_MSECS,
"1st read lock attempt failed", NO_MORE_OP);
- Thread t2=upgradeThread(caseNum, "t2", 0,
+ Thread t2 = upgradeThread(caseNum, "t2", 0,
"2nd upgrade lock attempt failed");
t1.start();
t2.start();
t1.join(3000);
t2.join(3000);
- assertTrue(checkLockingResult(caseNum+"-t1-RL-1") &&
- checkLockingResult(caseNum+"-t2-UL-1"));
+ assertTrue(checkLockingResult(caseNum + "-t1-RL-1") &&
+ checkLockingResult(caseNum + "-t2-UL-1"));
cleanLockingResult();
// possilbe deadlock check
if (t1.isAlive() || t2.isAlive())
fail("Possible deadlock resulted in testRead.");
}
- /** Case #9 - T1 acquires RL, T2 acquires RL followed by UL.*/
+ /**
+ * Case #9 - T1 acquires RL, T2 acquires RL followed by UL.
+ */
public void testUpgradeWithMultipleReaders() throws Exception
{
String caseNum = "9";
- Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS*2,
+ Thread t1 = readThread(caseNum, "t1", 0, SLEEP_MSECS * 2,
"1st read lock attempt failed", NO_MORE_OP);
- Thread t2=readThread(caseNum, "t2", 0, SLEEP_MSECS,
+ Thread t2 = readThread(caseNum, "t2", 0, SLEEP_MSECS,
"2nd read lock attempt failed", INVOKE_UPGRADE);
t1.start();
t2.start();
t1.join(3000);
t2.join(3000);
- assertTrue(checkLockingResult(caseNum+"-t1-RL-1") &&
- checkLockingResult(caseNum+"-t2-RL-1") &&
- checkLockingResult(caseNum+"-t2-UL-1"));
+ assertTrue(checkLockingResult(caseNum + "-t1-RL-1") &&
+ checkLockingResult(caseNum + "-t2-RL-1") &&
+ checkLockingResult(caseNum + "-t2-UL-1"));
cleanLockingResult();
// possilbe deadlock check
if (t1.isAlive() || t2.isAlive())
1.10 +214 -169 JBossCache/tests/functional/org/jboss/cache/lock/LockTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: LockTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/lock/LockTest.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- LockTest.java 8 Dec 2006 18:50:49 -0000 1.9
+++ LockTest.java 23 May 2007 10:28:52 -0000 1.10
@@ -1,52 +1,59 @@
package org.jboss.cache.lock;
-import java.util.concurrent.CyclicBarrier;
-import java.util.concurrent.Semaphore;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.Lock;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.cache.misc.TestingUtil;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+
/**
* Various tests that test isolation level semantics provided by locks
*
* @author Bela Ban
- * @version $Id: LockTest.java,v 1.9 2006/12/08 18:50:49 genman Exp $
+ * @version $Id: LockTest.java,v 1.10 2007/05/23 10:28:52 msurtani Exp $
*/
-public class LockTest extends TestCase {
- int value=10;
+public class LockTest extends TestCase
+{
+ int value = 10;
Throwable t1_ex, t2_ex;
- long start=0;
- final long TIMEOUT=5000;
- final long SLEEP=500;
+ long start = 0;
+ final long TIMEOUT = 5000;
+ final long SLEEP = 500;
volatile boolean committed;
- public LockTest(String name) {
+ public LockTest(String name)
+ {
super(name);
}
-
- public void tearDown() throws Exception {
+ public void tearDown() throws Exception
+ {
super.tearDown();
- t1_ex=t2_ex=null;
+ t1_ex = t2_ex = null;
committed = false;
}
- static class MyFIFOSemaphore extends Semaphore {
- public MyFIFOSemaphore(int permits) {
+ static class MyFIFOSemaphore extends Semaphore
+ {
+ public MyFIFOSemaphore(int permits)
+ {
super(permits);
}
- public void acquire() throws InterruptedException {
+ public void acquire() throws InterruptedException
+ {
super.acquire();
}
- public void release() {
+ public void release()
+ {
super.release();
}
}
@@ -63,22 +70,26 @@
* T1 reads data - 20
* </ol>
*/
- public void testReadUncommitted() throws Throwable {
- final LockStrategy s=new LockStrategyReadUncommitted();
- final Semaphore sem=new MyFIFOSemaphore(1);
- final CyclicBarrier barrier=new CyclicBarrier(2);
-
- Thread t1=new Thread("t1") {
- Lock lock=null;
-
- public void run() {
- try {
+ public void testReadUncommitted() throws Throwable
+ {
+ final LockStrategy s = new LockStrategyReadUncommitted();
+ final Semaphore sem = new MyFIFOSemaphore(1);
+ final CyclicBarrier barrier = new CyclicBarrier(2);
+
+ Thread t1 = new Thread("t1")
+ {
+ Lock lock = null;
+
+ public void run()
+ {
+ try
+ {
sem.acquire(); // we're first to the semaphore
// log("waiting on barrier");
barrier.await(); // wait until t2 joins us
// log("passed barrier");
- lock=s.readLock();
+ lock = s.readLock();
lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS);
log("1st read: value is " + value);
assertEquals(10, value);
@@ -95,11 +106,13 @@
log("3rd read: value is still " + value + "; we should see t2's committed change");
assertEquals(20, value);
}
- catch(Throwable ex) {
- t1_ex=ex;
- }
- finally {
- if(lock != null)
+ catch (Throwable ex)
+ {
+ t1_ex = ex;
+ }
+ finally
+ {
+ if (lock != null)
lock.unlock();
sem.release();
}
@@ -107,18 +120,21 @@
};
- Thread t2=new Thread("t2") {
- Lock lock=null;
-
- public void run() {
- try {
+ Thread t2 = new Thread("t2")
+ {
+ Lock lock = null;
+
+ public void run()
+ {
+ try
+ {
TestingUtil.sleepThread(100);
barrier.await();
sem.acquire();
- lock=s.writeLock();
+ lock = s.writeLock();
lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS);
log("changing value from " + value + " to 20");
- value=20;
+ value = 20;
sem.release(); // now t1 can read the uncommitted modification
TestingUtil.sleepThread(100);
@@ -126,11 +142,13 @@
log("committing the TX");
lock.unlock();
}
- catch(Throwable ex) {
- t2_ex=ex;
- }
- finally {
- if(lock != null)
+ catch (Throwable ex)
+ {
+ t2_ex = ex;
+ }
+ finally
+ {
+ if (lock != null)
lock.unlock();
sem.release();
}
@@ -141,14 +159,13 @@
t2.start();
t1.join();
t2.join();
- if(t1_ex != null)
+ if (t1_ex != null)
throw t1_ex;
- if(t2_ex != null)
+ if (t2_ex != null)
throw t2_ex;
}
-
/**
* Thread1 reads data, thread2 changes and - before thread2 commits - t1 should *not* see t2's changes.
* Timeline:
@@ -164,7 +181,6 @@
* isolation levels
*/
-
// removed in favor of o.j.c.transaction.IsolationLevelReadCommittedTest.testReadCommitted()
// The lock interceptor makes one request a read lock before *each* read.
@@ -230,19 +246,20 @@
if(t2_ex != null)
throw t2_ex;
}*/
-
-
-
- public void testWriteThanRead() throws Throwable {
- final LockStrategy s=new LockStrategyReadCommitted();
-
- Thread t1=new Thread("t1") {
- Lock lock=null;
-
- public void run() {
- try {
+ public void testWriteThanRead() throws Throwable
+ {
+ final LockStrategy s = new LockStrategyReadCommitted();
+
+ Thread t1 = new Thread("t1")
+ {
+ Lock lock = null;
+
+ public void run()
+ {
+ try
+ {
TestingUtil.sleepThread(100);
- lock=s.readLock();
+ lock = s.readLock();
lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS);
log("1st read: value is " + value);
assertEquals(20, value);
@@ -252,34 +269,41 @@
assertEquals(20, value); // we're seeing the modification by t2 before t2 committed (a.k.a. released the lock)
TestingUtil.sleepThread(SLEEP);
}
- catch(Throwable ex) {
- t1_ex=ex;
+ catch (Throwable ex)
+ {
+ t1_ex = ex;
}
- finally {
+ finally
+ {
lock.unlock();
}
}
};
- Thread t2=new Thread("t2") {
- Lock lock=null;
-
- public void run() {
- try {
- lock=s.writeLock();
+ Thread t2 = new Thread("t2")
+ {
+ Lock lock = null;
+
+ public void run()
+ {
+ try
+ {
+ lock = s.writeLock();
lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS);
log("changing value from " + value + " to 20");
- value=20;
+ value = 20;
TestingUtil.sleepThread(SLEEP);
log("committing the TX");
lock.unlock();
}
- catch(Throwable ex) {
- t2_ex=ex;
+ catch (Throwable ex)
+ {
+ t2_ex = ex;
}
- finally {
+ finally
+ {
lock.unlock();
}
}
@@ -289,15 +313,13 @@
t1.start();
t2.join();
t1.join();
- if(t1_ex != null)
+ if (t1_ex != null)
throw t1_ex;
- if(t2_ex != null)
+ if (t2_ex != null)
throw t2_ex;
}
-
-
/**
* Thread1 reads data, thread2 changes and - before thread2 commits - t1 should *not* see t2's changes.
* In addition, Thread1 should *not* see thread2's changes even after thread2 commits, until thread1 commits.
@@ -315,15 +337,19 @@
* execution: thread1 will acquire the read lock on the data and hold on to it until TX commit, only then will
* thread2 be able to access the data with a write lock.
*/
- public void testRepeatableRead() throws Throwable {
- final LockStrategy s=new LockStrategyRepeatableRead();
-
- Thread t1=new Thread("t1") {
- Lock lock=null;
-
- public void run() {
- try {
- lock=s.readLock();
+ public void testRepeatableRead() throws Throwable
+ {
+ final LockStrategy s = new LockStrategyRepeatableRead();
+
+ Thread t1 = new Thread("t1")
+ {
+ Lock lock = null;
+
+ public void run()
+ {
+ try
+ {
+ lock = s.readLock();
lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS);
log("1st read: value is " + value);
assertEquals(10, value);
@@ -342,35 +368,42 @@
log("4th read: value is now " + value + "; we should see t2's committed change in our new TX");
assertEquals(20, value);
}
- catch(Throwable ex) {
- t1_ex=ex;
+ catch (Throwable ex)
+ {
+ t1_ex = ex;
}
- finally {
+ finally
+ {
lock.unlock();
}
}
};
- Thread t2=new Thread("t2") {
- Lock lock=null;
-
- public void run() {
- try {
+ Thread t2 = new Thread("t2")
+ {
+ Lock lock = null;
+
+ public void run()
+ {
+ try
+ {
TestingUtil.sleepThread(100);
- lock=s.writeLock();
+ lock = s.writeLock();
lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS);
log("changing value from " + value + " to 20");
- value=20;
+ value = 20;
TestingUtil.sleepThread(SLEEP);
log("committing the TX");
lock.unlock();
}
- catch(Throwable ex) {
- t2_ex=ex;
+ catch (Throwable ex)
+ {
+ t2_ex = ex;
}
- finally {
+ finally
+ {
lock.unlock();
}
}
@@ -380,9 +413,9 @@
t2.start();
t1.join();
t2.join();
- if(t1_ex != null)
+ if (t1_ex != null)
throw t1_ex;
- if(t2_ex != null)
+ if (t2_ex != null)
throw t2_ex;
}
@@ -400,15 +433,19 @@
* T2 commits (releases its lock)
* </ol>
*/
- public void testSerializable() throws Throwable {
- final LockStrategy s=new LockStrategySerializable();
-
- Thread t1=new Thread("t1") {
- Lock lock=null;
-
- public void run() {
- try {
- lock=s.readLock();
+ public void testSerializable() throws Throwable
+ {
+ final LockStrategy s = new LockStrategySerializable();
+
+ Thread t1 = new Thread("t1")
+ {
+ Lock lock = null;
+
+ public void run()
+ {
+ try
+ {
+ lock = s.readLock();
lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS);
log("1st read: value is " + value);
assertEquals(10, value);
@@ -419,33 +456,40 @@
log("2nd read: value is " + value + "; we should see t2's committed change (20)");
assertEquals(20, value);
}
- catch(Throwable ex) {
- t1_ex=ex;
+ catch (Throwable ex)
+ {
+ t1_ex = ex;
}
- finally {
+ finally
+ {
lock.unlock();
}
}
};
- Thread t2=new Thread("t2") {
- Lock lock=null;
-
- public void run() {
- try {
+ Thread t2 = new Thread("t2")
+ {
+ Lock lock = null;
+
+ public void run()
+ {
+ try
+ {
TestingUtil.sleepThread(100);
- lock=s.writeLock();
+ lock = s.writeLock();
lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS);
log("changing value from " + value + " to 20");
- value=20;
+ value = 20;
log("committing the TX");
lock.unlock();
}
- catch(Throwable ex) {
- t2_ex=ex;
+ catch (Throwable ex)
+ {
+ t2_ex = ex;
}
- finally {
+ finally
+ {
lock.unlock();
}
}
@@ -455,35 +499,36 @@
t2.start();
t1.join();
t2.join();
- if(t1_ex != null)
+ if (t1_ex != null)
throw t1_ex;
- if(t2_ex != null)
+ if (t2_ex != null)
throw t2_ex;
}
- void log(String s) {
+ void log(String s)
+ {
long now;
- if(start == 0)
- start=System.currentTimeMillis();
- now=System.currentTimeMillis();
+ if (start == 0)
+ start = System.currentTimeMillis();
+ now = System.currentTimeMillis();
System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s);
}
- public static void main(String[] args) throws Exception {
+ public static void main(String[] args) throws Exception
+ {
junit.textui.TestRunner.run(suite());
}
// Needed for JUnit.
- public static Test suite() {
- TestSuite suite=new TestSuite();
+ public static Test suite()
+ {
+ TestSuite suite = new TestSuite();
suite.addTestSuite(LockTest.class);
return suite;
}
-
-
}
1.5 +147 -81 JBossCache/tests/functional/org/jboss/cache/lock/ReentrantWriterPreferenceReadWriteLockTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ReentrantWriterPreferenceReadWriteLockTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/lock/ReentrantWriterPreferenceReadWriteLockTest.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- ReentrantWriterPreferenceReadWriteLockTest.java 8 Dec 2006 18:50:49 -0000 1.4
+++ ReentrantWriterPreferenceReadWriteLockTest.java 23 May 2007 10:28:52 -0000 1.5
@@ -1,118 +1,149 @@
package org.jboss.cache.lock;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.Lock;
-
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.cache.misc.TestingUtil;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+
/**
* Tests ReentrantWriterPreferenceReadWriteLock
+ *
* @author Bela Ban
- * @version $Id: ReentrantWriterPreferenceReadWriteLockTest.java,v 1.4 2006/12/08 18:50:49 genman Exp $
+ * @version $Id: ReentrantWriterPreferenceReadWriteLockTest.java,v 1.5 2007/05/23 10:28:52 msurtani Exp $
*/
-public class ReentrantWriterPreferenceReadWriteLockTest extends TestCase {
+public class ReentrantWriterPreferenceReadWriteLockTest extends TestCase
+{
// ReentrantWriterPreferenceReadWriteLock lock;
SimpleReadWriteLock lock;
Lock rl, wl;
- Exception thread_ex=null;
+ Exception thread_ex = null;
- protected void setUp() throws Exception {
+ protected void setUp() throws Exception
+ {
super.setUp();
// lock=new ReentrantWriterPreferenceReadWriteLock();
- lock=new SimpleReadWriteLock();
- rl=lock.readLock();
- wl=lock.writeLock();
- thread_ex=null;
+ lock = new SimpleReadWriteLock();
+ rl = lock.readLock();
+ wl = lock.writeLock();
+ thread_ex = null;
}
- protected void tearDown() throws Exception {
+ protected void tearDown() throws Exception
+ {
super.tearDown();
- lock=null;
- if(thread_ex != null)
+ lock = null;
+ if (thread_ex != null)
throw thread_ex;
}
- public void testMultipleReadLockAcquisitions() throws InterruptedException {
+ public void testMultipleReadLockAcquisitions() throws InterruptedException
+ {
rl.lock();
rl.lock();
}
- public void testInterruptedLockAcquisition() {
+ public void testInterruptedLockAcquisition()
+ {
Thread.currentThread().interrupt();
- try {
+ try
+ {
rl.lockInterruptibly();
fail("thread should be in interrupted status");
}
- catch(InterruptedException e) {
+ catch (InterruptedException e)
+ {
}
- finally {
- try {
+ finally
+ {
+ try
+ {
rl.unlock();
fail("unlock() should throw an IllegalStateException");
}
- catch(IllegalMonitorStateException illegalStateEx) {
+ catch (IllegalMonitorStateException illegalStateEx)
+ {
assertTrue(true);
}
}
}
- public void testMultipleWriteLockAcquisitions() throws InterruptedException {
+ public void testMultipleWriteLockAcquisitions() throws InterruptedException
+ {
wl.lock();
wl.lock();
}
- public void testMultipleReadLockReleases() throws InterruptedException {
+ public void testMultipleReadLockReleases() throws InterruptedException
+ {
rl.lock();
rl.unlock();
- try {
+ try
+ {
rl.unlock();
fail("we should not get here, cannot lock RL once but unlock twice");
}
- catch(IllegalMonitorStateException illegalState) {
+ catch (IllegalMonitorStateException illegalState)
+ {
// this is as expected
}
}
- public void acquireReadAndWriteLocks() throws InterruptedException {
+ public void acquireReadAndWriteLocks() throws InterruptedException
+ {
rl.lock();
rl.lock();
- boolean fl=wl.tryLock(4000, TimeUnit.MILLISECONDS);
+ boolean fl = wl.tryLock(4000, TimeUnit.MILLISECONDS);
assertTrue(fl);
}
- public void acquireWriteThenReadLock() throws InterruptedException {
+ public void acquireWriteThenReadLock() throws InterruptedException
+ {
wl.lock();
rl.lock();
wl.unlock();
rl.unlock();
}
- public void testMultipleWriteLockReleases() throws InterruptedException {
+ public void testMultipleWriteLockReleases() throws InterruptedException
+ {
wl.lock();
wl.unlock();
- try {
+ try
+ {
wl.unlock();
fail("expected");
- } catch (IllegalMonitorStateException e) {}
+ }
+ catch (IllegalMonitorStateException e)
+ {
+ }
}
- public void testAcquireWriteLockAfterReadLock() throws InterruptedException {
+ public void testAcquireWriteLockAfterReadLock() throws InterruptedException
+ {
rl.lock();
rl.unlock();
wl.lock();
}
- public void testAcquiringReadLockedLockWithRead() throws InterruptedException {
- new Thread() {
- public void run() {
- try {rl.lockInterruptibly();}
- catch(InterruptedException e) {}
+ public void testAcquiringReadLockedLockWithRead() throws InterruptedException
+ {
+ new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ rl.lockInterruptibly();
+ }
+ catch (InterruptedException e)
+ {
+ }
}
}.start();
@@ -120,63 +151,77 @@
// now we have a RL by another thread
- boolean flag=rl.tryLock(3000, TimeUnit.MILLISECONDS);
+ boolean flag = rl.tryLock(3000, TimeUnit.MILLISECONDS);
assertTrue(flag);
- flag=wl.tryLock(3000, TimeUnit.MILLISECONDS);
+ flag = wl.tryLock(3000, TimeUnit.MILLISECONDS);
assertFalse(flag);
}
- public void testAcquiringReadLockedLock() throws InterruptedException {
- new Thread() {
- public void run() {
- try {rl.lockInterruptibly();}
- catch(InterruptedException e) {}
+ public void testAcquiringReadLockedLock() throws InterruptedException
+ {
+ new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ rl.lockInterruptibly();
+ }
+ catch (InterruptedException e)
+ {
+ }
}
}.start();
TestingUtil.sleepThread(500);
// now we have a RL by another thread
- boolean flag=wl.tryLock(3000, TimeUnit.MILLISECONDS);
+ boolean flag = wl.tryLock(3000, TimeUnit.MILLISECONDS);
assertFalse(flag);
}
- public void testWriteThenReadByDifferentTx() throws InterruptedException {
- Writer writer=new Writer("Writer");
- Reader reader=new Reader("Reader");
+ public void testWriteThenReadByDifferentTx() throws InterruptedException
+ {
+ Writer writer = new Writer("Writer");
+ Reader reader = new Reader("Reader");
writer.start();
TestingUtil.sleepThread(500);
reader.start();
TestingUtil.sleepThread(1000);
- synchronized(writer) {
+ synchronized (writer)
+ {
log("terminating Writer");
writer.notify();
}
TestingUtil.sleepThread(500);
- synchronized(reader) {
+ synchronized (reader)
+ {
reader.notify();
}
writer.join();
reader.join();
}
- public void testReadThenWriteByDifferentTx() throws InterruptedException {
- Writer writer=new Writer("Writer");
- Reader reader=new Reader("Reader");
+ public void testReadThenWriteByDifferentTx() throws InterruptedException
+ {
+ Writer writer = new Writer("Writer");
+ Reader reader = new Reader("Reader");
reader.start();
TestingUtil.sleepThread(500);
writer.start();
TestingUtil.sleepThread(1000);
- synchronized(reader) {
+ synchronized (reader)
+ {
log("terminating Reader");
reader.notify();
}
TestingUtil.sleepThread(500);
- synchronized(writer) {
+ synchronized (writer)
+ {
writer.notify();
}
writer.join();
@@ -184,103 +229,124 @@
}
-
- private static void log(String msg) {
+ private static void log(String msg)
+ {
System.out.println(System.currentTimeMillis() + " " + Thread.currentThread() +
" [" + Thread.currentThread().getName() + "]: " + msg);
}
- class Reader extends Thread {
+ class Reader extends Thread
+ {
- public Reader(String name) {
+ public Reader(String name)
+ {
super(name);
}
- public void run() {
- try {
+ public void run()
+ {
+ try
+ {
log("acquiring RL");
rl.lock();
log("acquired RL");
- synchronized(this) {
+ synchronized (this)
+ {
this.wait();
}
log("releasing RL");
rl.unlock();
log("released RL");
}
- catch(InterruptedException e) {
+ catch (InterruptedException e)
+ {
;
}
}
}
- class Writer extends Thread {
+ class Writer extends Thread
+ {
- public Writer(String name) {
+ public Writer(String name)
+ {
super(name);
}
- public void run() {
- try {
+ public void run()
+ {
+ try
+ {
log("acquiring WL");
wl.lock();
log("acquired WL");
- synchronized(this) {
+ synchronized (this)
+ {
this.wait();
}
log("releasing WL");
wl.unlock();
log("released WL");
}
- catch(InterruptedException e) {
+ catch (InterruptedException e)
+ {
;
}
}
}
- class Upgrader extends Thread {
- boolean upgradeSuccessful=false;
- public Upgrader(String name) {
+ class Upgrader extends Thread
+ {
+ boolean upgradeSuccessful = false;
+
+ public Upgrader(String name)
+ {
super(name);
}
- public boolean wasUpgradeSuccessful() {
+ public boolean wasUpgradeSuccessful()
+ {
return upgradeSuccessful;
}
- public void run() {
- try {
+ public void run()
+ {
+ try
+ {
log("acquiring RL");
rl.lock();
log("acquired RL");
- synchronized(this) {
+ synchronized (this)
+ {
this.wait();
}
log("attempting to lock WL");
// rl.unlock();
wl.lock();
- upgradeSuccessful=true;
+ upgradeSuccessful = true;
log("acquired WL");
log("releasing WL/RL");
wl.unlock();
log("released WL/RL");
}
- catch(InterruptedException e) {
+ catch (InterruptedException e)
+ {
;
}
}
}
-
- public static Test suite() {
+ public static Test suite()
+ {
return new TestSuite(ReentrantWriterPreferenceReadWriteLockTest.class);
}
- public static void main(String[] args) {
+ public static void main(String[] args)
+ {
junit.textui.TestRunner.run(suite());
}
More information about the jboss-cvs-commits
mailing list