Search in sources :

Example 11 with CacheListenerAdapter

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

the class HARQAddOperationJUnitTest method testAddWithQRMAndExpiry.

/**
   * Add with QRM & expiry : Add 10 conflatable objects (0-9). Send QRM LastDispatched as 4.
   * Validate the sequence ID field of LastDispatchedWrapper object is updated to 4. Perform Take
   * operations to remove next 5 to 9. The seqeunceId field should be 9. Allow ThreadIdenitifier to
   * expire. The expiration should fail as the original sequenceId ( -1) does not match with 9.
   * Validate reset with value 9 . The next expiry should remove the LastDisptachedWrapper
   */
@Test
public void testAddWithQRMAndExpiry() throws Exception {
    try {
        HARegionQueueAttributes attrs = new HARegionQueueAttributes();
        attrs.setExpiryTime(10);
        final HARegionQueue regionqueue = new HARegionQueue.TestOnlyHARegionQueue("testing", cache, attrs) {

            CacheListener createCacheListenerForHARegion() {
                return new CacheListenerAdapter() {

                    public void afterInvalidate(EntryEvent event) {
                        try {
                            expireTheEventOrThreadIdentifier(event);
                        } catch (CacheException ce) {
                            logger.error("HAREgionQueue::createCacheListener::Exception in the expiry thread", ce);
                        }
                        if (event.getKey() instanceof ThreadIdentifier) {
                            synchronized (HARQAddOperationJUnitTest.this) {
                                expiryCount++;
                                HARQAddOperationJUnitTest.this.notify();
                            }
                        }
                    }
                };
            }
        };
        Conflatable[] cf = new Conflatable[10];
        // put 10 conflatable objects
        for (int i = 0; i < 10; i++) {
            cf[i] = new ConflatableObject("key" + i, "value", new EventID(new byte[] { 1 }, 1, i), true, "testing");
            regionqueue.put(cf[i]);
        }
        ThreadIdentifier tID = new ThreadIdentifier(new byte[] { 1 }, 1);
        // verify that the sequence-id for Thread-identifier is -1 (default value).
        assertEquals(new Long(-1), regionqueue.getRegion().get(tID));
        // remove the first 5 - (0-4 sequence IDs)
        regionqueue.removeDispatchedEvents(new EventID(new byte[] { 1 }, 1, 4));
        // verify that the last dispatched event was of sequence id 4
        assertEquals(4, regionqueue.getLastDispatchedSequenceId(new EventID(new byte[] { 1 }, 1, 1)));
        // verify 1-5 not in region
        for (long i = 1; i < 6; i++) {
            assertTrue(!regionqueue.getRegion().containsKey(new Long(i)));
        }
        // verify 6-10 still in region queue
        for (long i = 6; i < 11; i++) {
            assertTrue(regionqueue.getRegion().containsKey(new Long(i)));
        }
        // Perform 5 take operations to remove next 5-9 sequence ids
        for (long i = 6; i < 11; i++) {
            regionqueue.take();
        }
        // verify that the last dispatched event was of sequence id 10
        assertEquals(9, regionqueue.getLastDispatchedSequenceId(new EventID(new byte[] { 1 }, 1, 1)));
        // verify that sequence ids 1-10 all are removed from the RQ
        for (long i = 1; i < 11; i++) {
            assertTrue(!regionqueue.getRegion().containsKey(new Long(i)));
        }
        // wait until expiry thread has run once
        synchronized (HARQAddOperationJUnitTest.this) {
            if (0 == expiryCount) {
                HARQAddOperationJUnitTest.this.wait();
            }
            if (1 == expiryCount) {
                // verify that the Thread-identifier has not yet expired
                assertEquals(1, regionqueue.getEventsMapForTesting().size());
                // verify that the sequence-id for Thread-identifier is updated to 9
                assertEquals(new Long(9), regionqueue.getRegion().get(tID));
                // wait until expiry thread has run again
                HARQAddOperationJUnitTest.this.wait();
            }
        }
        // verify that the Thread-identifier has expired
        assertEquals(0, regionqueue.getEventsMapForTesting().size());
        // verify that the sequence-id for Thread-identifier is null
        assertNull(regionqueue.getRegion().get(tID));
    } catch (Exception e) {
        throw new AssertionError("Exception occurred in test due to", e);
    }
}
Also used : CacheException(org.apache.geode.cache.CacheException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) EventID(org.apache.geode.internal.cache.EventID) Conflatable(org.apache.geode.internal.cache.Conflatable) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 12 with CacheListenerAdapter

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

the class HARegionJUnitTest method createHARegion.

/**
   * create the HARegion
   */
private Region createHARegion() throws TimeoutException, CacheWriterException, GatewayException, CacheExistsException, RegionExistsException, IOException, ClassNotFoundException {
    AttributesFactory factory = new AttributesFactory();
    factory.setDataPolicy(DataPolicy.REPLICATE);
    factory.setScope(Scope.DISTRIBUTED_ACK);
    ExpirationAttributes ea = new ExpirationAttributes(2000, ExpirationAction.LOCAL_INVALIDATE);
    factory.setStatisticsEnabled(true);
    ;
    factory.setCacheListener(new CacheListenerAdapter() {

        @Override
        public void afterInvalidate(EntryEvent event) {
        }
    });
    RegionAttributes ra = factory.create();
    Region region = HARegion.getInstance("HARegionJUnitTest_region", (GemFireCacheImpl) cache, null, ra);
    region.getAttributesMutator().setEntryTimeToLive(ea);
    return region;
}
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) HARegion(org.apache.geode.internal.cache.HARegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes)

Example 13 with CacheListenerAdapter

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

the class HARegionQueueDUnitTest method concurrentOperationsDunitTest.

private void concurrentOperationsDunitTest(final boolean createBlockingQueue, final Scope rscope) {
    // Create Cache and HARegionQueue in all the 4 VMs.
    CacheSerializableRunnable createRgnsAndQueues = new CacheSerializableRunnable("CreateCache, mirrored Region & HARegionQueue with a CacheListener") {

        @Override
        public void run2() throws CacheException {
            HARegionQueueDUnitTest test = new HARegionQueueDUnitTest();
            System.getProperties().put("QueueRemovalThreadWaitTime", "2000");
            cache = test.createCache();
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(rscope);
            factory.setDataPolicy(DataPolicy.REPLICATE);
            HARegionQueueAttributes hrqa = new HARegionQueueAttributes();
            hrqa.setExpiryTime(5);
            try {
                if (createBlockingQueue) {
                    hrq = HARegionQueue.getHARegionQueueInstance("testregion1", cache, hrqa, HARegionQueue.BLOCKING_HA_QUEUE, false);
                } else {
                    hrq = HARegionQueue.getHARegionQueueInstance("testregion1", cache, hrqa, HARegionQueue.NON_BLOCKING_HA_QUEUE, false);
                }
            } catch (Exception e) {
                throw new AssertionError(e);
            }
            factory.addCacheListener(new CacheListenerAdapter() {

                @Override
                public void afterCreate(final EntryEvent event) {
                    Conflatable conflatable = new ConflatableObject(event.getKey(), event.getNewValue(), ((EntryEventImpl) event).getEventId(), false, event.getRegion().getFullPath());
                    try {
                        hrq.put(conflatable);
                    } catch (Exception e) {
                        fail("The put operation in queue did not succeed due to exception =", e);
                    }
                }

                @Override
                public void afterUpdate(final EntryEvent event) {
                    Conflatable conflatable = new ConflatableObject(event.getKey(), event.getNewValue(), ((EntryEventImpl) event).getEventId(), true, event.getRegion().getFullPath());
                    try {
                        hrq.put(conflatable);
                    } catch (Exception e) {
                        fail("The put operation in queue did not succeed due to exception =", e);
                    }
                }
            });
            cache.createRegion("test_region", factory.create());
        }
    };
    vm0.invoke(createRgnsAndQueues);
    vm1.invoke(createRgnsAndQueues);
    vm2.invoke(createRgnsAndQueues);
    vm3.invoke(createRgnsAndQueues);
    CacheSerializableRunnable spawnThreadsAndperformOps = new CacheSerializableRunnable("Spawn multiple threads which do various operations") {

        @Override
        public void run2() throws CacheException {
            opThreads = new Thread[4 + 2 + 2 + 2];
            for (int i = 0; i < 4; ++i) {
                opThreads[i] = new Thread(new RunOp(RunOp.PUT, i), "ID=" + i + ",Op=" + RunOp.PUT);
            }
            for (int i = 4; i < 6; ++i) {
                opThreads[i] = new Thread(new RunOp(RunOp.PEEK, i), "ID=" + i + ",Op=" + RunOp.PEEK);
            }
            for (int i = 6; i < 8; ++i) {
                opThreads[i] = new Thread(new RunOp(RunOp.TAKE, i), "ID=" + i + ",Op=" + RunOp.TAKE);
            }
            for (int i = 8; i < 10; ++i) {
                opThreads[i] = new Thread(new RunOp(RunOp.TAKE, i), "ID=" + i + ",Op=" + RunOp.BATCH_PEEK);
            }
            for (int i = 0; i < opThreads.length; ++i) {
                opThreads[i].start();
            }
        }
    };
    vm0.invokeAsync(spawnThreadsAndperformOps);
    vm1.invokeAsync(spawnThreadsAndperformOps);
    vm2.invokeAsync(spawnThreadsAndperformOps);
    vm3.invokeAsync(spawnThreadsAndperformOps);
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e1) {
        fail("Test failed as the test thread encoutered exception in sleep", e1);
    }
    // Asif : In case of blocking HARegionQueue do some extra puts so that the
    // blocking threads
    // are exited
    CacheSerializableRunnable toggleFlag = new CacheSerializableRunnable("Toggle the flag to signal end of threads") {

        @Override
        public void run2() throws CacheException {
            toCnt = false;
            if (createBlockingQueue) {
                try {
                    for (int i = 0; i < 100; ++i) {
                        hrq.put(new ConflatableObject("1", "1", new EventID(new byte[] { 1 }, 100, i), false, "/x"));
                    }
                } catch (Exception e) {
                    throw new AssertionError(e);
                }
            }
        }
    };
    vm0.invokeAsync(toggleFlag);
    vm1.invokeAsync(toggleFlag);
    vm2.invokeAsync(toggleFlag);
    vm3.invokeAsync(toggleFlag);
    CacheSerializableRunnable joinWithThreads = new CacheSerializableRunnable("Join with the threads") {

        @Override
        public void run2() throws CacheException {
            for (int i = 0; i < opThreads.length; ++i) {
                if (opThreads[i].isInterrupted()) {
                    fail("Test failed because  thread encountered exception");
                }
                ThreadUtils.join(opThreads[i], 30 * 1000);
            }
        }
    };
    vm0.invoke(joinWithThreads);
    vm1.invoke(joinWithThreads);
    vm2.invoke(joinWithThreads);
    vm3.invoke(joinWithThreads);
    System.getProperties().remove("QueueRemovalThreadWaitTime");
}
Also used : EntryEventImpl(org.apache.geode.internal.cache.EntryEventImpl) CacheException(org.apache.geode.cache.CacheException) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) EventID(org.apache.geode.internal.cache.EventID) Conflatable(org.apache.geode.internal.cache.Conflatable)

Example 14 with CacheListenerAdapter

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

the class CacheServerTransactionsDUnitTest method createClientCache.

public static void createClientCache(String host, Integer port) throws Exception {
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new CacheServerTransactionsDUnitTest().createCache(props);
    PoolImpl p = (PoolImpl) PoolManager.createFactory().addServer(host, port.intValue()).setSubscriptionEnabled(true).create("CacheServerTransctionDUnitTestPool2");
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    factory.setPoolName(p.getName());
    factory.setCacheListener(new CacheListenerAdapter() {

        public void afterDestroy(EntryEvent event) {
            synchronized (CacheServerTransactionsDUnitTest.class) {
                destroyed = true;
                CacheServerTransactionsDUnitTest.class.notify();
            }
        }

        public void afterInvalidate(EntryEvent event) {
            synchronized (CacheServerTransactionsDUnitTest.class) {
                invalidated = true;
                CacheServerTransactionsDUnitTest.class.notifyAll();
            }
        }
    });
    Region region1 = cache.createRegion(REGION_NAME, factory.create());
    assertNotNull(region1);
    pool = p;
    registerKeys();
}
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) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl)

Example 15 with CacheListenerAdapter

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

the class CacheServerTransactionsDUnitTest method createServerCache.

public static Integer createServerCache(Integer maxThreads) throws Exception {
    new CacheServerTransactionsDUnitTest().createCache(new Properties());
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setDataPolicy(DataPolicy.REPLICATE);
    factory.setCacheListener(new CacheListenerAdapter() {

        public void afterDestroy(EntryEvent event) {
            synchronized (CacheServerTransactionsDUnitTest.class) {
                destroyed = true;
                CacheServerTransactionsDUnitTest.class.notify();
            }
        }

        public void afterInvalidate(EntryEvent event) {
            synchronized (CacheServerTransactionsDUnitTest.class) {
                invalidated = true;
                CacheServerTransactionsDUnitTest.class.notifyAll();
            }
        }
    });
    Region r1 = cache.createRegion(REGION_NAME, factory.create());
    assertNotNull(r1);
    CacheServer server1 = cache.addCacheServer();
    int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    server1.setPort(port);
    server1.setMaxThreads(maxThreads.intValue());
    server1.setNotifyBySubscription(true);
    server1.start();
    createEntries();
    return new Integer(server1.getPort());
}
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) CacheServer(org.apache.geode.cache.server.CacheServer) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties)

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