Search in sources :

Example 16 with CertifiableTestCacheListener

use of org.apache.geode.cache30.CertifiableTestCacheListener in project geode by apache.

the class ConnectionPoolDUnitTest method test028DynamicRegionCreation.

/**
   * Test dynamic region creation instantiated from a bridge client causing regions to be created on
   * two different bridge servers.
   *
   * Also tests the reverse situation, a dynamic region is created on the bridge server expecting
   * the same region to be created on the client.
   *
   * Note: This test re-creates Distributed Systems for its own purposes and uses a Loner
   * distributed systems to isolate the Bridge Client.
   *
   * @throws Exception
   */
@Test
public void test028DynamicRegionCreation() throws Exception {
    final String name = this.getName();
    final Host host = Host.getHost(0);
    final VM client1 = host.getVM(0);
    // VM client2 = host.getVM(1);
    final VM srv1 = host.getVM(2);
    final VM srv2 = host.getVM(3);
    final String k1 = name + "-key1";
    final String v1 = name + "-val1";
    final String k2 = name + "-key2";
    final String v2 = name + "-val2";
    final String k3 = name + "-key3";
    final String v3 = name + "-val3";
    client1.invoke(() -> disconnectFromDS());
    srv1.invoke(() -> disconnectFromDS());
    srv2.invoke(() -> disconnectFromDS());
    try {
        // setup servers
        CacheSerializableRunnable ccs = new CacheSerializableRunnable("Create Cache Server") {

            public void run2() throws CacheException {
                // Creates a new DS and Cache
                createDynamicRegionCache(name, (String) null);
                assertTrue(DynamicRegionFactory.get().isOpen());
                try {
                    startBridgeServer(0);
                } catch (IOException ugh) {
                    fail("Bridge Server startup failed");
                }
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setDataPolicy(DataPolicy.REPLICATE);
                factory.setConcurrencyChecksEnabled(false);
                Region region = createRootRegion(name, factory.create());
                region.put(k1, v1);
                Assert.assertTrue(region.get(k1).equals(v1));
            }
        };
        srv1.invoke(ccs);
        srv2.invoke(ccs);
        final String srv1Host = NetworkUtils.getServerHostName(srv1.getHost());
        final int srv1Port = srv1.invoke(() -> ConnectionPoolDUnitTest.getCacheServerPort());
        final int srv2Port = srv2.invoke(() -> ConnectionPoolDUnitTest.getCacheServerPort());
        // final String srv2Host = getServerHostName(srv2.getHost());
        // setup clients, do basic tests to make sure pool with notifier work as advertised
        client1.invoke(new CacheSerializableRunnable("Create Cache Client") {

            public void run2() throws CacheException {
                createLonerDS();
                AttributesFactory factory = new AttributesFactory();
                factory.setConcurrencyChecksEnabled(false);
                Pool cp = ClientServerTestCase.configureConnectionPool(factory, srv1Host, srv1Port, srv2Port, true, -1, -1, null);
                {
                    final PoolImpl pool = (PoolImpl) cp;
                    WaitCriterion ev = new WaitCriterion() {

                        public boolean done() {
                            if (pool.getPrimary() == null) {
                                return false;
                            }
                            if (pool.getRedundants().size() < 1) {
                                return false;
                            }
                            return true;
                        }

                        public String description() {
                            return null;
                        }
                    };
                    Wait.waitForCriterion(ev, 30 * 1000, 200, true);
                    assertNotNull(pool.getPrimary());
                    assertTrue("backups=" + pool.getRedundants() + " expected=" + 1, pool.getRedundants().size() >= 1);
                }
                createDynamicRegionCache(name, "testPool");
                assertTrue(DynamicRegionFactory.get().isOpen());
                factory.setScope(Scope.LOCAL);
                factory.setConcurrencyChecksEnabled(false);
                factory.setCacheListener(new CertifiableTestCacheListener(org.apache.geode.test.dunit.LogWriterUtils.getLogWriter()));
                Region region = createRootRegion(name, factory.create());
                assertNull(region.getEntry(k1));
                // this should match
                region.registerInterestRegex(".*", InterestResultPolicy.KEYS_VALUES);
                // the key
                // Update via registered interest
                assertEquals(v1, region.getEntry(k1).getValue());
                assertNull(region.getEntry(k2));
                // use the Pool
                region.put(k2, v2);
                // Ensure that the notifier didn't un-do
                assertEquals(v2, region.getEntry(k2).getValue());
                // the put, bug 35355
                // setup a key for invalidation from a notifier
                region.put(k3, v3);
            }
        });
        srv1.invoke(new CacheSerializableRunnable("Validate Server1 update") {

            public void run2() throws CacheException {
                CacheClientNotifier ccn = CacheClientNotifier.getInstance();
                final CacheClientNotifierStats ccnStats = ccn.getStats();
                final int eventCount = ccnStats.getEvents();
                Region r = getRootRegion(name);
                assertNotNull(r);
                // Validate the Pool worked, getEntry works
                assertEquals(v2, r.getEntry(k2).getValue());
                // because of the mirror
                // Make sure we have the other entry to use
                assertEquals(v3, r.getEntry(k3).getValue());
                // for notification
                // Change k3, sending some data to the client notifier
                r.put(k3, v1);
                // Wait for the update to propagate to the clients
                final int maxTime = 20000;
                // long start = System.currentTimeMillis();
                WaitCriterion ev = new WaitCriterion() {

                    public boolean done() {
                        return ccnStats.getEvents() > eventCount;
                    }

                    public String description() {
                        return "waiting for ccnStat";
                    }
                };
                Wait.waitForCriterion(ev, maxTime, 200, true);
            // Set prox = ccn.getClientProxies();
            // assertIndexDetailsEquals(1, prox.size());
            // for (Iterator cpi = prox.iterator(); cpi.hasNext(); ) {
            // CacheClientProxy ccp = (CacheClientProxy) cpi.next();
            // start = System.currentTimeMillis();
            // while (ccp.getMessagesProcessed() < 1) {
            // assertTrue("Waited more than " + maxTime + "ms for client notification",
            // (System.currentTimeMillis() - start) < maxTime);
            // try {
            // Thread.sleep(100);
            // } catch (InterruptedException ine) { fail("Interrupted while waiting for client
            // notifier to complete"); }
            // }
            // }
            }
        });
        srv2.invoke(new CacheSerializableRunnable("Validate Server2 update") {

            public void run2() throws CacheException {
                Region r = getRootRegion(name);
                assertNotNull(r);
                // Validate the Pool worked, getEntry works
                assertEquals(v2, r.getEntry(k2).getValue());
                // because of the mirror
                // From peer update
                assertEquals(v1, r.getEntry(k3).getValue());
            }
        });
        client1.invoke(new CacheSerializableRunnable("Validate Client notification") {

            public void run2() throws CacheException {
                Region r = getRootRegion(name);
                assertNotNull(r);
                CertifiableTestCacheListener ctl = (CertifiableTestCacheListener) r.getAttributes().getCacheListener();
                ctl.waitForUpdated(k3);
                // Ensure that the notifier updated the entry
                assertEquals(v1, r.getEntry(k3).getValue());
            }
        });
        // Ok, now we are ready to do some dynamic region action!
        final String v1Dynamic = v1 + "dynamic";
        final String dynFromClientName = name + "-dynamic-client";
        final String dynFromServerName = name + "-dynamic-server";
        client1.invoke(new CacheSerializableRunnable("Client dynamic region creation") {

            public void run2() throws CacheException {
                assertTrue(DynamicRegionFactory.get().isOpen());
                Region r = getRootRegion(name);
                assertNotNull(r);
                Region dr = DynamicRegionFactory.get().createDynamicRegion(name, dynFromClientName);
                // This should be enough to validate the creation on the server
                assertNull(dr.get(k1));
                dr.put(k1, v1Dynamic);
                assertEquals(v1Dynamic, dr.getEntry(k1).getValue());
            }
        });
        // Assert the servers have the dynamic region and the new value
        CacheSerializableRunnable valDR = new CacheSerializableRunnable("Validate dynamic region creation on server") {

            public void run2() throws CacheException {
                Region r = getRootRegion(name);
                assertNotNull(r);
                long end = System.currentTimeMillis() + 10000;
                Region dr = null;
                for (; ; ) {
                    try {
                        dr = r.getSubregion(dynFromClientName);
                        assertNotNull(dr);
                        assertNotNull(getCache().getRegion(name + Region.SEPARATOR + dynFromClientName));
                        break;
                    } catch (AssertionError e) {
                        if (System.currentTimeMillis() > end) {
                            throw e;
                        }
                    }
                }
                assertEquals(v1Dynamic, dr.getEntry(k1).getValue());
            }
        };
        srv1.invoke(valDR);
        srv2.invoke(valDR);
        // now delete the dynamic region and see if it goes away on servers
        client1.invoke(new CacheSerializableRunnable("Client dynamic region destruction") {

            public void run2() throws CacheException {
                assertTrue(DynamicRegionFactory.get().isActive());
                Region r = getRootRegion(name);
                assertNotNull(r);
                String drName = r.getFullPath() + Region.SEPARATOR + dynFromClientName;
                assertNotNull(getCache().getRegion(drName));
                DynamicRegionFactory.get().destroyDynamicRegion(drName);
                assertNull(getCache().getRegion(drName));
            }
        });
        // Assert the servers no longer have the dynamic region
        CacheSerializableRunnable valNoDR = new CacheSerializableRunnable("Validate dynamic region destruction on server") {

            public void run2() throws CacheException {
                Region r = getRootRegion(name);
                assertNotNull(r);
                String drName = r.getFullPath() + Region.SEPARATOR + dynFromClientName;
                assertNull(getCache().getRegion(drName));
                try {
                    DynamicRegionFactory.get().destroyDynamicRegion(drName);
                    fail("expected RegionDestroyedException");
                } catch (RegionDestroyedException expected) {
                }
            }
        };
        srv1.invoke(valNoDR);
        srv2.invoke(valNoDR);
        // Now try the reverse, create a dynamic region on the server and see if the client
        // has it
        srv2.invoke(new CacheSerializableRunnable("Server dynamic region creation") {

            public void run2() throws CacheException {
                Region r = getRootRegion(name);
                assertNotNull(r);
                Region dr = DynamicRegionFactory.get().createDynamicRegion(name, dynFromServerName);
                assertNull(dr.get(k1));
                dr.put(k1, v1Dynamic);
                assertEquals(v1Dynamic, dr.getEntry(k1).getValue());
            }
        });
        // Assert the servers have the dynamic region and the new value
        srv1.invoke(new CacheSerializableRunnable("Validate dynamic region creation propagation to other server") {

            public void run2() throws CacheException {
                Region r = getRootRegion(name);
                assertNotNull(r);
                Region dr = waitForSubRegion(r, dynFromServerName);
                assertNotNull(dr);
                assertNotNull(getCache().getRegion(name + Region.SEPARATOR + dynFromServerName));
                waitForEntry(dr, k1);
                assertNotNull(dr.getEntry(k1));
                assertEquals(v1Dynamic, dr.getEntry(k1).getValue());
            }
        });
        // Assert the clients have the dynamic region and the new value
        client1.invoke(new CacheSerializableRunnable("Validate dynamic region creation on client") {

            public void run2() throws CacheException {
                Region r = getRootRegion(name);
                assertNotNull(r);
                long end = System.currentTimeMillis() + 10000;
                Region dr = null;
                for (; ; ) {
                    try {
                        dr = r.getSubregion(dynFromServerName);
                        assertNotNull(dr);
                        assertNotNull(getCache().getRegion(name + Region.SEPARATOR + dynFromServerName));
                        break;
                    } catch (AssertionError e) {
                        if (System.currentTimeMillis() > end) {
                            throw e;
                        } else {
                            Wait.pause(1000);
                        }
                    }
                }
                waitForEntry(dr, k1);
                assertNotNull(dr.getEntry(k1));
                assertEquals(v1Dynamic, dr.getEntry(k1).getValue());
            }
        });
        // now delete the dynamic region on a server and see if it goes away on client
        srv2.invoke(new CacheSerializableRunnable("Server dynamic region destruction") {

            public void run2() throws CacheException {
                assertTrue(DynamicRegionFactory.get().isActive());
                Region r = getRootRegion(name);
                assertNotNull(r);
                String drName = r.getFullPath() + Region.SEPARATOR + dynFromServerName;
                assertNotNull(getCache().getRegion(drName));
                DynamicRegionFactory.get().destroyDynamicRegion(drName);
                assertNull(getCache().getRegion(drName));
            }
        });
        srv1.invoke(new CacheSerializableRunnable("Validate dynamic region destruction on other server") {

            public void run2() throws CacheException {
                Region r = getRootRegion(name);
                assertNotNull(r);
                String drName = r.getFullPath() + Region.SEPARATOR + dynFromServerName;
                {
                    int retry = 100;
                    while (retry-- > 0 && getCache().getRegion(drName) != null) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException ignore) {
                            fail("interrupted");
                        }
                    }
                }
                assertNull(getCache().getRegion(drName));
            }
        });
        // Assert the clients no longer have the dynamic region
        client1.invoke(new CacheSerializableRunnable("Validate dynamic region destruction on client") {

            public void run2() throws CacheException {
                Region r = getRootRegion(name);
                assertNotNull(r);
                String drName = r.getFullPath() + Region.SEPARATOR + dynFromServerName;
                {
                    int retry = 100;
                    while (retry-- > 0 && getCache().getRegion(drName) != null) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException ignore) {
                            fail("interrupted");
                        }
                    }
                }
                assertNull(getCache().getRegion(drName));
                // region,dynamicRegionList in DynamicRegionFactory // ?
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException ignore) {
                    fail("interrupted");
                }
                try {
                    DynamicRegionFactory.get().destroyDynamicRegion(drName);
                    fail("expected RegionDestroyedException");
                } catch (RegionDestroyedException expected) {
                }
            }
        });
    } finally {
        // clean-up loner
        client1.invoke(() -> disconnectFromDS());
        srv1.invoke(() -> disconnectFromDS());
        srv2.invoke(() -> disconnectFromDS());
    }
}
Also used : CacheClientNotifier(org.apache.geode.internal.cache.tier.sockets.CacheClientNotifier) CertifiableTestCacheListener(org.apache.geode.cache30.CertifiableTestCacheListener) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl) Endpoint(org.apache.geode.cache.client.internal.Endpoint) CacheClientNotifierStats(org.apache.geode.internal.cache.tier.sockets.CacheClientNotifierStats) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Pool(org.apache.geode.cache.client.Pool) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 17 with CertifiableTestCacheListener

