From 26d306c232fb7944a594aba7f1917b40c576ab51 Mon Sep 17 00:00:00 2001 From: Donald Woods Date: Fri, 9 Apr 2010 17:47:32 +0000 Subject: [PATCH] OPENJPA-1624 Fix intermittent datacache timeout failures in TestSJVMCache and CacheTest git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@932530 13f79535-47bb-0310-9956-ffa450edef68 --- .../persistence/datacache/CacheTest.java | 87 +++++++++++++++---- 1 file changed, 72 insertions(+), 15 deletions(-) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CacheTest.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CacheTest.java index ddfc2ed45..2c8646774 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CacheTest.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/CacheTest.java @@ -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); @@ -996,7 +999,7 @@ public abstract class CacheTest extends AbstractTestCase { CacheObjectH h = new CacheObjectH("h"); em.persist(h); - + endTx(em); Object[] ids = new Object[4]; @@ -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); - checkCache(cache, ids, new boolean[]{ true, true, true, true }); - + 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 (timeout=500) - Thread.currentThread().sleep(600); - Thread.yield(); - checkCache(cache, ids, new boolean[]{ true, true, true, false }); + 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. @@ -1040,10 +1068,23 @@ public abstract class CacheTest extends AbstractTestCase { .getConfiguration()).getDataCacheTimeout() > 0); // should cause f to be dropped (timeout=1000) - Thread.currentThread().sleep(500); - Thread.yield(); - checkCache(cache, ids, - new boolean[]{ eStatus, false, true, false }); + 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(); + } 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 @@ -1052,10 +1093,16 @@ public abstract class CacheTest extends AbstractTestCase { assertInCache(q2, Boolean.FALSE); // should cause g to be dropped (timeout=5000) - Thread.currentThread().sleep(4000); - Thread.yield(); - checkCache(cache, ids, - new boolean[]{ eStatus, false, false, false }); + 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(); + } + // 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