Author: manik.surtani(a)jboss.com
Date: 2007-11-15 10:07:51 -0500 (Thu, 15 Nov 2007)
New Revision: 4758
Modified:
core/trunk/src/test/java/org/jboss/cache/lock/ReadWriteLockWithUpgradeTest.java
Log:
Patched occasional failures in test, due to flawed test logic.
Modified: core/trunk/src/test/java/org/jboss/cache/lock/ReadWriteLockWithUpgradeTest.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/lock/ReadWriteLockWithUpgradeTest.java 2007-11-15
13:46:17 UTC (rev 4757)
+++
core/trunk/src/test/java/org/jboss/cache/lock/ReadWriteLockWithUpgradeTest.java 2007-11-15
15:07:51 UTC (rev 4758)
@@ -1,15 +1,15 @@
package org.jboss.cache.lock;
+import org.jboss.cache.misc.TestingUtil;
import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.fail;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.Test;
import java.util.Vector;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
-import org.jboss.cache.misc.TestingUtil;
-import org.testng.annotations.Test;
-
/**
* NonBlockingWriterLock is a read/write lock (with upgrade) that has
* non-blocking write lock acquisition on existing read lock(s).
@@ -36,21 +36,19 @@
int INVOKE_WRITE = 2;
int INVOKE_UPGRADE = 3;
+ @AfterMethod
+ public void tearDown()
+ {
+ cleanLockingResult();
+ }
+
+
private static void log(String str)
{
System.out.println(Thread.currentThread() + ": "
+ java.util.Calendar.getInstance().getTime() + " : " + str);
}
- // For debugging purpose
- private static void logX(String str)
- {
- log(str);
- // try {
- // logToFile(str);
- // } catch (IOException ioe) {
- // }
- }
/***************************************************************/
/* Utility functions to creat threads for RL, WL and UL */
@@ -115,13 +113,13 @@
{
if (!rlock.tryLock(msecs, TimeUnit.MILLISECONDS))
{
- logX(caseNum + "-" + name + " requesting read lock
failed!\n");
+ log(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");
+ log(caseNum + "-" + name + " requesting read lock
succeeded!\n");
String str = caseNum + "-" + name + "-RL-1";
postLockingResult(str);
TestingUtil.sleepThread(sleepSecs);
@@ -140,7 +138,7 @@
}
rlock.unlock();
- logX(caseNum + "-" + name + " releasing read
lock.\n");
+ log(caseNum + "-" + name + " releasing read
lock.\n");
}
catch (Exception ex)
{
@@ -169,13 +167,13 @@
Lock wlock = lock_.writeLock();
if (!wlock.tryLock(msecs, TimeUnit.MILLISECONDS))
{
- logX(caseNum + "-" + name + " requesting write lock
failed!\n");
+ log(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");
+ log(caseNum + "-" + name + " requesting write lock
succeeded!\n");
String str = caseNum + "-" + name + "-WL-1";
postLockingResult(str);
TestingUtil.sleepThread(sleepSecs);
@@ -194,10 +192,11 @@
}
wlock.unlock();
- logX(caseNum + "-" + name + " releasing write
lock.\n");
+ log(caseNum + "-" + name + " releasing write
lock.\n");
}
catch (Exception ex)
{
+ ex.printStackTrace();
}
}
};
@@ -224,23 +223,23 @@
Lock wlock = null;
if (!rlock.tryLock(msecs, TimeUnit.MILLISECONDS))
{
- logX(caseNum + "-" + name + " requesting read lock
failed!\n");
+ log(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");
+ log(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");
+ log(caseNum + "-" + name + " requesting upgrade lock
failed!\n");
str += "0";
}
else
{
- logX(caseNum + "-" + name + " requesting upgrade lock
succeeded!\n");
+ log(caseNum + "-" + name + " requesting upgrade lock
succeeded!\n");
str += "1";
}
postLockingResult(str);
@@ -249,7 +248,7 @@
if (wlock != null)
{
wlock.unlock();
- logX(caseNum + "-" + name + " releasing upgrade
lock.\n");
+ log(caseNum + "-" + name + " releasing upgrade
lock.\n");
}
rlock.unlock();
}
@@ -278,18 +277,18 @@
Lock rlock = lock_.readLock();
if (!rlock.tryLock(msecs, TimeUnit.MILLISECONDS))
{
- logX(caseNum + "-" + name + " requesting read lock
failed!\n");
+ log(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");
+ log(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");
+ log(caseNum + "-" + name + " releasing read lock.\n");
}
catch (Exception ex)
{
@@ -308,18 +307,18 @@
Lock wlock = lock_.writeLock();
if (!wlock.tryLock(msecs, TimeUnit.MILLISECONDS))
{
- logX(caseNum + "-" + name + " requesting write lock
failed!\n");
+ log(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");
+ log(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");
+ log(caseNum + "-" + name + " releasing write lock.\n");
}
catch (Exception ex)
{
@@ -338,18 +337,18 @@
Lock ulock = null;
if ((ulock = lock_.upgradeLockAttempt(msecs)) == null)
{
- logX(caseNum + "-" + name + " requesting upgrade lock
failed!\n");
+ log(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");
+ log(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");
+ log(caseNum + "-" + name + " releasing upgrade lock.\n");
}
catch (Exception ex)
{
@@ -372,7 +371,7 @@
*/
protected synchronized void postLockingResult(Object obj)
{
- logX(" Added *" + obj + "* to the result vector\n");
+ log(" Added *" + obj + "* to the result vector\n");
// Make sure we only have one in the vector
//if (!checkLockingResult((String)obj))
lockResult.addElement(obj);
@@ -396,11 +395,11 @@
}
if (rc)
{
- logX(" Searching for *" + expected + "* SUCCEEDED.\n");
+ log(" Searching for *" + expected + "* SUCCEEDED.\n");
}
else
{
- logX(" Searching for *" + expected + "* FAILED.\n");
+ log(" Searching for *" + expected + "* FAILED.\n");
}
return rc;
}
@@ -545,8 +544,14 @@
t2.start();
t1.join(3000);
t2.join(3000);
- assertTrue(checkLockingResult(caseNum + "-t1-RL-1") &&
- checkLockingResult(caseNum + "-t2-WL-0"));
+
+ // there is NO guarantee as to which thread will get the lock!!
+ boolean t0GetsLock = checkLockingResult(caseNum + "-t1-RL-1") &&
checkLockingResult(caseNum + "-t2-WL-0");
+ boolean t1GetsLock = checkLockingResult(caseNum + "-t1-RL-0") &&
checkLockingResult(caseNum + "-t2-WL-1");
+
+ assert !(t0GetsLock && t1GetsLock); // both can't be true
+ assert t0GetsLock || t1GetsLock; // one must be true
+
cleanLockingResult();
// possilbe deadlock check
if (t1.isAlive() || t2.isAlive())
@@ -570,8 +575,14 @@
t2.start();
t1.join(3000);
t2.join(3000);
- assertTrue(checkLockingResult(caseNum + "-t1-WL-1") &&
- checkLockingResult(caseNum + "-t2-RL-0"));
+
+ // there is NO guarantee as to which thread will get the lock!!
+ boolean t0GetsLock = checkLockingResult(caseNum + "-t1-WL-1") &&
checkLockingResult(caseNum + "-t2-RL-0");
+ boolean t1GetsLock = checkLockingResult(caseNum + "-t1-WL-0") &&
checkLockingResult(caseNum + "-t2-RL-1");
+
+ assert !(t0GetsLock && t1GetsLock); // both can't be true
+ assert t0GetsLock || t1GetsLock; // one must be true
+
cleanLockingResult();
// possilbe deadlock check
if (t1.isAlive() || t2.isAlive())
@@ -595,8 +606,14 @@
t2.start();
t1.join(3000);
t2.join(3000);
- assertTrue(checkLockingResult(caseNum + "-t1-WL-1") &&
- checkLockingResult(caseNum + "-t2-WL-0"));
+
+ // there is NO guarantee as to which thread will get the lock!!
+ boolean t0GetsLock = checkLockingResult(caseNum + "-t1-WL-1") &&
checkLockingResult(caseNum + "-t2-WL-0");
+ boolean t1GetsLock = checkLockingResult(caseNum + "-t1-WL-0") &&
checkLockingResult(caseNum + "-t2-WL-1");
+
+ assert !(t0GetsLock && t1GetsLock); // both can't be true
+ assert t0GetsLock || t1GetsLock; // one must be true
+
cleanLockingResult();
// possilbe deadlock check
if (t1.isAlive() || t2.isAlive())