Search in sources :

Example 16 with RegionEvent

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

the class RegionTestCase method testCloseRegion.

/**
   * Tests closing a region, and checks different behavior when this is a disk region with
   * persistBackup.
   */
@Test
public void testCloseRegion() throws CacheException {
    // @todo added a remote region to make sure close just does a localDestroy
    String name = this.getUniqueName();
    AttributesFactory fac = new AttributesFactory(getRegionAttributes());
    TestCacheListener list = new TestCacheListener() {

        public void afterCreate2(EntryEvent event) {
        // do nothing
        }

        public void afterRegionDestroy2(RegionEvent re) {
            assertEquals(Operation.REGION_CLOSE, re.getOperation());
        }

        public void close2() {
        // okay
        }
    };
    fac.setCacheListener(list);
    RegionAttributes attrs = fac.create();
    Region region = createRegion(name, attrs);
    File diskDir = null;
    if (attrs.getDataPolicy().withPersistence()) {
        diskDir = getCache().findDiskStore(attrs.getDiskStoreName()).getDiskDirs()[0];
        // @todo We no longer start with a clean slate because the DiskStore hangs around.
        // If we want a clean slate then we need to destroy the DiskStore after each
        // test completes.
        // assert that if this is a disk region, the disk dirs are empty
        // to make sure we start with a clean slate
        getCache().getLogger().info("list=" + Arrays.toString(diskDir.list()));
    // assertIndexDetailsEquals("list="+Arrays.toString(diskDir.list()),
    // 0, diskDir.list().length);
    }
    for (int i = 0; i < 1000; i++) {
        region.put(new Integer(i), String.valueOf(i));
    }
    // reset wasInvoked after creates
    assertTrue(list.wasInvoked());
    // assert that if this is a disk region, the disk dirs are not empty
    if (attrs.getDataPolicy().withPersistence()) {
        assertTrue(diskDir.list().length > 0);
    }
    boolean persistent = region.getAttributes().getDataPolicy().withPersistence();
    region.close();
    // assert that if this is a disk region, the disk dirs are not empty
    if (attrs.getDataPolicy().withPersistence()) {
        assertTrue(diskDir.list().length > 0);
    }
    assertTrue(list.waitForInvocation(333));
    assertTrue(list.isClosed());
    assertTrue(region.isDestroyed());
    // if (persistent) {
    // // remove this when bug #41049 is fixed
    // return;
    // }
    // if this is a disk region, then check to see if recreating the region
    // repopulates with data
    region = createRegion(name, attrs);
    if (attrs.getDataPolicy().withPersistence()) {
        for (int i = 0; i < 1000; i++) {
            Region.Entry entry = region.getEntry(new Integer(i));
            assertNotNull("entry " + i + " not found", entry);
            assertEquals(String.valueOf(i), entry.getValue());
        }
        assertEquals(1000, region.keySet().size());
    } else {
        assertEquals(0, region.keySet().size());
    }
    region.localDestroyRegion();
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Entry(org.apache.geode.cache.Region.Entry) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) RegionEvent(org.apache.geode.cache.RegionEvent) File(java.io.File) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 17 with RegionEvent

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

the class RegionReliabilityTestCase method testRegionDistributionException.

