Search in sources :

Example 71 with EntryEvent

use of org.apache.geode.cache.EntryEvent in project geode by apache.

the class CallbackArgDUnitTest method doTest.

private void doTest() throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(DataPolicy.REPLICATE);
    af.setScope(Scope.DISTRIBUTED_ACK);
    CacheListener cl1 = new CacheListenerAdapter() {

        public void afterCreate(EntryEvent e) {
            assertEquals(getCurrentExpectedKey(), e.getKey());
            assertEquals(callbackArg, e.getCallbackArgument());
            assertEquals(true, e.isCallbackArgumentAvailable());
        }
    };
    af.addCacheListener(cl1);
    Region r1 = createRootRegion("r1", af.create());
    Region r2 = r1.createSubregion("r2", af.create());
    r2.createSubregion("r3", af.create());
    TransactionListener tl1 = new TransactionListenerAdapter() {

        public void afterCommit(TransactionEvent e) {
            assertEquals(6, e.getEvents().size());
            ArrayList keys = new ArrayList();
            Iterator it = e.getEvents().iterator();
            while (it.hasNext()) {
                EntryEvent ee = (EntryEvent) it.next();
                keys.add(ee.getKey());
                assertEquals(callbackArg, ee.getCallbackArgument());
                assertEquals(true, ee.isCallbackArgumentAvailable());
            }
            assertEquals(CallbackArgDUnitTest.this.expectedKeys, keys);
            CallbackArgDUnitTest.this.invokeCount = 1;
        }
    };
    CacheTransactionManager ctm = getCache().getCacheTransactionManager();
    ctm.addListener(tl1);
    this.invokeCount = 0;
    this.clCount = 0;
    this.expectedKeys = Arrays.asList(new String[] { "b", "c", "a", "a2", "c2", "b2" });
    doCommitOtherVm();
    assertEquals(1, this.invokeCount);
    assertEquals(6, this.clCount);
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter) TransactionEvent(org.apache.geode.cache.TransactionEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) CacheListener(org.apache.geode.cache.CacheListener) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager)

Example 72 with EntryEvent

use of org.apache.geode.cache.EntryEvent in project geode by apache.

the class DistributionManagerDUnitTest method getSleepingListener.

static CacheListener getSleepingListener(final boolean playDead) {
    regionDestroyedInvoked = false;
    return new CacheListenerAdapter() {

        @Override
        public void afterCreate(EntryEvent event) {
            try {
                if (playDead) {
                    MembershipManagerHelper.beSickMember(getSystemStatic());
                    MembershipManagerHelper.playDead(getSystemStatic());
                }
                Thread.sleep(15000);
            } catch (InterruptedException ie) {
                fail("interrupted", ie);
            }
        }

        @Override
        public void afterRegionDestroy(RegionEvent event) {
            LogWriter logger = myCache.getLogger();
            logger.info("afterRegionDestroyed invoked in sleeping listener");
            logger.info("<ExpectedException action=remove>service failure</ExpectedException>");
            logger.info("<ExpectedException action=remove>org.apache.geode.ForcedDisconnectException</ExpectedException>");
            regionDestroyedInvoked = true;
        }
    };
}
Also used : CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) LogWriter(org.apache.geode.LogWriter) EntryEvent(org.apache.geode.cache.EntryEvent) RegionEvent(org.apache.geode.cache.RegionEvent)

Example 73 with EntryEvent

use of org.apache.geode.cache.EntryEvent in project geode by apache.

the class LRUEvictionControllerDUnitTest method testBug31592.

/**
   * Tests that a Region with an LRU capacity controller can be accessed from inside a cache
   * listener.
   */