use of org.apache.geode.cache30.CertifiableTestCacheListener in project geode by apache.

the class ConnectionPoolDUnitTest method test017ExpireDestroyHasEntryInCallback.

/**
   * Tests interest key registration.
   */
@Test
public void test017ExpireDestroyHasEntryInCallback() throws CacheException {
    disconnectAllFromDS();
    final String name = this.getName();
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    // Create cache server
    vm0.invoke(new CacheSerializableRunnable("Create Cache Server") {

        public void run2() throws CacheException {
            // In lieu of System.setProperty("gemfire.EXPIRE_SENDS_ENTRY_AS_CALLBACK", "true");
            EntryExpiryTask.expireSendsEntryAsCallback = true;
            AttributesFactory factory = getBridgeServerRegionAttributes(null, null);
            factory.setEntryTimeToLive(new ExpirationAttributes(1, ExpirationAction.DESTROY));
            createRegion(name, factory.create());
            try {
                startBridgeServer(0);
            } catch (Exception ex) {
                org.apache.geode.test.dunit.Assert.fail("While starting CacheServer", ex);
            }
        }
    });
    // Create cache server clients
    final int port = vm0.invoke(() -> ConnectionPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    SerializableRunnable createClient = new CacheSerializableRunnable("Create region") {

        public void run2() throws CacheException {
            getLonerSystem();
            getCache();
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            factory.setDataPolicy(DataPolicy.EMPTY);
            factory.setSubscriptionAttributes(new SubscriptionAttributes((InterestPolicy.ALL)));
            factory.setConcurrencyChecksEnabled(false);
            // create bridge writer
            ClientServerTestCase.configureConnectionPool(factory, host0, port, -1, true, -1, -1, null);
            CertifiableTestCacheListener l = new CertifiableTestCacheListener(org.apache.geode.test.dunit.LogWriterUtils.getLogWriter());
            factory.setCacheListener(l);
            Region r = createRegion(name, factory.create());
            r.registerInterest("ALL_KEYS");
        }
    };
    vm1.invoke(createClient);
    vm1.invoke(new CacheSerializableRunnable("Turn on history") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            CertifiableTestCacheListener ctl = (CertifiableTestCacheListener) region.getAttributes().getCacheListener();
            ctl.enableEventHistory();
        }
    });
    Wait.pause(500);
    // Create some entries on the client
    vm1.invoke(new CacheSerializableRunnable("Create entries") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i < 5; i++) {
                region.put("key-client-" + i, "value-client-" + i);
            }
        }
    });
    // Create some entries on the server
    vm0.invoke(new CacheSerializableRunnable("Create entries") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i < 5; i++) {
                region.put("key-server-" + i, "value-server-" + i);
            }
        }
    });
    // Wait for expiration
    Wait.pause(2000);
    vm1.invoke(new CacheSerializableRunnable("Validate listener events") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            CertifiableTestCacheListener ctl = (CertifiableTestCacheListener) region.getAttributes().getCacheListener();
            int destroyCallbacks = 0;
            List<CacheEvent> l = ctl.getEventHistory();
            for (CacheEvent ce : l) {
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("--->>> " + ce);
                if (ce.getOperation() == Operation.DESTROY && ce.getCallbackArgument() instanceof String) {
                    destroyCallbacks++;
                }
            }
            assertEquals(10, destroyCallbacks);
        }
    });
    // Close cache server clients
    SerializableRunnable close = new CacheSerializableRunnable("Close Pool") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.localDestroyRegion();
        }
    };
    vm1.invoke(close);
    // Stop cache server
    vm0.invoke(new SerializableRunnable("Stop CacheServer") {

        public void run() {
            stopBridgeServer(getCache());
        }
    });
}
Also used : CertifiableTestCacheListener(org.apache.geode.cache30.CertifiableTestCacheListener) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) Host(org.apache.geode.test.dunit.Host) NoAvailableServersException(org.apache.geode.cache.client.NoAvailableServersException) CancelException(org.apache.geode.CancelException) IOException(java.io.IOException) Endpoint(org.apache.geode.cache.client.internal.Endpoint) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 18 with CertifiableTestCacheListener