@Test
public void testRegionDistributionException() throws Exception {
    if (getRegionScope().isDistributedNoAck())
        // skip test under DistributedNoAck
        return;
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    final String[] requiredRoles = { roleA };
    Set requiredRolesSet = new HashSet();
    for (int i = 0; i < requiredRoles.length; i++) {
        requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
    }
    assertEquals(requiredRoles.length, requiredRolesSet.size());
    // connect controller to system...
    Properties config = new Properties();
    config.setProperty(ROLES, "");
    getSystem(config);
    getCache();
    RegionMembershipListener listener = new RegionMembershipListenerAdapter() {

        public void afterRemoteRegionDeparture(RegionEvent event) {
            synchronized (detectedDeparture_testRegionDistributionException) {
                detectedDeparture_testRegionDistributionException[0] = Boolean.TRUE;
                detectedDeparture_testRegionDistributionException.notify();
            }
        }
    };
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.NO_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    // fac.addCacheListener(listener);
    RegionAttributes attr = fac.create();
    Region region = createRootRegion(name, attr);
    assertTrue(((AbstractRegion) region).requiresReliabilityCheck());
    // use vm1 to create role
    CacheSerializableRunnable createRegion = new CacheSerializableRunnable("Create Region") {

        public void run2() throws CacheException {
            createConnection(new String[] { roleA });
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            RegionAttributes attr = fac.create();
            createRootRegion(name, attr);
        }
    };
    Host.getHost(0).getVM(1).invoke(createRegion);
    region.put("DESTROY_ME", "VAL");
    region.put("INVALIDATE_ME", "VAL");
    // define the afterReleaseLocalLocks callback
    SerializableRunnable removeRequiredRole = new SerializableRunnable() {

        public void run() {
            Host.getHost(0).getVM(1).invoke(new SerializableRunnable("Close Region") {

                public void run() {
                    getRootRegion(name).close();
                }
            });
        // try {
        // synchronized (detectedDeparture_testRegionDistributionException) {
        // while (detectedDeparture_testRegionDistributionException[0] == Boolean.FALSE) {
        // detectedDeparture_testRegionDistributionException.wait();
        // }
        // }
        // }
        // catch (InterruptedException e) {}
        }
    };
    DistributedCacheOperation.setBeforePutOutgoing(() -> {
        try {
            removeRequiredRole.run();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
    Runnable reset = new Runnable() {

        public void run() {
        // synchronized (detectedDeparture_testRegionDistributionException) {
        // detectedDeparture_testRegionDistributionException[0] = Boolean.FALSE;
        // }
        }
    };
    // PUT
    try {
        region.put("KEY", "VAL");
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // INVALIDATE
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.invalidate("INVALIDATE_ME");
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // DESTROY
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.destroy("DESTROY_ME");
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // CLEAR
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.clear();
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // PUTALL
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        Map putAll = new HashMap();
        putAll.put("PUTALL_ME", "VAL");
        region.putAll(putAll);
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // INVALIDATE REGION
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.invalidateRegion();
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // DESTROY REGION
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.destroyRegion();
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RegionAttributes(org.apache.geode.cache.RegionAttributes) HashMap(java.util.HashMap) RegionDistributionException(org.apache.geode.cache.RegionDistributionException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) RegionMembershipListener(org.apache.geode.cache.RegionMembershipListener) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent) RegionReinitializedException(org.apache.geode.cache.RegionReinitializedException) CommitDistributionException(org.apache.geode.cache.CommitDistributionException) RegionDistributionException(org.apache.geode.cache.RegionDistributionException) RegionAccessException(org.apache.geode.cache.RegionAccessException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheException(org.apache.geode.cache.CacheException) AttributesFactory(org.apache.geode.cache.AttributesFactory) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) RegionMembershipListenerAdapter(org.apache.geode.cache.util.RegionMembershipListenerAdapter) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 18 with RegionEvent

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

the class HAClearDUnitTest method createClientCache.

public static void createClientCache(String hostName, Integer port1, Integer port2, Boolean listenerAttached, Boolean registerInterest) throws Exception {
    int PORT1 = port1.intValue();
    int PORT2 = port2.intValue();
    boolean isListenerAttached = listenerAttached.booleanValue();
    boolean isRegisterInterest = registerInterest.booleanValue();
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new HAClearDUnitTest().createCache(props);
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    ClientServerTestCase.configureConnectionPool(factory, hostName, new int[] { PORT1, PORT2 }, true, -1, 2, null);
    if (isListenerAttached) {
        factory.setCacheListener(new CacheListenerAdapter() {

            public void afterRegionClear(RegionEvent event) {
                LogWriterUtils.getLogWriter().info("-------> afterRegionClear received");
                synchronized (HAClearDUnitTest.class) {
                    gotClearCallback = true;
                    HAClearDUnitTest.class.notifyAll();
                }
            }

            public void afterRegionDestroy(RegionEvent event) {
                synchronized (HAClearDUnitTest.class) {
                    LogWriterUtils.getLogWriter().info("-------> afterRegionDestroy received");
                    gotDestroyRegionCallback = true;
                    HAClearDUnitTest.class.notifyAll();
                }
            }
        });
    }
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME, attrs);
    Region region = cache.getRegion(Region.SEPARATOR + REGION_NAME);
    assertNotNull(region);
    if (isRegisterInterest) {
        region.registerInterest("ALL_KEYS", InterestResultPolicy.NONE);
    } else {
        // don't want data but do want events
        region.registerInterestRegex("*.", false, false);
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) RegionAttributes(org.apache.geode.cache.RegionAttributes) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent)

Example 19 with RegionEvent

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

the class EventIdOptimizationDUnitTest method createClientCache2.

/**
   * Creates the client cache2, connected to server3
   * 
   * @param port - bridgeserver port
   * @throws Exception - thrown if any problem occurs in setting up the client
   */
public static void createClientCache2(String hostName, Integer port) throws Exception {
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new EventIdOptimizationDUnitTest().createCache(props);
    AttributesFactory factory = new AttributesFactory();
    ClientServerTestCase.configureConnectionPool(factory, hostName, port.intValue(), -1, true, -1, 2, null);
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.addCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            String key = (String) event.getKey();
            validateEventsAtReceivingClientListener(key);
        }

        public void afterDestroy(EntryEvent event) {
            String key = (String) event.getKey();
            validateEventsAtReceivingClientListener(key);
        }

        public void afterRegionDestroy(RegionEvent event) {
            validateEventsAtReceivingClientListener(" <destroyRegion Event> ");
        }

        public void afterRegionClear(RegionEvent event) {
            validateEventsAtReceivingClientListener(" <clearRegion Event> ");
        }
    });
    RegionAttributes attrs = factory.create();
    Region region = cache.createRegion(REGION_NAME, attrs);
    region.registerInterest("ALL_KEYS");
    for (int i = 0; i < eventIds.length; i++) {
        region = cache.createRegion(REGION_NAME + i, attrs);
        region.registerInterest("ALL_KEYS");
    }
    pool = (PoolImpl) PoolManager.find("testPool");
}
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) Region(org.apache.geode.cache.Region) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent)

Example 20 with RegionEvent

use of org.apache.geode.cache.RegionEvent 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)

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