Search in sources :

Example 51 with CacheListenerAdapter

use of org.apache.geode.cache.util.CacheListenerAdapter 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 52 with CacheListenerAdapter

use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.

the class ClientServerTransactionDUnitTest method testAdjunctMessage.

@Test
public void testAdjunctMessage() {
    Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM client1 = host.getVM(2);
    VM client2 = host.getVM(3);
    final String regionName = "testAdjunctMessage";
    final int port1 = createRegionsAndStartServer(server1, false);
    final int port2 = createRegionsAndStartServer(server2, false);
    SerializableCallable createServerRegionWithInterest = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            RegionFactory rf = getCache().createRegionFactory(RegionShortcut.PARTITION);
            rf.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.CACHE_CONTENT));
            rf.create(regionName);
            return null;
        }
    };
    server1.invoke(createServerRegionWithInterest);
    server2.invoke(createServerRegionWithInterest);
    // get two colocated keys on server1
    final List<String> keys = (List<String>) server1.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region r = getCache().getRegion(regionName);
            PartitionedRegion pr = (PartitionedRegion) r;
            List<String> server1Keys = new ArrayList<String>();
            for (int i = 0; i < 100; i++) {
                String key = "k" + i;
                // pr.put(key, "v" + i);
                DistributedMember owner = pr.getOwnerForKey(pr.getKeyInfo(key));
                if (owner.equals(pr.getMyId())) {
                    server1Keys.add(key);
                    if (server1Keys.size() == 2) {
                        break;
                    }
                }
            }
            return server1Keys;
        }
    });
    class ClientListener extends CacheListenerAdapter {

        Set keys = new HashSet();

        @Override
        public void afterCreate(EntryEvent event) {
            add(event);
        }

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

        private void add(EntryEvent event) {
            keys.add(event.getKey());
        }
    }
    client2.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "bridge.disableShufflingOfEndpoints", "true");
            ClientCacheFactory ccf = new ClientCacheFactory();
            ccf.addPoolServer("localhost", /* getServerHostName(Host.getHost(0)) */
            port2);
            ccf.setPoolMinConnections(0);
            ccf.setPoolSubscriptionEnabled(true);
            ccf.setPoolSubscriptionRedundancy(0);
            ccf.set(LOG_LEVEL, getDUnitLogLevel());
            ClientCache cCache = getClientCache(ccf);
            Region r = cCache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).addCacheListener(new ClientListener()).create(regionName);
            r.registerInterestRegex(".*");
            // cCache.readyForEvents();
            return null;
        }
    });
    client1.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "bridge.disableShufflingOfEndpoints", "true");
            ClientCacheFactory ccf = new ClientCacheFactory();
            ccf.addPoolServer("localhost", /* getServerHostName(Host.getHost(0)) */
            port1);
            ccf.setPoolMinConnections(0);
            ccf.setPoolSubscriptionEnabled(true);
            ccf.set(LOG_LEVEL, getDUnitLogLevel());
            ClientCache cCache = getClientCache(ccf);
            Region r = cCache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
            getCache().getCacheTransactionManager().begin();
            for (String key : keys) {
                r.put(key, "value");
            }
            getCache().getCacheTransactionManager().commit();
            return null;
        }
    });
    client2.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region r = getCache().getRegion(regionName);
            CacheListener[] listeners = r.getAttributes().getCacheListeners();
            boolean foundListener = false;
            for (CacheListener listener : listeners) {
                if (listener instanceof ClientListener) {
                    foundListener = true;
                    final ClientListener clientListener = (ClientListener) listener;
                    WaitCriterion wc = new WaitCriterion() {

                        @Override
                        public boolean done() {
                            return clientListener.keys.containsAll(keys);
                        }

                        @Override
                        public String description() {
                            return "expected:" + keys + " found:" + clientListener.keys;
                        }
                    };
                    Wait.waitForCriterion(wc, 30 * 1000, 500, true);
                }
            }
            assertTrue(foundListener);
            return null;
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RollbackException(javax.transaction.RollbackException) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 53 with CacheListenerAdapter

use of org.apache.geode.cache.util.CacheListenerAdapter 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 54 with CacheListenerAdapter