use of org.apache.geode.cache30.CertifiableTestCacheListener in project geode by apache.

the class ConnectionPoolDUnitTest method test015InvalidateAndDestroyToEmptyAllPropagation.

/**
   * Tests that invalidates and destroys are propagated to {@link Pool}s correctly to
   * DataPolicy.EMPTY + InterestPolicy.ALL
   *
   * @since GemFire 5.0
   */
@Test
public void test015InvalidateAndDestroyToEmptyAllPropagation() throws CacheException {
    final String name = this.getName();
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    vm0.invoke(new CacheSerializableRunnable("Create Cache Server") {

        public void run2() throws CacheException {
            AttributesFactory factory = getBridgeServerRegionAttributes(null, null);
            createRegion(name, factory.create());
            // pause(1000);
            try {
                startBridgeServer(0);
            } catch (Exception ex) {
                org.apache.geode.test.dunit.Assert.fail("While starting CacheServer", ex);
            }
        }
    });
    final int port = vm0.invoke(() -> ConnectionPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    SerializableRunnable createEmpty = new CacheSerializableRunnable("Create region") {

        public void run2() throws CacheException {
            getLonerSystem();
            getCache();
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            factory.setConcurrencyChecksEnabled(false);
            ClientServerTestCase.configureConnectionPool(factory, host0, port, -1, true, -1, -1, null);
            CertifiableTestCacheListener l = new CertifiableTestCacheListener(org.apache.geode.test.dunit.LogWriterUtils.getLogWriter());
            factory.setCacheListener(l);
            factory.setDataPolicy(DataPolicy.EMPTY);
            factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
            Region rgn = createRegion(name, factory.create());
            rgn.registerInterestRegex(".*", false, false);
        }
    };
    SerializableRunnable createNormal = new CacheSerializableRunnable("Create region") {

        public void run2() throws CacheException {
            getLonerSystem();
            getCache();
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            factory.setConcurrencyChecksEnabled(false);
            ClientServerTestCase.configureConnectionPool(factory, host0, port, -1, true, -1, -1, null);
            CertifiableTestCacheListener l = new CertifiableTestCacheListener(org.apache.geode.test.dunit.LogWriterUtils.getLogWriter());
            factory.setCacheListener(l);
            Region rgn = createRegion(name, factory.create());
            rgn.registerInterestRegex(".*", false, false);
        }
    };
    vm1.invoke(createEmpty);
    vm1.invoke(new CacheSerializableRunnable("Populate region") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i < 10; i++) {
                region.put(new Integer(i), "old" + i);
            }
        }
    });
    vm2.invoke(createNormal);
    vm1.invoke(new CacheSerializableRunnable("Turn on history") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            CertifiableTestCacheListener ctl = (CertifiableTestCacheListener) region.getAttributes().getCacheListener();
            ctl.enableEventHistory();
        }
    });
    vm2.invoke(new CacheSerializableRunnable("Update region") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i < 10; i++) {
                region.put(new Integer(i), "new" + i, "callbackArg" + i);
            }
        }
    });
    Wait.pause(5 * 1000);
    vm1.invoke(new CacheSerializableRunnable("Verify invalidates") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            CertifiableTestCacheListener ctl = (CertifiableTestCacheListener) region.getAttributes().getCacheListener();
            for (int i = 0; i < 10; i++) {
                Object key = new Integer(i);
                ctl.waitForInvalidated(key);
                Region.Entry entry = region.getEntry(key);
                // we are empty!
                assertNull(entry);
            }
            {
                List l = ctl.getEventHistory();
                assertEquals(10, l.size());
                for (int i = 0; i < 10; i++) {
                    Object key = new Integer(i);
                    EntryEvent ee = (EntryEvent) l.get(i);
                    assertEquals(key, ee.getKey());
                    assertEquals(null, ee.getOldValue());
                    // failure
                    assertEquals(false, ee.isOldValueAvailable());
                    assertEquals(Operation.INVALIDATE, ee.getOperation());
                    assertEquals("callbackArg" + i, ee.getCallbackArgument());
                    assertEquals(true, ee.isOriginRemote());
                }
            }
        }
    });
    vm2.invoke(new CacheSerializableRunnable("Validate original and destroy") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i < 10; i++) {
                Object key = new Integer(i);
                assertEquals("new" + i, region.getEntry(key).getValue());
                region.destroy(key, "destroyCB" + i);
            }
        }
    });
    Wait.pause(5 * 1000);
    vm1.invoke(new CacheSerializableRunnable("Verify destroys") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            CertifiableTestCacheListener ctl = (CertifiableTestCacheListener) region.getAttributes().getCacheListener();
            for (int i = 0; i < 10; i++) {
                Object key = new Integer(i);
                ctl.waitForDestroyed(key);
                Region.Entry entry = region.getEntry(key);
                assertNull(entry);
            }
            {
                List l = ctl.getEventHistory();
                assertEquals(10, l.size());
                for (int i = 0; i < 10; i++) {
                    Object key = new Integer(i);
                    EntryEvent ee = (EntryEvent) l.get(i);
                    assertEquals(key, ee.getKey());
                    assertEquals(null, ee.getOldValue());
                    assertEquals(false, ee.isOldValueAvailable());
                    assertEquals(Operation.DESTROY, ee.getOperation());
                    assertEquals("destroyCB" + i, ee.getCallbackArgument());
                    assertEquals(true, ee.isOriginRemote());
                }
            }
        }
    });
    vm2.invoke(new CacheSerializableRunnable("recreate") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i < 10; i++) {
                Object key = new Integer(i);
                region.create(key, "create" + i, "createCB" + i);
            }
        }
    });
    Wait.pause(5 * 1000);
    vm1.invoke(new CacheSerializableRunnable("Verify creates") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            CertifiableTestCacheListener ctl = (CertifiableTestCacheListener) region.getAttributes().getCacheListener();
            for (int i = 0; i < 10; i++) {
                Object key = new Integer(i);
                ctl.waitForInvalidated(key);
                Region.Entry entry = region.getEntry(key);
                assertNull(entry);
            }
            List l = ctl.getEventHistory();
            assertEquals(10, l.size());
            for (int i = 0; i < 10; i++) {
                Object key = new Integer(i);
                EntryEvent ee = (EntryEvent) l.get(i);
                assertEquals(key, ee.getKey());
                assertEquals(null, ee.getOldValue());
                assertEquals(false, ee.isOldValueAvailable());
                assertEquals(Operation.INVALIDATE, ee.getOperation());
                assertEquals("createCB" + i, ee.getCallbackArgument());
                assertEquals(true, ee.isOriginRemote());
            }
            // now see if we can get it from the server
            for (int i = 0; i < 10; i++) {
                Object key = new Integer(i);
                assertEquals("create" + i, region.get(key, "loadCB" + i));
            }
            l = ctl.getEventHistory();
            assertEquals(10, l.size());
            for (int i = 0; i < 10; i++) {
                Object key = new Integer(i);
                EntryEvent ee = (EntryEvent) l.get(i);
                assertEquals(key, ee.getKey());
                assertEquals(null, ee.getOldValue());
                assertEquals("create" + i, ee.getNewValue());
                assertEquals(Operation.LOCAL_LOAD_CREATE, ee.getOperation());
                assertEquals("loadCB" + i, ee.getCallbackArgument());
                assertEquals(false, ee.isOriginRemote());
            }
        }
    });
    SerializableRunnable close = new CacheSerializableRunnable("Close Pool") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.localDestroyRegion();
        }
    };
    vm1.invoke(close);
    vm2.invoke(close);
    vm0.invoke(new SerializableRunnable("Stop CacheServer") {

        public void run() {
            stopBridgeServer(getCache());
        }
    });
}
Also used : CertifiableTestCacheListener(org.apache.geode.cache30.CertifiableTestCacheListener) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) Host(org.apache.geode.test.dunit.Host) NoAvailableServersException(org.apache.geode.cache.client.NoAvailableServersException) CancelException(org.apache.geode.CancelException) IOException(java.io.IOException) Endpoint(org.apache.geode.cache.client.internal.Endpoint) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 19 with CertifiableTestCacheListener

