From b31c1a53c8ff017d171266d88eac5f6365fe57f7 Mon Sep 17 00:00:00 2001 From: Donald Woods Date: Fri, 9 Apr 2010 19:16:42 +0000 Subject: [PATCH] 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 --- .../persistence/datacache/CacheTest.java | 93 +++++++++++++++---- 1 file changed, 75 insertions(+), 18 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 9748c8033..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 }); - - // should cause h to be dropped. - Thread.currentThread().sleep(551); - Thread.yield(); - checkCache(cache, ids, new boolean[]{ true, true, true, 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 (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); - Thread.yield(); - checkCache(cache, ids, - new boolean[]{ eStatus, false, true, false }); + // 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(); + } 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); - Thread.yield(); - checkCache(cache, ids, - new boolean[]{ eStatus, false, false, false }); + // 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(); + } + // 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