Search in sources :

Example 41 with CacheListenerAdapter

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

the class Bug34387DUnitTest method testCreateAndLI.

/**
   * test create followed by localInvalidate
   */
@Test
public void testCreateAndLI() throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(DataPolicy.REPLICATE);
    af.setScope(Scope.DISTRIBUTED_ACK);
    af.setConcurrencyChecksEnabled(true);
    callbackFailure = false;
    CacheListener cl1 = new CacheListenerAdapter() {

        public void afterCreate(EntryEvent e) {
            callbackAssertEquals("key not equal", "createKey", e.getKey());
            callbackAssertEquals("value not equal", "createValue", e.getNewValue());
            Bug34387DUnitTest.this.invokeCount++;
        }
    };
    af.addCacheListener(cl1);
    Region r1 = createRootRegion("r1", af.create());
    this.invokeCount = 0;
    assertNull(r1.getEntry("createKey"));
    doCommitOtherVm(false);
    assertNotNull(r1.getEntry("createKey"));
    assertEquals("createValue", r1.getEntry("createKey").getValue());
    assertEquals(1, this.invokeCount);
    assertFalse("Errors in callbacks; check logs for details", callbackFailure);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) CacheListener(org.apache.geode.cache.CacheListener) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 42 with CacheListenerAdapter

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

the class Bug38013DUnitTest method doCreateOtherVm.

private void doCreateOtherVm() {
    VM vm = getOtherVm();
    vm.invoke(new CacheSerializableRunnable("create root") {

        public void run2() throws CacheException {
            getSystem();
            AttributesFactory af = new AttributesFactory();
            CacheListener cl = new CacheListenerAdapter() {

                public void afterCreate(EntryEvent event) {
                    // getLogWriter().info("afterCreate " + event.getKey());
                    if (event.getCallbackArgument() != null) {
                        lastCallback = event.getCallbackArgument();
                    }
                }

                public void afterUpdate(EntryEvent event) {
                    // getLogWriter().info("afterUpdate " + event.getKey());
                    if (event.getCallbackArgument() != null) {
                        lastCallback = event.getCallbackArgument();
                    }
                }

                public void afterInvalidate(EntryEvent event) {
                    if (event.getCallbackArgument() != null) {
                        lastCallback = event.getCallbackArgument();
                    }
                }

                public void afterDestroy(EntryEvent event) {
                    if (event.getCallbackArgument() != null) {
                        lastCallback = event.getCallbackArgument();
                    }
                }
            };
            af.setCacheListener(cl);
            // create a pr with a data store
            PartitionAttributesFactory paf = new PartitionAttributesFactory();
            paf.setRedundantCopies(0);
            // use defaults so this is a data store
            af.setPartitionAttributes(paf.create());
            createRootRegion("bug38013", af.create());
        }
    });
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) CacheListener(org.apache.geode.cache.CacheListener)

Example 43 with CacheListenerAdapter

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

the class ProxyJUnitTest method testExpiration.

/**
   * Make sure a proxy region expiration behaves as expected
   */