use of org.apache.geode.cache30.CertifiableTestCacheListener in project geode by apache.

the class CqQueryUsingPoolDUnitTest method testInterestListAndCQs.

/**
   * Test for InterestList and CQ registered from same clients.
   */
@Test
public void testInterestListAndCQs() throws Exception {
    final Host host = Host.getHost(0);
    VM server = host.getVM(0);
    VM client = host.getVM(1);
    /* Init Server and Client */
    createServer(server);
    final int thePort = server.invoke(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server.getHost());
    String poolName = "testInterestListAndCQs";
    createPool(client, poolName, host0, thePort);
    createClient(client, thePort, host0);
    /* Create CQs. */
    createCQ(client, poolName, "testInterestListAndCQs_0", cqs[0]);
    validateCQCount(client, 1);
    /* Init values at server. */
    final int size = 10;
    executeCQ(client, "testInterestListAndCQs_0", false, null);
    registerInterestListCQ(client, regions[0], size, false);
    createValues(server, regions[0], size);
    for (int i = 1; i <= 10; i++) {
        waitForCreated(client, "testInterestListAndCQs_0", KEY + i);
    }
    Wait.pause(5 * 1000);
    // validate CQs.
    validateCQ(client, "testInterestListAndCQs_0", /* resultSize: */
    noTest, /* creates: */
    size, /* updates: */
    noTest, /* deletes; */
    noTest, /* queryInserts: */
    size, /* queryUpdates: */
    0, /* queryDeletes: */
    0, /* totalEvents: */
    size);
    // Validate InterestList.
    // CREATE
    client.invoke(new CacheSerializableRunnable("validate updates") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(regions[0]);
            assertNotNull(region);
            // TODO does this WaitCriterion actually help?
            WaitCriterion wc = new WaitCriterion() {

                String excuse;

                @Override
                public boolean done() {
                    int sz = region.entrySet().size();
                    if (sz == size) {
                        return true;
                    }
                    excuse = "Mismatch, number of keys (" + sz + ") in local region is not equal to the interest list size (" + size + ")";
                    return false;
                }

                @Override
                public String description() {
                    return excuse;
                }
            };
            Wait.waitForCriterion(wc, 60 * 1000, 1000, true);
            CertifiableTestCacheListener ctl = (CertifiableTestCacheListener) region.getAttributes().getCacheListener();
            for (int i = 1; i <= 10; i++) {
                ctl.waitForCreated(KEY + i);
                assertNotNull(region.getEntry(KEY + i));
            }
        }
    });
    // UPDATE
    createValues(server, regions[0], size);
    // Wait for client to Synch.
    for (int i = 1; i <= 10; i++) {
        waitForUpdated(client, "testInterestListAndCQs_0", KEY + i);
    }
    client.invoke(new CacheSerializableRunnable("validate updates") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regions[0]);
            assertNotNull(region);
            Set keys = region.entrySet();
            assertEquals("Mismatch, number of keys in local region is not equal to the interest list size", size, keys.size());
            CertifiableTestCacheListener ctl = (CertifiableTestCacheListener) region.getAttributes().getCacheListener();
            for (int i = 1; i <= 10; i++) {
                ctl.waitForUpdated(KEY + i);
                assertNotNull(region.getEntry(KEY + i));
            }
        }
    });
    // INVALIDATE
    server.invoke(new CacheSerializableRunnable("Invalidate values") {

        @Override
        public void run2() throws CacheException {
            Region region1 = getRootRegion().getSubregion(regions[0]);
            for (int i = 1; i <= size; i++) {
                region1.invalidate(KEY + i);
            }
        }
    });
    waitForInvalidated(client, "testInterestListAndCQs_0", KEY + 10);
    client.invoke(new CacheSerializableRunnable("validate invalidates") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regions[0]);
            assertNotNull(region);
            Set keys = region.entrySet();
            assertEquals("Mismatch, number of keys in local region is not equal to the interest list size", size, keys.size());
            CertifiableTestCacheListener ctl = (CertifiableTestCacheListener) region.getAttributes().getCacheListener();
            for (int i = 1; i <= 10; i++) {
                ctl.waitForInvalidated(KEY + i);
                assertNotNull(region.getEntry(KEY + i));
            }
        }
    });
    validateCQ(client, "testInterestListAndCQs_0", /* resultSize: */
    noTest, /* creates: */
    size, /* updates: */
    size, /* deletes; */
    noTest, /* queryInserts: */
    size, /* queryUpdates: */
    size, /* queryDeletes: */
    size, /* totalEvents: */
    size * 3);
    // DESTROY - this should not have any effect on CQ, as the events are
    // already destroyed from invalidate events.
    server.invoke(new CacheSerializableRunnable("Invalidate values") {

        @Override
        public void run2() throws CacheException {
            Region region1 = getRootRegion().getSubregion(regions[0]);
            for (int i = 1; i <= size; i++) {
                region1.destroy(KEY + i);
            }
        }
    });
    // Wait for destroyed.
    client.invoke(new CacheSerializableRunnable("validate destroys") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regions[0]);
            assertNotNull(region);
            CertifiableTestCacheListener ctl = (CertifiableTestCacheListener) region.getAttributes().getCacheListener();
            for (int i = 1; i <= 10; i++) {
                ctl.waitForDestroyed(KEY + i);
            }
        }
    });
    validateCQ(client, "testInterestListAndCQs_0", /* resultSize: */
    noTest, /* creates: */
    size, /* updates: */
    size, /* deletes; */
    noTest, /* queryInserts: */
    size, /* queryUpdates: */
    size, /* queryDeletes: */
    size, /* totalEvents: */
    size * 3);
    closeClient(client);
    closeServer(server);
}
Also used : WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) Set(java.util.Set) HashSet(java.util.HashSet) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) CertifiableTestCacheListener(org.apache.geode.cache30.CertifiableTestCacheListener) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 20 with CertifiableTestCacheListener

