Search in sources :

Example 21 with SerializableRunnable

use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.

the class PRQueryDUnitHelper method getCacheSerializableRunnableForLocalRegionWithAsyncIndexCreation.

public CacheSerializableRunnable getCacheSerializableRunnableForLocalRegionWithAsyncIndexCreation(final String regionName, final Class constraint) {
    SerializableRunnable createPrRegion;
    createPrRegion = new CacheSerializableRunnable(regionName) {

        @Override
        public void run2() throws CacheException {
            Cache cache = getCache();
            Region localRegion = null;
            try {
                AttributesFactory attr = new AttributesFactory();
                attr.setValueConstraint(constraint);
                attr.setScope(Scope.LOCAL);
                attr.setIndexMaintenanceSynchronous(false);
                localRegion = cache.createRegion(regionName, attr.create());
            } catch (IllegalStateException ex) {
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().warning("PRQueryDUnitHelper#getCacheSerializableRunnableForPRCreate: Creation caught IllegalStateException", ex);
            }
            assertNotNull("PRQueryDUnitHelper#getCacheSerializableRunnableForPRCreate: Partitioned Region " + regionName + " not in cache", cache.getRegion(regionName));
            assertNotNull("PRQueryDUnitHelper#getCacheSerializableRunnableForPRCreate: Partitioned Region ref null", localRegion);
            assertTrue("PRQueryDUnitHelper#getCacheSerializableRunnableForPRCreate: Partitioned Region ref claims to be destroyed", !localRegion.isDestroyed());
        }
    };
    return (CacheSerializableRunnable) createPrRegion;
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache)

Example 22 with SerializableRunnable

use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.

the class PRQueryDUnitHelper method getCacheSerializableRunnableForPRRandomOps.

/**
   * This function puts portfolio objects into the created Region (PR or RR). Also, other operation
   * like, invalidate, destroy and create are performed in random manner based on
   * {@link Random#nextInt(int)}.
   * 
   * @param regionName
   * @param to
   * @param from
   * @return cacheSerializable object
   */
public CacheSerializableRunnable getCacheSerializableRunnableForPRRandomOps(final String regionName, final int from, final int to) {
    SerializableRunnable prPuts = new CacheSerializableRunnable("PRPuts") {

        @Override
        public void run2() throws CacheException {
            Cache cache = getCache();
            Region region = cache.getRegion(regionName);
            for (int i = 0; i < 3; i++) {
                for (int j = from; j < to; j++) {
                    int op = new Random().nextInt(4);
                    try {
                        switch(op) {
                            case 0:
                                // Put operation
                                region.put(new Integer(j), new Portfolio(j));
                                break;
                            case 1:
                                // invalidate
                                if (region.containsKey(new Integer(j))) {
                                    region.invalidate(new Integer(j));
                                }
                                break;
                            case 2:
                                if (region.containsKey(new Integer(j))) {
                                    region.destroy(new Integer(j));
                                }
                                break;
                            case 3:
                                if (!region.containsKey(new Integer(j))) {
                                    region.create(new Integer(j), null);
                                }
                                break;
                            default:
                                break;
                        }
                    } catch (EntryExistsException e) {
                        // Do nothing let it go
                        org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("EntryExistsException was thrown for key " + j);
                    } catch (EntryNotFoundException e) {
                        // Do nothing let it go
                        org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("EntryNotFoundException was thrown for key " + j);
                    }
                }
            }
        }
    };
    return (CacheSerializableRunnable) prPuts;
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) Random(java.util.Random) Portfolio(org.apache.geode.cache.query.data.Portfolio) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) EntryExistsException(org.apache.geode.cache.EntryExistsException) Cache(org.apache.geode.cache.Cache)

Example 23 with SerializableRunnable

use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.

the class PRQueryDUnitHelper method getCacheSerializableRunnableForPersistentPRCreate.

