Search in sources :

Example 96 with EntryEvent

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

the class ConcurrentMapOpsDUnitTest method doRetriedOperation.

private void doRetriedOperation(final Operation op, boolean usePR) {
    Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM server2 = host.getVM(1);
    final VM client = host.getVM(2);
    final int port1 = createRegionsAndStartServer(server1, true);
    final int port2 = createRegionsAndStartServer(server2, true);
    final String regionName = usePR ? PR_REG_NAME : REP_REG_NAME;
    IgnoredException.addIgnoredException("java.net.SocketException");
    createClientRegion(client, port1, false, port2);
    SerializableCallable getID = new SerializableCallable("get DM ID") {

        public Object call() {
            return getSystem().getDistributedMember();
        }
    };
    final DistributedMember server1ID = (DistributedMember) server1.invoke(getID);
    final DistributedMember server2ID = (DistributedMember) server2.invoke(getID);
    Set<IgnoredException> exceptions = new HashSet<IgnoredException>();
    exceptions.add(IgnoredException.addIgnoredException("Membership: requesting removal", server1));
    exceptions.add(IgnoredException.addIgnoredException("Membership: requesting removal", server2));
    exceptions.add(IgnoredException.addIgnoredException("ForcedDisconnect", server1));
    exceptions.add(IgnoredException.addIgnoredException("ForcedDisconnect", server2));
    try {
        server1.invoke(new SerializableCallable("install crasher in server1") {

            public Object call() throws Exception {
                Region r = getCache().getRegion(regionName);
                r.put("key0", "value");
                if (op == Operation.PUT_IF_ABSENT) {
                    r.destroy("key0");
                }
                // force client to use server1 for now
                // getCache().getCacheServers().get(0).stop();
                r.getAttributesMutator().addCacheListener(new CacheListenerAdapter() {

                    private void killSender(EntryEvent event) {
                        if (event.isOriginRemote()) {
                            MembershipManager mgr = MembershipManagerHelper.getMembershipManager(getSystem());
                            mgr.requestMemberRemoval(server2ID, "removing for test");
                            try {
                                mgr.waitForDeparture(server2ID);
                            } catch (Exception e) {
                                fail("failed to stop the other server for this test:" + e.getMessage());
                            }
                        }
                    }

                    @Override
                    public void afterCreate(EntryEvent event) {
                        getCache().getLogger().info("afterCreate invoked with " + event);
                        killSender(event);
                    }

                    @Override
                    public void afterUpdate(EntryEvent event) {
                        getCache().getLogger().info("afterUpdate invoked with " + event);
                        killSender(event);
                    }

                    @Override
                    public void afterDestroy(EntryEvent event) {
                        getCache().getLogger().info("afterDestroy invoked with " + event);
                        killSender(event);
                    }
                });
                return null;
            }
        });
        server2.invoke(new SerializableCallable("install crasher in server2") {

            public Object call() throws Exception {
                Region r = getCache().getRegion(regionName);
                // force client to use server1 for now
                // getCache().getCacheServers().get(0).stop();
                r.getAttributesMutator().addCacheListener(new CacheListenerAdapter() {

                    private void killSender(EntryEvent event) {
                        if (event.isOriginRemote()) {
                            MembershipManager mgr = MembershipManagerHelper.getMembershipManager(getSystem());
                            mgr.requestMemberRemoval(server1ID, "removing for test");
                            try {
                                mgr.waitForDeparture(server1ID);
                            } catch (Exception e) {
                                fail("failed to stop the other server for this test:" + e.getMessage());
                            }
                        }
                    }

                    @Override
                    public void afterCreate(EntryEvent event) {
                        getCache().getLogger().info("afterCreate invoked with " + event);
                        killSender(event);
                    }

                    @Override
                    public void afterUpdate(EntryEvent event) {
                        getCache().getLogger().info("afterUpdate invoked with " + event);
                        killSender(event);
                    }

                    @Override
                    public void afterDestroy(EntryEvent event) {
                        getCache().getLogger().info("afterDestroy invoked with " + event);
                        killSender(event);
                    }
                });
                return null;
            }
        });
        client.invoke(new SerializableRunnable() {

            public void run() {
                GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
                Region r = cache.getRegion(regionName);
                if (op == Operation.PUT_IF_ABSENT) {
                    assertTrue("expected putIfAbsent to succeed and return null", r.putIfAbsent("key0", "newvalue") == null);
                } else if (op == Operation.REMOVE) {
                    assertTrue("expected remove operation to succeed and return true", r.remove("key0", "value"));
                } else if (op == Operation.REPLACE) {
                    assertTrue("expected replace operation to succeed and return true", r.replace("key0", "value", "newvalue"));
                }
            }
        });
    } finally {
        disconnectAllFromDS();
        for (IgnoredException ex : exceptions) {
            ex.remove();
        }
    }
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) InvalidDeltaException(org.apache.geode.InvalidDeltaException) IOException(java.io.IOException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) EntryEvent(org.apache.geode.cache.EntryEvent) DistributedMember(org.apache.geode.distributed.DistributedMember) IgnoredException(org.apache.geode.test.dunit.IgnoredException) Region(org.apache.geode.cache.Region) MembershipManager(org.apache.geode.distributed.internal.membership.MembershipManager)

