| package com.gemstone.gemfire; |
| |
| import java.util.Properties; |
| |
| import org.junit.After; |
| import org.junit.AfterClass; |
| import org.junit.Before; |
| |
| import com.gemstone.gemfire.cache.AttributesFactory; |
| import com.gemstone.gemfire.cache.AttributesMutator; |
| import com.gemstone.gemfire.cache.Cache; |
| import com.gemstone.gemfire.cache.CacheException; |
| import com.gemstone.gemfire.cache.CacheFactory; |
| import com.gemstone.gemfire.cache.CacheTransactionManager; |
| import com.gemstone.gemfire.cache.CacheWriter; |
| import com.gemstone.gemfire.cache.CacheWriterException; |
| import com.gemstone.gemfire.cache.EntryEvent; |
| import com.gemstone.gemfire.cache.Region; |
| import com.gemstone.gemfire.cache.RegionEvent; |
| import com.gemstone.gemfire.cache.Scope; |
| import com.gemstone.gemfire.cache.TransactionEvent; |
| import com.gemstone.gemfire.cache.TransactionListener; |
| import com.gemstone.gemfire.cache.util.CacheListenerAdapter; |
| import com.gemstone.gemfire.distributed.DistributedSystem; |
| import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem; |
| import com.gemstone.gemfire.internal.cache.GemFireCacheImpl; |
| |
| /** |
| * Extracted from TXWriterJUnitTest to share with TXWriterOOMEJUnitTest. |
| * |
| * @author Kirk Lund |
| */ |
| @SuppressWarnings("deprecation") |
| public class TXWriterTestCase { |
| |
| protected int cbCount; |
| protected int failedCommits = 0; |
| protected int afterCommits = 0; |
| protected int afterRollbacks = 0; |
| |
| protected GemFireCacheImpl cache; |
| protected CacheTransactionManager txMgr; |
| protected Region<String, String> region; |
| |
| protected void createCache() throws CacheException { |
| Properties p = new Properties(); |
| p.setProperty("mcast-port", "0"); // loner |
| this.cache = (GemFireCacheImpl)CacheFactory.create(DistributedSystem.connect(p)); |
| AttributesFactory<?, ?> af = new AttributesFactory<String, String>(); |
| af.setScope(Scope.DISTRIBUTED_NO_ACK); |
| af.setIndexMaintenanceSynchronous(true); |
| this.region = this.cache.createRegion("TXTest", af.create()); |
| this.txMgr = this.cache.getCacheTransactionManager(); |
| } |
| private void closeCache() { |
| if (this.cache != null) { |
| if (this.txMgr != null) { |
| try { |
| this.txMgr.rollback(); |
| } catch (IllegalStateException ignore) { |
| } |
| } |
| this.region = null; |
| this.txMgr = null; |
| Cache c = this.cache; |
| this.cache = null; |
| c.close(); |
| } |
| } |
| |
| @Before |
| public void setUp() { |
| createCache(); |
| } |
| |
| @After |
| public void tearDown() { |
| try { |
| if (this.txMgr != null) { |
| ((CacheTransactionManager)this.txMgr).setWriter(null); |
| ((CacheTransactionManager)this.txMgr).setListener(null); |
| } |
| } finally { |
| closeCache(); |
| } |
| } |
| |
| @AfterClass |
| public static void afterClass() { |
| InternalDistributedSystem ids = InternalDistributedSystem.getAnyInstance(); |
| if (ids != null) { |
| ids.disconnect(); |
| } |
| } |
| |
| protected void installCacheListenerAndWriter() { |
| AttributesMutator<String, String> mutator = this.region.getAttributesMutator(); |
| mutator.setCacheListener(new CacheListenerAdapter<String, String>() { |
| public void close() {cbCount++;} |
| public void afterCreate(EntryEvent<String, String> event) {cbCount++;} |
| public void afterUpdate(EntryEvent<String, String> event) {cbCount++;} |
| public void afterInvalidate(EntryEvent<String, String> event) {cbCount++;} |
| public void afterDestroy(EntryEvent<String, String> event) {cbCount++;} |
| public void afterRegionInvalidate(RegionEvent<String, String> event) {cbCount++;} |
| public void afterRegionDestroy(RegionEvent<String, String> event) {cbCount++;} |
| }); |
| mutator.setCacheWriter(new CacheWriter<String, String>() { |
| public void close() {cbCount++;} |
| public void beforeUpdate(EntryEvent<String, String> event) |
| throws CacheWriterException {cbCount++;} |
| public void beforeCreate(EntryEvent<String, String> event) |
| throws CacheWriterException {cbCount++;} |
| public void beforeDestroy(EntryEvent<String, String> event) |
| throws CacheWriterException {cbCount++;} |
| public void beforeRegionDestroy(RegionEvent<String, String> event) |
| throws CacheWriterException {cbCount++;} |
| public void beforeRegionClear(RegionEvent<String, String> event) |
| throws CacheWriterException {cbCount++;} |
| }); |
| } |
| |
| protected void installTransactionListener() { |
| ((CacheTransactionManager)this.txMgr).setListener(new TransactionListener() { |
| public void afterFailedCommit(TransactionEvent event) { |
| failedCommits++; |
| } |
| public void afterCommit(TransactionEvent event) { |
| afterCommits++; |
| } |
| public void afterRollback(TransactionEvent event) { |
| afterRollbacks++; |
| } |
| public void close() {} |
| }); |
| } |
| } |