Search in sources :

Example 26 with RegionEvent

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

the class MultiVMRegionTestCase method testRemoteCacheListenerInSubregion.

/**
   * Tests that a {@link CacheListener} is invoked in a remote VM.
   */
@Test
public void testRemoteCacheListenerInSubregion() throws Exception {
    assumeTrue(supportsSubregions());
    assertTrue(getRegionAttributes().getScope().isDistributed());
    final String name = this.getUniqueName();
    SerializableRunnable create = new CacheSerializableRunnable("Create Region") {

        @Override
        public void run2() throws CacheException {
            createRegion(name);
        }
    };
    Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);
    vm0.invoke(new CacheSerializableRunnable("Create Root") {

        @Override
        public void run2() throws CacheException {
            createRootRegion();
        }
    });
    vm1.invoke(create);
    vm1.invoke(new CacheSerializableRunnable("Set listener") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(name);
            listener = new TestCacheListener() {

                @Override
                public void afterRegionInvalidate2(RegionEvent event) {
                    assertEquals(Operation.REGION_INVALIDATE, event.getOperation());
                    assertEquals(region, event.getRegion());
                    assertTrue(event.getOperation().isDistributed());
                    assertFalse(event.getOperation().isExpiration());
                    assertTrue(event.isOriginRemote());
                    assertEquals(event.getCallbackArgument(), event.getDistributedMember());
                }
            };
            region.getAttributesMutator().addCacheListener(listener);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Invalidate Root Region") {

        @Override
        public void run2() throws CacheException {
            getRootRegion().invalidateRegion(getSystem().getDistributedMember());
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Verify Invalidate Region") {

        @Override
        public void run2() throws CacheException {
            listener.waitForInvocation(3000, 10);
            // Setup listener for next test
            final Region region = getRootRegion().getSubregion(name);
            listener = new TestCacheListener() {

                @Override
                public void afterRegionDestroy2(RegionEvent event) {
                    assertEquals(Operation.REGION_DESTROY, event.getOperation());
                    assertEquals(region, event.getRegion());
                    assertTrue(event.getOperation().isDistributed());
                    assertFalse(event.getOperation().isExpiration());
                    assertTrue(event.isOriginRemote());
                    assertEquals(event.getCallbackArgument(), event.getDistributedMember());
                }
            };
            region.getAttributesMutator().addCacheListener(listener);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Destroy Root Region") {

        @Override
        public void run2() throws CacheException {
            getRootRegion().destroyRegion(getSystem().getDistributedMember());
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Verify Destroy Region") {

        @Override
        public void run2() throws CacheException {
            listener.waitForInvocation(3000, 10);
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) RegionEvent(org.apache.geode.cache.RegionEvent) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 27 with RegionEvent

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

the class MapInterface2JUnitTest method testSuccessGlobalScopeInSingleVM.

@Test
public void testSuccessGlobalScopeInSingleVM() {
    CacheUtils.getCache().setLockLease(10);
    CacheUtils.getCache().setLockTimeout(15);
    final Region region = CacheUtils.createRegion("Global", String.class, Scope.GLOBAL);
    for (int i = 0; i < 10; i++) {
        region.put("" + i, "" + i);
    }
    LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = true;
    CacheObserverHolder.setInstance(new CacheObserverAdapter() {

        public void afterRegionClear(RegionEvent event) {
            try {
                synchronized (MapInterface2JUnitTest.this) {
                    MapInterface2JUnitTest.this.mainThreadProceed = true;
                    MapInterface2JUnitTest.this.notify();
                }
                event.getRegion().getCache().getLogger().info("*******Main THread Notified *********");
                Thread.sleep(1000);
                event.getRegion().getCache().getLogger().info("******* After Sleeping 5000 *********");
            } catch (InterruptedException ie) {
                fail("interrupted");
            }
        }
    });
    Thread th = new Thread(new Runnable() {

        public void run() {
            region.clear();
        }
    });
    th.start();
    try {
        synchronized (this) {
            if (!this.mainThreadProceed) {
                region.getCache().getLogger().info("*******Main THread is going in wait********");
                this.wait();
            }
        }
        region.getCache().getLogger().info("*******Main THread coming out of wait*********");
        region.put("test", "test");
        String str = (String) region.get("test");
        assertNotNull("The put operation has succeeded", str);
    } catch (Exception cwe) {
        fail("The test experienced exception " + cwe);
    }
    ThreadUtils.join(th, 30 * 1000);
}
Also used : Region(org.apache.geode.cache.Region) RegionEvent(org.apache.geode.cache.RegionEvent) CommitConflictException(org.apache.geode.cache.CommitConflictException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 28 with RegionEvent

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

the class ClearPropagationDUnitTest method createClientCache.

public static void createClientCache(String host, Integer port1, Integer port2) throws Exception {
    PORT1 = port1.intValue();
    PORT2 = port2.intValue();
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new ClearPropagationDUnitTest().createCache(props);
    CacheServerTestUtil.disableShufflingOfEndpoints();
    Pool p;
    try {
        p = PoolManager.createFactory().addServer(host, PORT1).addServer(host, PORT2).setSubscriptionEnabled(true).setReadTimeout(2000).setSocketBufferSize(1000).setMinConnections(4).create("ClearPropagationDUnitTestPool");
    } finally {
        CacheServerTestUtil.enableShufflingOfEndpoints();
    }
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setPoolName(p.getName());
    factory.setCacheListener(new CacheListenerAdapter() {

        public void afterRegionClear(RegionEvent re) {
            synchronized (ClearPropagationDUnitTest.class) {
                gotClear = true;
                ClearPropagationDUnitTest.class.notify();
            }
        }

        public void afterRegionDestroy(RegionEvent re) {
            synchronized (ClearPropagationDUnitTest.class) {
                gotDestroyed = true;
                ClearPropagationDUnitTest.class.notify();
            }
        }
    });
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME, attrs);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) RegionAttributes(org.apache.geode.cache.RegionAttributes) Pool(org.apache.geode.cache.client.Pool) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent)

Example 29 with RegionEvent

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

the class EventIDVerificationInP2PDUnitTest method createServerCache.

public static void createServerCache(Integer type) throws Exception {
    new EventIDVerificationInP2PDUnitTest().createCache(new Properties());
    AttributesFactory factory = new AttributesFactory();
    if (type.intValue() == DISTRIBUTED_ACK)
        factory.setScope(Scope.DISTRIBUTED_ACK);
    if (type.intValue() == GLOBAL)
        factory.setScope(Scope.GLOBAL);
    else
        factory.setScope(Scope.DISTRIBUTED_NO_ACK);
    factory.setDataPolicy(DataPolicy.REPLICATE);
    factory.addCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            eventId = ((InternalCacheEvent) event).getEventId();
            if (receiver) {
                synchronized (EventIDVerificationInP2PDUnitTest.class) {
                    gotCallback = true;
                    EventIDVerificationInP2PDUnitTest.class.notify();
                }
            }
        }

        public void afterUpdate(EntryEvent event) {
            eventId = ((InternalCacheEvent) event).getEventId();
            if (receiver) {
                synchronized (EventIDVerificationInP2PDUnitTest.class) {
                    gotCallback = true;
                    EventIDVerificationInP2PDUnitTest.class.notify();
                }
            }
        }

        public void afterDestroy(EntryEvent event) {
            eventId = ((InternalCacheEvent) event).getEventId();
            if (receiver) {
                synchronized (EventIDVerificationInP2PDUnitTest.class) {
                    gotCallback = true;
                    EventIDVerificationInP2PDUnitTest.class.notify();
                }
            }
        }

        public void afterRegionDestroy(RegionEvent event) {
            eventId = ((InternalCacheEvent) event).getEventId();
            if (receiver) {
                synchronized (EventIDVerificationInP2PDUnitTest.class) {
                    gotCallback = true;
                    EventIDVerificationInP2PDUnitTest.class.notify();
                }
            }
        }

        public void afterRegionInvalidate(RegionEvent event) {
            eventId = ((InternalCacheEvent) event).getEventId();
            if (receiver) {
                synchronized (EventIDVerificationInP2PDUnitTest.class) {
                    gotCallback = true;
                    EventIDVerificationInP2PDUnitTest.class.notify();
                }
            }
        }
    });
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME, attrs);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) RegionAttributes(org.apache.geode.cache.RegionAttributes) EntryEvent(org.apache.geode.cache.EntryEvent) InternalCacheEvent(org.apache.geode.internal.cache.InternalCacheEvent) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent)

Aggregations

RegionEvent (org.apache.geode.cache.RegionEvent)29 Test (org.junit.Test)21 Region (org.apache.geode.cache.Region)20 AttributesFactory (org.apache.geode.cache.AttributesFactory)19 EntryEvent (org.apache.geode.cache.EntryEvent)18 CacheException (org.apache.geode.cache.CacheException)12 RegionAttributes (org.apache.geode.cache.RegionAttributes)12 LocalRegion (org.apache.geode.internal.cache.LocalRegion)11 Properties (java.util.Properties)10 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)10 VM (org.apache.geode.test.dunit.VM)9 CacheWriterException (org.apache.geode.cache.CacheWriterException)8 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)8 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)8 Host (org.apache.geode.test.dunit.Host)8 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)7 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)7 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)6 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)6 CacheWriter (org.apache.geode.cache.CacheWriter)5