📄 abstractentityregionaccessstrategytestcase.java
字号:
/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Middleware LLC. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */package org.hibernate.test.cache.jbc2.entity;import java.util.Iterator;import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;import junit.extensions.TestSetup;import junit.framework.AssertionFailedError;import junit.framework.Test;import junit.framework.TestSuite;import org.hibernate.cache.CacheDataDescription;import org.hibernate.cache.EntityRegion;import org.hibernate.cache.access.AccessType;import org.hibernate.cache.access.EntityRegionAccessStrategy;import org.hibernate.cache.impl.CacheDataDescriptionImpl;import org.hibernate.cache.jbc2.BasicRegionAdapter;import org.hibernate.cache.jbc2.JBossCacheRegionFactory;import org.hibernate.cache.jbc2.MultiplexedJBossCacheRegionFactory;import org.hibernate.cache.jbc2.builder.MultiplexingCacheInstanceManager;import org.hibernate.cache.jbc2.entity.EntityRegionImpl;import org.hibernate.cache.jbc2.entity.TransactionalAccess;import org.hibernate.cache.jbc2.util.CacheHelper;import org.hibernate.cache.jbc2.util.NonLockingDataVersion;import org.hibernate.cfg.Configuration;import org.hibernate.test.cache.jbc2.AbstractJBossCacheTestCase;import org.hibernate.test.util.CacheTestUtil;import org.hibernate.util.ComparableComparator;import org.jboss.cache.Cache;import org.jboss.cache.Fqn;import org.jboss.cache.Node;import org.jboss.cache.NodeSPI;import org.jboss.cache.transaction.BatchModeTransactionManager;/** * Base class for tests of EntityRegionAccessStrategy impls. * * @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a> * @version $Revision: 1 $ */public abstract class AbstractEntityRegionAccessStrategyTestCase extends AbstractJBossCacheTestCase { public static final String REGION_NAME = "test/com.foo.test"; public static final String KEY_BASE = "KEY"; public static final String VALUE1 = "VALUE1"; public static final String VALUE2 = "VALUE2"; protected static int testCount; protected static Configuration localCfg; protected static JBossCacheRegionFactory localRegionFactory; protected static Cache localCache; protected static Configuration remoteCfg; protected static JBossCacheRegionFactory remoteRegionFactory; protected static Cache remoteCache; protected static boolean invalidation; protected static boolean optimistic; protected static boolean synchronous; protected EntityRegion localEntityRegion; protected EntityRegionAccessStrategy localAccessStrategy; protected EntityRegion remoteEntityRegion; protected EntityRegionAccessStrategy remoteAccessStrategy; protected Exception node1Exception; protected Exception node2Exception; protected AssertionFailedError node1Failure; protected AssertionFailedError node2Failure; public static Test getTestSetup(Class testClass, String configName) { TestSuite suite = new TestSuite(testClass); return new AccessStrategyTestSetup(suite, configName); } public static Test getTestSetup(Test test, String configName) { return new AccessStrategyTestSetup(test, configName); } /** * Create a new TransactionalAccessTestCase. * * @param name */ public AbstractEntityRegionAccessStrategyTestCase(String name) { super(name); } protected abstract AccessType getAccessType(); protected void setUp() throws Exception { super.setUp(); // Sleep a bit to avoid concurrent FLUSH problem avoidConcurrentFlush(); localEntityRegion = localRegionFactory.buildEntityRegion(REGION_NAME, localCfg.getProperties(), getCacheDataDescription()); localAccessStrategy = localEntityRegion.buildAccessStrategy(getAccessType()); // Sleep a bit to avoid concurrent FLUSH problem avoidConcurrentFlush(); remoteEntityRegion = remoteRegionFactory.buildEntityRegion(REGION_NAME, remoteCfg.getProperties(), getCacheDataDescription()); remoteAccessStrategy = remoteEntityRegion.buildAccessStrategy(getAccessType()); node1Exception = null; node2Exception = null; node1Failure = null; node2Failure = null; } protected void tearDown() throws Exception { super.tearDown(); if (localEntityRegion != null) localEntityRegion.destroy(); if (remoteEntityRegion != null) remoteEntityRegion.destroy(); try { localCache.getInvocationContext().getOptionOverrides().setCacheModeLocal(true); localCache.removeNode(Fqn.ROOT); } catch (Exception e) { log.error("Problem purging local cache" ,e); } try { remoteCache.getInvocationContext().getOptionOverrides().setCacheModeLocal(true); remoteCache.removeNode(Fqn.ROOT); } catch (Exception e) { log.error("Problem purging remote cache" ,e); } node1Exception = null; node2Exception = null; node1Failure = null; node2Failure = null; } protected static Configuration createConfiguration(String configName) { Configuration cfg = CacheTestUtil.buildConfiguration(REGION_PREFIX, MultiplexedJBossCacheRegionFactory.class, true, false); cfg.setProperty(MultiplexingCacheInstanceManager.ENTITY_CACHE_RESOURCE_PROP, configName); return cfg; } protected CacheDataDescription getCacheDataDescription() { return new CacheDataDescriptionImpl(true, true, ComparableComparator.INSTANCE); } protected boolean isUsingOptimisticLocking() { return optimistic; } protected boolean isUsingInvalidation() { return invalidation; } protected boolean isSynchronous() { return synchronous; } protected Fqn getRegionFqn(String regionName, String regionPrefix) { return BasicRegionAdapter.getTypeLastRegionFqn(regionName, regionPrefix, EntityRegionImpl.TYPE); } protected void assertThreadsRanCleanly() { if (node1Failure != null) throw node1Failure; if (node2Failure != null) throw node2Failure; if (node1Exception != null) { log.error("node1 saw an exception", node1Exception); assertEquals("node1 saw no exceptions", null, node1Exception); } if (node2Exception != null) { log.error("node2 saw an exception", node2Exception); assertEquals("node2 saw no exceptions", null, node2Exception); } } /** * This is just a setup test where we assert that the cache config is * as we expected. */ public abstract void testCacheConfiguration(); /** * Test method for {@link TransactionalAccess#getRegion()}. */ public void testGetRegion() { assertEquals("Correct region", localEntityRegion, localAccessStrategy.getRegion()); } /** * Test method for {@link TransactionalAccess#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object)}. */ public void testPutFromLoad() throws Exception { putFromLoadTest(false); } /** * Test method for {@link TransactionalAccess#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object, boolean)}. */ public void testPutFromLoadMinimal() throws Exception { putFromLoadTest(true); } /** * Simulate 2 nodes, both start, tx do a get, experience a cache miss, * then 'read from db.' First does a putFromLoad, then an update. * Second tries to do a putFromLoad with stale data (i.e. it took * longer to read from the db). Both commit their tx. Then * both start a new tx and get. First should see the updated data; * second should either see the updated data (isInvalidation() == false) * or null (isInvalidation() == true). * * @param useMinimalAPI * @throws Exception */ private void putFromLoadTest(final boolean useMinimalAPI) throws Exception { final String KEY = KEY_BASE + testCount++; final CountDownLatch writeLatch1 = new CountDownLatch(1); final CountDownLatch writeLatch2 = new CountDownLatch(1); final CountDownLatch completionLatch = new CountDownLatch(2); Thread node1 = new Thread() { public void run() { try { long txTimestamp = System.currentTimeMillis(); BatchModeTransactionManager.getInstance().begin();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -