Search in sources :

Example 46 with CacheListenerAdapter

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

the class TXOrderDUnitTest method testFarSideOpForLoad.

/**
   * Tests fix for #40870 Remote CacheListeners invoke afterCreate with Operation.LOCAL_LOAD_CREATE
   * when create executed transactionally"
   */
@Ignore("TODO: test is disabled")
@Test
public void testFarSideOpForLoad() throws Exception {
    Host host = Host.getHost(0);
    VM vm1 = host.getVM(0);
    VM vm2 = host.getVM(1);
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            AttributesFactory af = new AttributesFactory();
            af.setDataPolicy(DataPolicy.REPLICATE);
            af.setScope(Scope.DISTRIBUTED_ACK);
            CacheListener cl1 = new CacheListenerAdapter() {

                public void afterCreate(EntryEvent e) {
                    assertTrue(e.getOperation().isLocalLoad());
                }
            };
            af.addCacheListener(cl1);
            CacheLoader cl = new CacheLoader() {

                public Object load(LoaderHelper helper) throws CacheLoaderException {
                    LogWriterUtils.getLogWriter().info("Loading value:" + helper.getKey() + "_value");
                    return helper.getKey() + "_value";
                }

                public void close() {
                }
            };
            af.setCacheLoader(cl);
            createRootRegion("r1", af.create());
            return null;
        }
    });
    vm2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            AttributesFactory af = new AttributesFactory();
            af.setDataPolicy(DataPolicy.REPLICATE);
            af.setScope(Scope.DISTRIBUTED_ACK);
            CacheListener cl1 = new CacheListenerAdapter() {

                public void afterCreate(EntryEvent e) {
                    LogWriterUtils.getLogWriter().info("op:" + e.getOperation().toString());
                    assertTrue(!e.getOperation().isLocalLoad());
                }
            };
            af.addCacheListener(cl1);
            createRootRegion("r1", af.create());
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region r = getRootRegion("r1");
            getCache().getCacheTransactionManager().begin();
            r.get("obj_2");
            getCache().getCacheTransactionManager().commit();
            return null;
        }
    });
}
Also used : Host(org.apache.geode.test.dunit.Host) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheException(org.apache.geode.cache.CacheException) CacheListener(org.apache.geode.cache.CacheListener) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) Ignore(org.junit.Ignore) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 47 with CacheListenerAdapter

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

the class TXOrderDUnitTest method testFarSideOrder.

/**
   * make sure listeners get invoked in correct order on far side of tx
   */
@Test
public void testFarSideOrder() throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(DataPolicy.REPLICATE);
    af.setScope(Scope.DISTRIBUTED_ACK);
    CacheListener cl1 = new CacheListenerAdapter() {

        public void afterCreate(EntryEvent e) {
            assertEquals(getCurrentExpectedKey(), e.getKey());
        }
    };
    af.addCacheListener(cl1);
    Region r1 = createRootRegion("r1", af.create());
    Region r2 = r1.createSubregion("r2", af.create());
    r2.createSubregion("r3", af.create());
    TransactionListener tl1 = new TransactionListenerAdapter() {

        public void afterCommit(TransactionEvent e) {
            assertEquals(6, e.getEvents().size());
            ArrayList keys = new ArrayList();
            Iterator it = e.getEvents().iterator();
            while (it.hasNext()) {
                EntryEvent ee = (EntryEvent) it.next();
                keys.add(ee.getKey());
                assertEquals(null, ee.getCallbackArgument());
                assertEquals(true, ee.isCallbackArgumentAvailable());
            }
            assertEquals(TXOrderDUnitTest.this.expectedKeys, keys);
            TXOrderDUnitTest.this.invokeCount = 1;
        }
    };
    CacheTransactionManager ctm = getCache().getCacheTransactionManager();
    ctm.addListener(tl1);
    this.invokeCount = 0;
    this.clCount = 0;
    this.expectedKeys = Arrays.asList(new String[] { "b", "c", "a", "a2", "c2", "b2" });
    doCommitOtherVm();
    assertEquals(1, this.invokeCount);
    assertEquals(6, this.clCount);
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter) TransactionEvent(org.apache.geode.cache.TransactionEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) CacheListener(org.apache.geode.cache.CacheListener) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 48 with CacheListenerAdapter

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

the class MultiVMRegionTestCase method testOrderedUpdates.

/**
   * Tests that distributed updates are delivered in order
   *
   * <P>
   *
   * Note that this test does not make sense for regions that are {@link Scope#DISTRIBUTED_NO_ACK}
   * for which we do not guarantee the ordering of updates for a single producer/single consumer.
   *
   * DISABLED 4-16-04 - the current implementation assumes events are processed synchronously, which
   * is no longer true.
   */
