HHH-7560 ehcache unit test intermittently failing

(UnknownServiceException)
This commit is contained in:
brmeyer 2012-08-29 17:58:11 -04:00
parent 759620cecb
commit d4eade8199
1 changed files with 27 additions and 49 deletions

View File

@ -1,17 +1,15 @@
package org.hibernate.test.cache; package org.hibernate.test.cache;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.hamcrest.CoreMatchers; import org.hamcrest.CoreMatchers;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.hibernate.HibernateException;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.cache.ehcache.internal.strategy.ItemValueExtractor; import org.hibernate.cache.ehcache.internal.strategy.ItemValueExtractor;
import org.hibernate.cache.spi.access.SoftLock; import org.hibernate.cache.spi.access.SoftLock;
@ -25,49 +23,31 @@ import org.hibernate.test.domain.Item;
import org.hibernate.test.domain.Person; import org.hibernate.test.domain.Person;
import org.hibernate.test.domain.PhoneNumber; import org.hibernate.test.domain.PhoneNumber;
import org.hibernate.test.domain.VersionedItem; import org.hibernate.test.domain.VersionedItem;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.hamcrest.CoreMatchers.equalTo; import org.junit.Test;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
/** /**
* @author Chris Dennis * @author Chris Dennis
* @author Brett Meyer
*/ */
public class HibernateCacheTest { public class HibernateCacheTest extends BaseCoreFunctionalTestCase {
private static SessionFactory sessionFactory;
private static Configuration config;
private static final String REGION_PREFIX = "hibernate.test."; private static final String REGION_PREFIX = "hibernate.test.";
public synchronized static SessionFactory getSessionFactory() { @Override
if ( sessionFactory == null ) { protected void configure(Configuration config) {
try {
sessionFactory = config.buildSessionFactory();
}
catch ( HibernateException ex ) {
System.err.println( "Initial SessionFactory creation failed." + ex );
throw new ExceptionInInitializerError( ex );
}
}
return sessionFactory;
}
@BeforeClass
public static void setUp() {
System.setProperty( "derby.system.home", "target/derby" ); System.setProperty( "derby.system.home", "target/derby" );
config = new Configuration().configure( "/hibernate-config/hibernate.cfg.xml" ); config.configure( "hibernate-config/hibernate.cfg.xml" );
config.setProperty( "hibernate.hbm2ddl.auto", "create" );
getSessionFactory().getStatistics().setStatisticsEnabled( true );
} }
@AfterClass @Override
public static void tearDown() { protected void afterSessionFactoryBuilt() {
getSessionFactory().close(); sessionFactory().getStatistics().setStatisticsEnabled( true );
} }
@Test @Test
public void testQueryCacheInvalidation() throws Exception { public void testQueryCacheInvalidation() throws Exception {
Session s = getSessionFactory().openSession(); Session s = sessionFactory().openSession();
Transaction t = s.beginTransaction(); Transaction t = s.beginTransaction();
Item i = new Item(); Item i = new Item();
i.setName( "widget" ); i.setName( "widget" );
@ -76,7 +56,7 @@ public class HibernateCacheTest {
t.commit(); t.commit();
s.close(); s.close();
SecondLevelCacheStatistics slcs = s.getSessionFactory() SecondLevelCacheStatistics slcs = sessionFactory()
.getStatistics() .getStatistics()
.getSecondLevelCacheStatistics( REGION_PREFIX + Item.class.getName() ); .getSecondLevelCacheStatistics( REGION_PREFIX + Item.class.getName() );
@ -84,7 +64,7 @@ public class HibernateCacheTest {
assertThat( slcs.getElementCountInMemory(), equalTo( 1L ) ); assertThat( slcs.getElementCountInMemory(), equalTo( 1L ) );
assertThat( slcs.getEntries().size(), equalTo( 1 ) ); assertThat( slcs.getEntries().size(), equalTo( 1 ) );
s = getSessionFactory().openSession(); s = sessionFactory().openSession();
t = s.beginTransaction(); t = s.beginTransaction();
i = (Item) s.get( Item.class, i.getId() ); i = (Item) s.get( Item.class, i.getId() );
@ -110,7 +90,7 @@ public class HibernateCacheTest {
assertThat( (String) map.get( "name" ), equalTo( "widget" ) ); assertThat( (String) map.get( "name" ), equalTo( "widget" ) );
// cleanup // cleanup
s = getSessionFactory().openSession(); s = sessionFactory().openSession();
t = s.beginTransaction(); t = s.beginTransaction();
s.delete( i ); s.delete( i );
t.commit(); t.commit();
@ -119,8 +99,8 @@ public class HibernateCacheTest {
@Test @Test
public void testEmptySecondLevelCacheEntry() throws Exception { public void testEmptySecondLevelCacheEntry() throws Exception {
getSessionFactory().evictEntity( Item.class.getName() ); sessionFactory().evictEntity( Item.class.getName() );
Statistics stats = getSessionFactory().getStatistics(); Statistics stats = sessionFactory().getStatistics();
stats.clear(); stats.clear();
SecondLevelCacheStatistics statistics = stats.getSecondLevelCacheStatistics( REGION_PREFIX + Item.class.getName() ); SecondLevelCacheStatistics statistics = stats.getSecondLevelCacheStatistics( REGION_PREFIX + Item.class.getName() );
Map cacheEntries = statistics.getEntries(); Map cacheEntries = statistics.getEntries();
@ -129,7 +109,7 @@ public class HibernateCacheTest {
@Test @Test
public void testStaleWritesLeaveCacheConsistent() { public void testStaleWritesLeaveCacheConsistent() {
Session s = getSessionFactory().openSession(); Session s = sessionFactory().openSession();
Transaction txn = s.beginTransaction(); Transaction txn = s.beginTransaction();
VersionedItem item = new VersionedItem(); VersionedItem item = new VersionedItem();
item.setName( "steve" ); item.setName( "steve" );
@ -144,7 +124,7 @@ public class HibernateCacheTest {
item.setVersion( item.getVersion() - 1 ); item.setVersion( item.getVersion() - 1 );
try { try {
s = getSessionFactory().openSession(); s = sessionFactory().openSession();
txn = s.beginTransaction(); txn = s.beginTransaction();
s.update( item ); s.update( item );
txn.commit(); txn.commit();
@ -172,7 +152,7 @@ public class HibernateCacheTest {
} }
// check the version value in the cache... // check the version value in the cache...
SecondLevelCacheStatistics slcs = getSessionFactory().getStatistics() SecondLevelCacheStatistics slcs = sessionFactory().getStatistics()
.getSecondLevelCacheStatistics( REGION_PREFIX + VersionedItem.class.getName() ); .getSecondLevelCacheStatistics( REGION_PREFIX + VersionedItem.class.getName() );
assertThat( slcs, CoreMatchers.<Object>notNullValue() ); assertThat( slcs, CoreMatchers.<Object>notNullValue() );
final Map entries = slcs.getEntries(); final Map entries = slcs.getEntries();
@ -189,7 +169,7 @@ public class HibernateCacheTest {
// cleanup // cleanup
s = getSessionFactory().openSession(); s = sessionFactory().openSession();
txn = s.beginTransaction(); txn = s.beginTransaction();
item = (VersionedItem) s.load( VersionedItem.class, item.getId() ); item = (VersionedItem) s.load( VersionedItem.class, item.getId() );
s.delete( item ); s.delete( item );
@ -200,8 +180,8 @@ public class HibernateCacheTest {
@Test @Test
public void testGeneralUsage() { public void testGeneralUsage() {
EventManager mgr = new EventManager( getSessionFactory() ); EventManager mgr = new EventManager( sessionFactory() );
Statistics stats = getSessionFactory().getStatistics(); Statistics stats = sessionFactory().getStatistics();
// create 3 persons Steve, Orion, Tim // create 3 persons Steve, Orion, Tim
Person stevePerson = new Person(); Person stevePerson = new Person();
@ -247,8 +227,6 @@ public class HibernateCacheTest {
mgr.listEmailsOfEvent( event.getId() ); mgr.listEmailsOfEvent( event.getId() );
} }
getSessionFactory().close();
QueryStatistics queryStats = stats.getQueryStatistics( "from Event" ); QueryStatistics queryStats = stats.getQueryStatistics( "from Event" );
assertThat( "Cache Miss Count", queryStats.getCacheMissCount(), equalTo( 1L ) ); assertThat( "Cache Miss Count", queryStats.getCacheMissCount(), equalTo( 1L ) );
assertThat( "Cache Hit Count", queryStats.getCacheHitCount(), equalTo( 0L ) ); assertThat( "Cache Hit Count", queryStats.getCacheHitCount(), equalTo( 0L ) );