@Test
public void testExpiration() throws Exception {
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        // now make sure they don't on proxy
        {
            AttributesFactory af = new AttributesFactory();
            af.setStatisticsEnabled(true);
            af.setEntryIdleTimeout(new ExpirationAttributes(1, ExpirationAction.LOCAL_INVALIDATE));
            af.setEntryTimeToLive(new ExpirationAttributes(2, ExpirationAction.LOCAL_DESTROY));
            af.setDataPolicy(DataPolicy.EMPTY);
            try {
                af.create();
                fail("expected IllegalStateException");
            } catch (IllegalStateException expected) {
            }
        }
        // make sure regionIdleTimeout works on proxy
        {
            CacheListener cl1 = new CacheListenerAdapter() {

                public void afterRegionDestroy(RegionEvent e) {
                    clInvokeCount++;
                }

                public void afterRegionInvalidate(RegionEvent e) {
                    clInvokeCount++;
                }
            };
            AttributesFactory af = new AttributesFactory();
            af.setStatisticsEnabled(true);
            final int EXPIRE_MS = 500;
            af.setRegionIdleTimeout(new ExpirationAttributes(EXPIRE_MS, ExpirationAction.LOCAL_DESTROY));
            af.addCacheListener(cl1);
            af.setDataPolicy(DataPolicy.EMPTY);
            clearCallbackState();
            Region r = this.c.createRegion("rEMPTY", af.create());
            assertTrue(clInvokeCount == 0);
            r.put("key", "value");
            long endTime = System.currentTimeMillis() + (EXPIRE_MS * 2);
            do {
                r.get("key");
            } while (System.currentTimeMillis() < endTime);
            assertEquals(0, this.clInvokeCount);
            Thread.sleep(EXPIRE_MS * 2);
            boolean done = false;
            try {
                for (StopWatch time = new StopWatch(true); !done && time.elapsedTimeMillis() < 1000; done = (ProxyJUnitTest.this.clInvokeCount == 1)) {
                    Thread.sleep(200);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            assertTrue("waiting for invocation", done);
        }
        // make sure regionTimeToLive works on proxy
        {
            CacheListener cl1 = new CacheListenerAdapter() {

                public void afterRegionDestroy(RegionEvent e) {
                    clInvokeCount++;
                }

                public void afterRegionInvalidate(RegionEvent e) {
                    clInvokeCount++;
                }
            };
            AttributesFactory af = new AttributesFactory();
            af.setStatisticsEnabled(true);
            final int EXPIRE_MS = 500;
            af.setRegionTimeToLive(new ExpirationAttributes(EXPIRE_MS, ExpirationAction.LOCAL_DESTROY));
            af.addCacheListener(cl1);
            af.setDataPolicy(DataPolicy.EMPTY);
            clearCallbackState();
            Region r = this.c.createRegion("rEMPTY", af.create());
            assertTrue(clInvokeCount == 0);
            r.put("key", "value");
            long endTime = System.currentTimeMillis() + (EXPIRE_MS * 2);
            do {
                r.put("key", "value");
            } while (System.currentTimeMillis() < endTime);
            assertEquals(0, this.clInvokeCount);
            Thread.sleep(EXPIRE_MS * 2);
            boolean done = false;
            try {
                for (StopWatch time = new StopWatch(true); !done && time.elapsedTimeMillis() < 1000; done = (ProxyJUnitTest.this.clInvokeCount == 1)) {
                    Thread.sleep(200);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            assertTrue("waiting for invocation", done);
        }
    } finally {
        System.clearProperty(LocalRegion.EXPIRY_MS_PROPERTY);
        assertEquals(null, System.getProperty(LocalRegion.EXPIRY_MS_PROPERTY));
    }
}
Also used : CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) LocalRegion(org.apache.geode.internal.cache.LocalRegion) StopWatch(org.apache.geode.internal.util.StopWatch) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 44 with CacheListenerAdapter

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

the class Bug34387DUnitTest method testCreateAndLD.

////////////////////// Test Methods //////////////////////
/**
   * test create followed by localDestroy
   */
@Test
public void testCreateAndLD() throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(DataPolicy.REPLICATE);
    af.setScope(Scope.DISTRIBUTED_ACK);
    af.setConcurrencyChecksEnabled(true);
    callbackFailure = false;
    CacheListener cl1 = new CacheListenerAdapter() {

        public void afterCreate(EntryEvent e) {
            callbackAssertEquals("Keys not equal", "createKey", e.getKey());
            callbackAssertEquals("Values not equal", "createValue", e.getNewValue());
            Bug34387DUnitTest.this.invokeCount++;
        }
    };
    af.addCacheListener(cl1);
    Region r1 = createRootRegion("r1", af.create());
    this.invokeCount = 0;
    assertNull(r1.getEntry("createKey"));
    doCommitOtherVm(true);
    assertNotNull(r1.getEntry("createKey"));
    assertEquals("createValue", r1.getEntry("createKey").getValue());
    assertEquals(1, this.invokeCount);
    assertFalse("Errors in callbacks; check logs for details", callbackFailure);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) CacheListener(org.apache.geode.cache.CacheListener) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 45 with CacheListenerAdapter

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

the class SlowRecDUnitTest method doCreateOtherVm.

private void doCreateOtherVm(final Properties p, final boolean addListener) {
    VM vm = getOtherVm();
    vm.invoke(new CacheSerializableRunnable("create root") {

        public void run2() throws CacheException {
            getSystem(p);
            createAckRegion(true, false);
            AttributesFactory af = new AttributesFactory();
            af.setScope(Scope.DISTRIBUTED_NO_ACK);
            af.setDataPolicy(DataPolicy.REPLICATE);
            if (addListener) {
                CacheListener cl = new CacheListenerAdapter() {

                    public void afterUpdate(EntryEvent event) {
                        // make the slow receiver event slower!
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException shuttingDown) {
                            fail("interrupted");
                        }
                    }
                };
                af.setCacheListener(cl);
            } else {
                CacheListener cl = new CacheListenerAdapter() {

                    public void afterCreate(EntryEvent event) {
                        if (event.getCallbackArgument() != null) {
                            lastCallback = event.getCallbackArgument();
                        }
                        if (event.getKey().equals("sleepkey")) {
                            int sleepMs = ((Integer) event.getNewValue()).intValue();
                            try {
                                Thread.sleep(sleepMs);
                            } catch (InterruptedException ignore) {
                                fail("interrupted");
                            }
                        }
                    }

                    public void afterUpdate(EntryEvent event) {
                        if (event.getCallbackArgument() != null) {
                            lastCallback = event.getCallbackArgument();
                        }
                        if (event.getKey().equals("sleepkey")) {
                            int sleepMs = ((Integer) event.getNewValue()).intValue();
                            try {
                                Thread.sleep(sleepMs);
                            } catch (InterruptedException ignore) {
                                fail("interrupted");
                            }
                        }
                    }

                    public void afterInvalidate(EntryEvent event) {
                        if (event.getCallbackArgument() != null) {
                            lastCallback = event.getCallbackArgument();
                        }
                    }

                    public void afterDestroy(EntryEvent event) {
                        if (event.getCallbackArgument() != null) {
                            lastCallback = event.getCallbackArgument();
                        }
                    }
                };
                af.setCacheListener(cl);
            }
            Region r1 = createRootRegion("slowrec", af.create());
            // place holder so we receive updates
            r1.create("key", "value");
        }
    });
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) CacheListener(org.apache.geode.cache.CacheListener)

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