@Test
public void testBug31592() throws Exception {
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    final Object key2 = "KEY2";
    final Object value2 = "VALUE2";
    AttributesFactory factory = new AttributesFactory();
    factory.setOffHeap(isOffHeapEnabled());
    factory.setScope(Scope.LOCAL);
    factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(10));
    final Region region = createRegion(name, factory.create());
    region.put(key, value);
    final Throwable[] errors = new Throwable[1];
    region.getAttributesMutator().addCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            try {
                LogWriterUtils.getLogWriter().info("AFTER CREATE");
                region.put(key, value2);
            } catch (VirtualMachineError e) {
                SystemFailure.initiateFailure(e);
                throw e;
            } catch (Throwable ex) {
                region.getCache().getLogger().severe("failed to access cache from listener", ex);
                errors[0] = ex;
            }
        }
    });
    region.put(key2, value2);
    assertNull(errors[0]);
    assertEquals(value2, region.get(key));
    assertEquals(value2, region.get(key2));
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 74 with EntryEvent

use of org.apache.geode.cache.EntryEvent in project geode by apache.

the class LocalRegionDUnitTest method testLocalCreateModifiedCallbackArgument.

/**
   * Tests that a local writer receives a modified version of the callback argument on a create.
   */
@Test
public void testLocalCreateModifiedCallbackArgument() throws CacheException {
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    final Object one = "ONE";
    final Object two = "TWO";
    TestCacheLoader loader = new TestCacheLoader() {

        public Object load2(LoaderHelper helper) throws CacheLoaderException {
            Object[] array = (Object[]) helper.getArgument();
            assertEquals(one, array[0]);
            array[0] = two;
            return value;
        }
    };
    TestCacheWriter writer = new TestCacheWriter() {

        public void beforeCreate2(EntryEvent event) throws CacheWriterException {
            Object[] array = (Object[]) event.getCallbackArgument();
            assertEquals(two, array[0]);
        }
    };
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    factory.setCacheLoader(loader);
    factory.setCacheWriter(writer);
    Region region = createRegion(name, factory.create());
    Object[] array = new Object[] { one };
    assertEquals(value, region.get(key, array));
    assertTrue(loader.wasInvoked());
    assertTrue(writer.wasInvoked());
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 75 with EntryEvent

use of org.apache.geode.cache.EntryEvent in project geode by apache.

the class DistributedNoAckRegionCCEDUnitTest method addBlockingListener.

static void addBlockingListener() {
    ListenerBlocking = true;
    CCRegion.getAttributesMutator().addCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            onEvent(event);
        }

        private void onEvent(EntryEvent event) {
            boolean blocked = false;
            if (event.isOriginRemote()) {
                synchronized (this) {
                    while (ListenerBlocking) {
                        LogWriterUtils.getLogWriter().info("blocking cache operations for " + event.getDistributedMember());
                        blocked = true;
                        try {
                            wait();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                            LogWriterUtils.getLogWriter().info("blocking cache listener interrupted");
                            return;
                        }
                    }
                }
                if (blocked) {
                    LogWriterUtils.getLogWriter().info("allowing cache operations for " + event.getDistributedMember());
                }
            }
        }

        @Override
        public void close() {
            LogWriterUtils.getLogWriter().info("closing blocking listener");
            ListenerBlocking = false;
            synchronized (this) {
                notifyAll();
            }
        }

        @Override
        public void afterUpdate(EntryEvent event) {
            onEvent(event);
        }

        @Override
        public void afterInvalidate(EntryEvent event) {
            onEvent(event);
        }

        @Override
        public void afterDestroy(EntryEvent event) {
            onEvent(event);
        }
    });
}
Also used : CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent)

Aggregations

EntryEvent (org.apache.geode.cache.EntryEvent)111 AttributesFactory (org.apache.geode.cache.AttributesFactory)75 Region (org.apache.geode.cache.Region)69 Test (org.junit.Test)66 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)55 RegionAttributes (org.apache.geode.cache.RegionAttributes)37 LocalRegion (org.apache.geode.internal.cache.LocalRegion)32 CacheException (org.apache.geode.cache.CacheException)30 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)30 VM (org.apache.geode.test.dunit.VM)29 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)26 CacheWriterException (org.apache.geode.cache.CacheWriterException)22 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)22 Host (org.apache.geode.test.dunit.Host)21 Properties (java.util.Properties)20 RegionEvent (org.apache.geode.cache.RegionEvent)18 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)17 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)15 ExpirationAttributes (org.apache.geode.cache.ExpirationAttributes)15 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)15