Example 97 with EntryEvent

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

the class DeltaPropagationStatsDUnitTest method createClientCache.

public static void createClientCache(String host, Integer port) throws Exception {
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    cache = new DeltaPropagationStatsDUnitTest().createCache(props);
    pool = PoolManager.createFactory().addServer(host, port).setThreadLocalConnections(true).setMinConnections(1).setSubscriptionEnabled(true).setSubscriptionRedundancy(0).setReadTimeout(10000).setSocketBufferSize(32768).create("DeltaPropagationStatsDunitTestPool");
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setDataPolicy(DataPolicy.NORMAL);
    factory.setPoolName(pool.getName());
    factory.setCloningEnabled(false);
    factory.addCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            if (LAST_KEY.equals(event.getKey())) {
                lastKeyReceived = true;
            }
        }
    });
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME, attrs).registerInterest("ALL_KEYS");
}
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) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties)

Example 98 with EntryEvent

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

the class ConflationDUnitTest method createClientCache2UniqueWriter.

/**
   * create client 2 with 2 regions each with a unique writer but both having a common listener
   * 
   * @throws Exception
   */
public static void createClientCache2UniqueWriter(String host, Integer port) throws Exception {
    ConflationDUnitTest test = new ConflationDUnitTest();
    cache = test.createCache(createProperties1());
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    factory.setPoolName(createPool(host, "p1", port, true).getName());
    factory.addCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            String val = (String) event.getNewValue();
            LogWriterUtils.getLogWriter().info("Listener received event " + event);
            synchronized (ConflationDUnitTest.class) {
                if (val.equals(MARKER)) {
                    count++;
                } else {
                    counterCreate++;
                }
                if (2 == count) {
                    ConflationDUnitTest.class.notify();
                }
            }
        }

        public void afterUpdate(EntryEvent event) {
            synchronized (this) {
                counterUpdate++;
            }
        }

        public void afterDestroy(EntryEvent event) {
            synchronized (this) {
                if (!event.getKey().equals(MARKER)) {
                    counterDestroy++;
                }
            }
        }
    });
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME1, attrs);
    factory.setPoolName(createPool(host, "p2", port, true).getName());
    attrs = factory.create();
    cache.createRegion(REGION_NAME2, 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)

Example 99 with EntryEvent

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

the class ConflationDUnitTest method createClientCache2CommonWriterTest3.

public static void createClientCache2CommonWriterTest3(String host, Integer port) throws Exception {
    ConflationDUnitTest test = new ConflationDUnitTest();
    cache = test.createCache(createProperties1());
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    factory.setPoolName(createPool(host, "p1", port, true).getName());
    factory.addCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            LogWriterUtils.getLogWriter().info("Listener received event " + event);
            String val = (String) event.getNewValue();
            synchronized (ConflationDUnitTest.class) {
                if (val.equals(MARKER)) {
                    count++;
                } else {
                    counterCreate++;
                }
                if (2 == count) {
                    ConflationDUnitTest.class.notify();
                }
            }
        }

        public void afterUpdate(EntryEvent event) {
            LogWriterUtils.getLogWriter().info("Listener received event " + event);
            synchronized (this) {
                counterUpdate++;
            }
        }

        public void afterDestroy(EntryEvent event) {
            LogWriterUtils.getLogWriter().info("Listener received event " + event);
            synchronized (this) {
                if (!event.getKey().equals(MARKER)) {
                    counterDestroy++;
                }
            }
        }
    });
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME1, attrs);
    cache.createRegion(REGION_NAME2, 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)

Example 100 with EntryEvent

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

the class ExportLogsCacheWriterUnitTest method writerReturnsPathIfWritten.

@Test
public void writerReturnsPathIfWritten() throws Exception {
    writer.startFile("server-1");
    EntryEvent event = mock(EntryEvent.class);
    when(event.getNewValue()).thenReturn(new byte[] {});
    writer.beforeCreate(event);
    assertThat(writer.endFile()).isNotNull();
}
Also used : EntryEvent(org.apache.geode.cache.EntryEvent) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

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