@Ignore("TODO: test is DISABLED 4-16-04 - the current implementation assumes events are processed synchronously, which is no longer true")
@Test
public void testOrderedUpdates() throws Exception {
    assumeFalse(getRegionAttributes().getScope() == Scope.DISTRIBUTED_NO_ACK);
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final int lastNumber = 10;
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    SerializableRunnable create = new CacheSerializableRunnable("Create region entry") {

        @Override
        public void run2() throws CacheException {
            Region region = createRegion(name);
            region.create(key, null);
        }
    };
    vm0.invoke(create);
    vm1.invoke(create);
    vm1.invoke(new CacheSerializableRunnable("Set listener") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.setUserAttribute(new LinkedBlockingQueue());
            region.getAttributesMutator().addCacheListener(new CacheListenerAdapter() {

                @Override
                public void afterUpdate(EntryEvent e) {
                    Region region2 = e.getRegion();
                    LinkedBlockingQueue queue = (LinkedBlockingQueue) region2.getUserAttribute();
                    Object value = e.getNewValue();
                    assertNotNull(value);
                    try {
                        org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("++ Adding " + value);
                        queue.put(value);
                    } catch (InterruptedException ex) {
                        fail("Why was I interrupted?", ex);
                    }
                }
            });
            flushIfNecessary(region);
        }
    });
    AsyncInvocation ai1 = vm1.invokeAsync(new CacheSerializableRunnable("Verify") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            LinkedBlockingQueue queue = (LinkedBlockingQueue) region.getUserAttribute();
            for (int i = 0; i <= lastNumber; i++) {
                try {
                    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("++ Waiting for " + i);
                    Integer value = (Integer) queue.take();
                    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("++ Got " + value);
                    assertEquals(i, value.intValue());
                } catch (InterruptedException ex) {
                    fail("Why was I interrupted?", ex);
                }
            }
        }
    });
    AsyncInvocation ai0 = vm0.invokeAsync(new CacheSerializableRunnable("Populate") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i <= lastNumber; i++) {
                // org.apache.geode.internal.GemFireVersion.waitForJavaDebugger(getLogWriter());
                region.put(key, new Integer(i));
            }
        }
    });
    ThreadUtils.join(ai0, 30 * 1000);
    ThreadUtils.join(ai1, 30 * 1000);
    if (ai0.exceptionOccurred()) {
        fail("ai0 failed", ai0.getException());
    } else if (ai1.exceptionOccurred()) {
        fail("ai1 failed", ai1.getException());
    }
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) StoredObject(org.apache.geode.internal.offheap.StoredObject) Ignore(org.junit.Ignore) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 49 with CacheListenerAdapter

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

the class PostProcessorDUnitTest method testRegisterInterestPostProcess.

@Test
public void testRegisterInterestPostProcess() {
    client1.invoke(() -> {
        ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
        ClientRegionFactory factory = cache.createClientRegionFactory(ClientRegionShortcut.PROXY);
        factory.addCacheListener(new CacheListenerAdapter() {

            @Override
            public void afterUpdate(EntryEvent event) {
                assertEquals("super-user/AuthRegion/key1/value2", event.getSerializedNewValue().getDeserializedValue());
            }
        });
        Region region = factory.create(REGION_NAME);
        region.put("key1", "value1");
        region.registerInterest("key1");
    });
    client2.invoke(() -> {
        ClientCache cache = createClientCache("dataUser", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        region.put("key1", "value2");
    });
}
Also used : CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) ClientRegionFactory(org.apache.geode.cache.client.ClientRegionFactory) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 50 with CacheListenerAdapter

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

the class PDXPostProcessorDUnitTest method testRegisterInterest.

@Test
public void testRegisterInterest() {
    IgnoredException.addIgnoredException("NoAvailableServersException");
    client1.invoke(() -> {
        ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
        ClientRegionFactory factory = cache.createClientRegionFactory(ClientRegionShortcut.PROXY);
        factory.addCacheListener(new CacheListenerAdapter() {

            @Override
            public void afterUpdate(EntryEvent event) {
                Object key = event.getKey();
                Object value = ((EntryEventImpl) event).getDeserializedValue();
                if (key.equals("key1")) {
                    assertTrue(value instanceof SimpleClass);
                } else if (key.equals("key2")) {
                    assertTrue(Arrays.equals(BYTES, (byte[]) value));
                }
            }
        });
        Region region = factory.create(REGION_NAME);
        region.put("key1", "value1");
        region.registerInterest("key1");
        region.registerInterest("key2");
    });
    client2.invoke(() -> {
        ClientCache cache = createClientCache("dataUser", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        // put in a value that's a domain object
        region.put("key1", new SimpleClass(1, (byte) 1));
        region.put("key2", BYTES);
    });
    // wait for events to fire
    Awaitility.await().atMost(1, TimeUnit.SECONDS);
    PDXPostProcessor pp = (PDXPostProcessor) SecurityService.getSecurityService().getPostProcessor();
    assertEquals(pp.getCount(), 2);
}
Also used : CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) ClientRegionFactory(org.apache.geode.cache.client.ClientRegionFactory) SimpleClass(org.apache.geode.pdx.SimpleClass) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

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