OPENJPA-1624 Fix intermittent datacache timeout failures in TestSJVMCache and CacheTest

git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.0.x@932553 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald Woods 2010-04-09 19:16:42 +00:00
parent 729460be78
commit b31c1a53c8
1 changed files with 75 additions and 18 deletions

View File

@ -985,6 +985,9 @@ public abstract class CacheTest extends AbstractTestCase {
try {
startTx(em);
// get starting time for sleep calculations below
Date startTime = new Date();
CacheObjectE e = new CacheObjectE("e");
em.persist(e);
@ -1023,14 +1026,39 @@ public abstract class CacheTest extends AbstractTestCase {
iterate((Collection) q2.execute());
assertInCache(q2, Boolean.TRUE);
Date currentTime = new Date();
long diff = (currentTime.getTime() - startTime.getTime());
long sleep = 0;
getLog().info("CacheTest.timeoutsHelper() testing all are still in the cache, elapsed time="+diff);
DataCache cache = cacheManager(factory).getDataCache(
DataCache.NAME_DEFAULT, false);
if (diff < 500) {
// all should still be in the cache
checkCache(cache, ids, new boolean[]{ true, true, true, true });
} else {
// need to skip the test on slow systems or when using remote DB connections
getLog().warn("CacheTest.timeoutsHelper() skipping checkCache(all, <500) because diff="+diff);
}
// should cause h to be dropped.
Thread.currentThread().sleep(551);
// should cause h to be dropped (timeout=500)
currentTime = new Date();
diff = (currentTime.getTime() - startTime.getTime());
sleep = 750 - diff;
if (sleep > 0) {
getLog().info("CacheTest.timeoutsHelper() testing h to be dropped by waiting sleep="+sleep);
Thread.currentThread().sleep(sleep);
Thread.yield();
} else {
sleep = 0;
}
if ((diff + sleep) < 950) {
// only h should be dropped
checkCache(cache, ids, new boolean[]{ true, true, true, false });
} else {
// need to skip the test on slow systems or when using remote DB connections
getLog().warn("CacheTest.timeoutsHelper() skipping checkCache(h=500) because diff="+(diff+sleep));
}
// if this run has a default timeout (set to 1 sec in the test
// case), e should be timed out by this point.
@ -1039,11 +1067,24 @@ public abstract class CacheTest extends AbstractTestCase {
boolean eStatus = !((((OpenJPAEntityManagerFactorySPI) factory)
.getConfiguration()).getDataCacheTimeout() > 0);
// should cause f to be dropped.
Thread.currentThread().sleep(550);
// should cause f to be dropped (timeout=1000)
currentTime = new Date();
diff = currentTime.getTime() - startTime.getTime();
sleep = 2000 - diff;
if (sleep > 0) {
getLog().info("CacheTest.timeoutsHelper() testing f to be dropped by waiting sleep="+sleep);
Thread.currentThread().sleep(sleep);
Thread.yield();
checkCache(cache, ids,
new boolean[]{ eStatus, false, true, false });
} else {
sleep = 0;
}
if ((diff + sleep) < 4900) {
// e is conditional, h and f should be dropped, but not g yet
checkCache(cache, ids, new boolean[]{ eStatus, false, true, false });
} else {
// need to skip the test on slow systems or when using remote DB connections
getLog().warn("CacheTest.timeoutsHelper() skipping checkCache(f=1000) because diff="+(diff+sleep));
}
// at this point, q2 should be dropped (because its candidate
// class is CacheObjectF), and q1 might be dropped, depending
@ -1051,11 +1092,17 @@ public abstract class CacheTest extends AbstractTestCase {
assertInCache(q1, (eStatus) ? Boolean.TRUE : Boolean.FALSE);
assertInCache(q2, Boolean.FALSE);
// should cause g to be dropped.
Thread.currentThread().sleep(4050);
// should cause g to be dropped (timeout=5000)
currentTime = new Date();
diff = currentTime.getTime() - startTime.getTime();
sleep = 6000 - diff;
if (sleep > 0) {
getLog().info("CacheTest.timeoutsHelper() testing g to be dropped by waiting sleep="+sleep);
Thread.currentThread().sleep(sleep);
Thread.yield();
checkCache(cache, ids,
new boolean[]{ eStatus, false, false, false });
}
// all of them should be dropped now, since diff > 5000
checkCache(cache, ids, new boolean[]{ eStatus, false, false, false });
}
finally {
endEm(em);
@ -1074,6 +1121,9 @@ public abstract class CacheTest extends AbstractTestCase {
try {
startTx(em);
// get starting time for sleep calculations below
Date startTime = new Date();
CacheObjectE e = new CacheObjectE("e");
em.persist(e);
@ -1105,7 +1155,14 @@ public abstract class CacheTest extends AbstractTestCase {
.getDataCacheTimeout() > 0);
// should cause f to be dropped.
Thread.currentThread().sleep(1100);
Date currentTime = new Date();
long diff = currentTime.getTime() - startTime.getTime();
long sleep = 2000 - diff;
if (sleep > 0) {
getLog().trace("CacheTest.queryTimeoutsHelper() testing f to be dropped by waiting sleep="+sleep);
Thread.currentThread().sleep(sleep);
Thread.yield();
}
// at this point, q2 should be dropped (because its candidate
// class is CacheObjectF), and q1 might be dropped, depending