HHH-5025 - Test case fix

This commit is contained in:
Lukasz Antoniak 2011-05-28 13:44:23 +02:00
parent 5a1697a77e
commit d62e362d54
3 changed files with 36 additions and 31 deletions

View File

@ -3,31 +3,31 @@ package org.hibernate.envers.test.integration.cache;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.cache.internal.EhCacheProvider; import org.hibernate.cache.internal.EhCacheProvider;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.envers.RevisionType; import org.hibernate.envers.RevisionType;
import org.hibernate.envers.query.criteria.RevisionTypeAuditExpression; import org.hibernate.envers.query.criteria.RevisionTypeAuditExpression;
import org.hibernate.envers.test.AbstractSessionTest; import org.hibernate.envers.test.AbstractSessionTest;
import org.hibernate.envers.test.Priority; import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrTestEntity; import org.hibernate.envers.test.entities.StrTestEntity;
import org.hibernate.stat.Statistics; import org.hibernate.testing.TestForIssue;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.io.File;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL;
/** /**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/ */
public class HibernateSecLvlQueryCache extends AbstractSessionTest { public class HibernateSecLvlQueryCache extends AbstractSessionTest {
private static final String QUERY_CACHE_REGION = "queryCacheRegion";
@Override @Override
protected void initMappings() throws MappingException, URISyntaxException { protected void initMappings() throws MappingException, URISyntaxException {
config.addAnnotatedClass(StrTestEntity.class); config.addAnnotatedClass(StrTestEntity.class);
config.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true"); config.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
config.setProperty(Environment.USE_QUERY_CACHE, "true"); config.setProperty(Environment.USE_QUERY_CACHE, "true");
config.setProperty(Environment.CACHE_PROVIDER, EhCacheProvider.class.getName()); config.setProperty(Environment.CACHE_PROVIDER, EhCacheProvider.class.getName());
URL cacheURL = Thread.currentThread().getContextClassLoader().getResource("ehCache.xml"); config.setProperty(Environment.CACHE_PROVIDER_CONFIG, "ehcache-test.xml");
config.setProperty(Environment.CACHE_PROVIDER_CONFIG, new File(cacheURL.toURI()).getAbsolutePath());
config.setProperty(Environment.GENERATE_STATISTICS, "true");
} }
@Test @Test
@ -38,31 +38,29 @@ public class HibernateSecLvlQueryCache extends AbstractSessionTest {
StrTestEntity ste = new StrTestEntity("data"); StrTestEntity ste = new StrTestEntity("data");
getSession().persist(ste); getSession().persist(ste);
getSession().getTransaction().commit(); getSession().getTransaction().commit();
// Evicting old query cache.
getSession().getSessionFactory().getCache().evictQueryRegion(QUERY_CACHE_REGION);
} }
@Test @Test
public void testSecLvlCacheWithRevisionTypeDiskPersistent() { @TestForIssue(jiraKey="HHH-5025")
// Invoking the same query twice for caching purpose. public void testSecLvlCacheWithRevisionTypeDiskPersistent() throws InterruptedException {
invokeSampleCachingRevTypeQuery();
invokeSampleCachingRevTypeQuery();
assert getQueryCacheStatistics() > 0;
}
private void invokeSampleCachingRevTypeQuery() {
// Cached query that requires serializing RevisionType variable when persisting to disk. // Cached query that requires serializing RevisionType variable when persisting to disk.
getAuditReader().createQuery().forEntitiesAtRevision(StrTestEntity.class, 1) getAuditReader().createQuery().forEntitiesAtRevision(StrTestEntity.class, 1)
.add(new RevisionTypeAuditExpression(RevisionType.ADD, "=")) .add(new RevisionTypeAuditExpression(RevisionType.ADD, "="))
.setCacheable(true).getResultList(); .setCacheable(true).setCacheRegion(QUERY_CACHE_REGION).getResultList();
// Waiting for cached data to persist to disk.
Thread.sleep(1000);
Assert.assertTrue(getQueryCacheSize() > 0);
} }
private double getQueryCacheStatistics() { private int getQueryCacheSize() {
Statistics stats = getSession().getSessionFactory().getStatistics(); // Statistics are not notified about persisting cached data failure. However, cache entry gets evicted.
// See DiskWriteTask.call() method (net.sf.ehcache.store.compound.factories.DiskStorageFactory).
double queryCacheHitCount = stats.getQueryCacheHitCount(); SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) getSession().getSessionFactory();
double queryCacheMissCount = stats.getQueryCacheMissCount(); return sessionFactoryImplementor.getQueryCache(QUERY_CACHE_REGION).getRegion().toMap().size();
double queryCacheHitRatio = queryCacheHitCount / (queryCacheHitCount + queryCacheMissCount);
return queryCacheHitRatio;
} }
} }

View File

@ -1,7 +0,0 @@
<ehcache>
<diskStore path="java.io.tmpdir" />
<!-- Mandatory persist cached objects to disk (checks whether they are serializable). -->
<defaultCache eternal="false" diskPersistent="true"
maxElementsInMemory="1" maxElementsOnDisk="10000" overflowToDisk="true"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
</ehcache>

View File

@ -0,0 +1,14 @@
<ehcache>
<diskStore path="java.io.tmpdir" />
<!-- Fail safe. -->
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120"
overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
<!-- Mandatory persist cached objects to disk (checks whether they are serializable).
This cache region is used by HibernateSecLvlQueryCache test case. -->
<cache name="queryCacheRegion" eternal="false" diskPersistent="true"
maxElementsInMemory="1" maxElementsOnDisk="10000" overflowToDisk="true"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
</ehcache>