public CacheSerializableRunnable getCacheSerializableRunnableForPersistentPRCreate(final String regionName, final int redundancy, final Class constraint) {
    SerializableRunnable createPrRegion;
    createPrRegion = new CacheSerializableRunnable(regionName) {

        @Override
        public void run2() throws CacheException {
            Cache cache = getCache();
            Region partitionedregion = null;
            try {
                cache.createDiskStoreFactory().setDiskDirs(JUnit4CacheTestCase.getDiskDirs()).create("diskstore");
                AttributesFactory attr = new AttributesFactory();
                attr.setValueConstraint(constraint);
                attr.setDataPolicy(DataPolicy.PERSISTENT_PARTITION);
                attr.setDiskStoreName("diskstore");
                PartitionAttributesFactory paf = new PartitionAttributesFactory();
                PartitionAttributes prAttr = paf.setRedundantCopies(redundancy).create();
                attr.setPartitionAttributes(prAttr);
                partitionedregion = cache.createRegion(regionName, attr.create());
            } catch (IllegalStateException ex) {
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().warning("PRQueryDUnitHelper#getCacheSerializableRunnableForPRCreateWithRedundancy: Creation caught IllegalStateException", ex);
            }
            assertNotNull("PRQueryDUnitHelper#getCacheSerializableRunnableForPRCreateWithRedundancy: Partitioned Region " + regionName + " not in cache", cache.getRegion(regionName));
            assertNotNull("PRQueryDUnitHelper#getCacheSerializableRunnableForPRCreateWithRedundancy: Partitioned Region ref null", partitionedregion);
            assertTrue("PRQueryDUnitHelper#getCacheSerializableRunnableForPRCreateWithRedundancy: Partitioned Region ref claims to be destroyed", !partitionedregion.isDestroyed());
        }
    };
    return (CacheSerializableRunnable) createPrRegion;
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) PartitionAttributes(org.apache.geode.cache.PartitionAttributes) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache)

Example 24 with SerializableRunnable

use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.

the class ConnectionPoolDUnitTest method test024CreateNullValue.

/**
   * Tests that invoking {@link Region#create} with a <code>null</code> value does the right thing
   * with the {@link Pool}.
   *
   * @since GemFire 3.5
   */
@Test
public void test024CreateNullValue() 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);
    final Object createCallbackArg = "CREATE CALLBACK ARG";
    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 create = 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);
            createRegion(name, factory.create());
        }
    };
    vm1.invoke(create);
    vm2.invoke(create);
    vm2.invoke(new CacheSerializableRunnable("Create nulls") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i < 10; i++) {
                region.create(new Integer(i), null, createCallbackArg);
            }
        }
    });
    // Wait for updates to be propagated
    Wait.pause(1000);
    vm2.invoke(new CacheSerializableRunnable("Verify invalidates") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i < 10; i++) {
                Region.Entry entry = region.getEntry(new Integer(i));
                assertNotNull(entry);
                assertNull(entry.getValue());
            }
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Attempt to create values") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i < 10; i++) {
                region.create(new Integer(i), "new" + i);
            }
        }
    });
    // Wait for updates to be propagated
    Wait.pause(1000);
    vm2.invoke(new CacheSerializableRunnable("Verify invalidates") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i < 10; i++) {
                Region.Entry entry = region.getEntry(new Integer(i));
                assertNotNull(entry);
                assertNull(entry.getValue());
            }
        }
    });
    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 : 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) 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 25 with SerializableRunnable

use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.

the class ConnectionPoolDUnitTest method test014InvalidateAndDestroyPropagation.

/**
   * Tests that invalidates and destroys are propagated to {@link Pool}s.
   *
   * @since GemFire 3.5
   */
@Test
public void test014InvalidateAndDestroyPropagation() 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 create = 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(create);
    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(create);
    Wait.pause(5 * 1000);
    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);
                assertNotNull(entry);
                assertNull(entry.getValue());
            }
            {
                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("old" + i, ee.getOldValue());
                    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(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);
            }
        }
    });
    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();
            List l = ctl.getEventHistory();
            org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("history (should be empty): " + l);
            assertEquals(0, l.size());
            // 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);
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("processing " + ee);
                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)

Aggregations

SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)741 VM (org.apache.geode.test.dunit.VM)405 Test (org.junit.Test)403 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)353 Region (org.apache.geode.cache.Region)347 Host (org.apache.geode.test.dunit.Host)344 Cache (org.apache.geode.cache.Cache)274 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)259 CacheException (org.apache.geode.cache.CacheException)207 AttributesFactory (org.apache.geode.cache.AttributesFactory)204 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)198 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)166 LocalRegion (org.apache.geode.internal.cache.LocalRegion)160 IOException (java.io.IOException)145 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)120 Properties (java.util.Properties)66 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)66 AsyncInvocation (org.apache.geode.test.dunit.AsyncInvocation)65 IgnoredException (org.apache.geode.test.dunit.IgnoredException)61 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)53