use of org.apache.geode.cache30.CertifiableTestCacheListener in project geode by apache.

the class CqQueryDUnitTest method registerInterestListCQ.

/* Register CQs */
private void registerInterestListCQ(VM vm, final String regionName, final int keySize) {
    vm.invoke(new CacheSerializableRunnable("Register InterestList and CQ") {

        public void run2() throws CacheException {
            // Get CQ Service.
            Region region = null;
            try {
                region = getRootRegion().getSubregion(regionName);
                region.getAttributesMutator().setCacheListener(new CertifiableTestCacheListener(LogWriterUtils.getLogWriter()));
            } catch (Exception cqe) {
                AssertionError err = new AssertionError("Failed to get Region.");
                err.initCause(cqe);
                throw err;
            }
            try {
                List list = new ArrayList();
                for (int i = 1; i <= keySize; i++) {
                    list.add(KEY + i);
                }
                region.registerInterest(list);
            } catch (Exception ex) {
                AssertionError err = new AssertionError("Failed to Register InterestList");
                err.initCause(ex);
                throw err;
            }
        }
    });
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) CertifiableTestCacheListener(org.apache.geode.cache30.CertifiableTestCacheListener) ArrayList(java.util.ArrayList) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) Region(org.apache.geode.cache.Region) List(java.util.List) ArrayList(java.util.ArrayList) CqExistsException(org.apache.geode.cache.query.CqExistsException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqClosedException(org.apache.geode.cache.query.CqClosedException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException)

Aggregations

CertifiableTestCacheListener (org.apache.geode.cache30.CertifiableTestCacheListener)23 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)19 Region (org.apache.geode.cache.Region)14 VM (org.apache.geode.test.dunit.VM)14 Host (org.apache.geode.test.dunit.Host)12 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)12 Test (org.junit.Test)12 IOException (java.io.IOException)11 CacheException (org.apache.geode.cache.CacheException)11 ArrayList (java.util.ArrayList)9 List (java.util.List)9 LocalRegion (org.apache.geode.internal.cache.LocalRegion)9 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)9 CancelException (org.apache.geode.CancelException)6 Endpoint (org.apache.geode.cache.client.internal.Endpoint)6 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)6 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)6 NoAvailableServersException (org.apache.geode.cache.client.NoAvailableServersException)5 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)5 LinkedList (java.util.LinkedList)4