use of org.apache.geode.cache.util.CacheListenerAdapter 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 55 with CacheListenerAdapter

use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.

the class ConnectionProxyJUnitTest method testListenerOnServerSitForever.

/**
   * This test verifies the behaviour of client request when the listener on the server sits
   * forever. This is done in following steps:<br>
   * 1)create server<br>
   * 2)initialize proxy object and create region for client having a CacheListener and make
   * afterCreate in the listener to wait infinitely<br>
   * 3)perform a PUT on client by acquiring Connection through proxy<br>
   * 4)Verify that exception occurs due to infinite wait in the listener<br>
   * 5)Verify that above exception occurs sometime after the readTimeout configured for the client
   * <br>
   *
   */
@Ignore
@Test
public void testListenerOnServerSitForever() throws Exception {
    int port3 = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    Region testRegion = null;
    CacheServer server = this.cache.addCacheServer();
    server.setMaximumTimeBetweenPings(10000);
    server.setPort(port3);
    server.start();
    try {
        PoolFactory pf = PoolManager.createFactory();
        pf.addServer("localhost", port3);
        pf.setSubscriptionEnabled(false);
        pf.setSubscriptionRedundancy(-1);
        pf.setReadTimeout(2000);
        pf.setThreadLocalConnections(true);
        pf.setSocketBufferSize(32768);
        pf.setRetryAttempts(1);
        pf.setPingInterval(10000);
        proxy = (PoolImpl) pf.create("clientPool");
        AttributesFactory factory = new AttributesFactory();
        factory.setScope(Scope.DISTRIBUTED_ACK);
        factory.setCacheListener(new CacheListenerAdapter() {

            public void afterCreate(EntryEvent event) {
                synchronized (ConnectionProxyJUnitTest.this) {
                    try {
                        ConnectionProxyJUnitTest.this.wait();
                    } catch (InterruptedException e) {
                        fail("interrupted");
                    }
                }
            }
        });
        RegionAttributes attrs = factory.create();
        testRegion = cache.createRegion("testregion", attrs);
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Failed to initialize client");
    }
    Connection conn = (proxy).acquireConnection();
    long t1 = 0;
    try {
        t1 = System.currentTimeMillis();
        EntryEventImpl event = new EntryEventImpl((Object) null);
        try {
            event.setEventId(new EventID(new byte[] { 1 }, 1, 1));
            PutOp.execute(conn, proxy, testRegion.getFullPath(), "key1", "val1", event, null, false);
        } finally {
            event.release();
        }
        fail("Test failed as exception was expected");
    } catch (Exception e) {
        long t2 = System.currentTimeMillis();
        long net = (t2 - t1);
        assertTrue(net / 1000 < 5);
    }
    synchronized (ConnectionProxyJUnitTest.this) {
        ConnectionProxyJUnitTest.this.notify();
    }
}
Also used : EntryEventImpl(org.apache.geode.internal.cache.EntryEventImpl) Connection(org.apache.geode.cache.client.internal.Connection) PoolFactory(org.apache.geode.cache.client.PoolFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) CacheServer(org.apache.geode.cache.server.CacheServer) EventID(org.apache.geode.internal.cache.EventID) Ignore(org.junit.Ignore) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)66 EntryEvent (org.apache.geode.cache.EntryEvent)55 AttributesFactory (org.apache.geode.cache.AttributesFactory)40 Region (org.apache.geode.cache.Region)30 Test (org.junit.Test)25 RegionAttributes (org.apache.geode.cache.RegionAttributes)21 Properties (java.util.Properties)20 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)16 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)15 VM (org.apache.geode.test.dunit.VM)14 CacheException (org.apache.geode.cache.CacheException)11 CacheListener (org.apache.geode.cache.CacheListener)11 LocalRegion (org.apache.geode.internal.cache.LocalRegion)9 Host (org.apache.geode.test.dunit.Host)9 RegionEvent (org.apache.geode.cache.RegionEvent)8 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)8 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)7 CacheServer (org.apache.geode.cache.server.CacheServer)7 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)7 SubscriptionAttributes (org.apache.geode.cache.SubscriptionAttributes)6