Search in sources :

Example 6 with SerializableCallable

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

the class SnapshotByteArrayDUnitTest method loadCache.

public void loadCache() throws Exception {
    SerializableCallable setup = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            CacheFactory cf = new CacheFactory().setPdxSerializer(new MyPdxSerializer()).setPdxPersistent(true);
            Cache cache = getCache(cf);
            RegionGenerator rgen = new RegionGenerator();
            rgen.createRegion(cache, null, RegionType.REPLICATE, "snapshot-ops");
            return null;
        }
    };
    SnapshotDUnitTest.forEachVm(setup, true);
}
Also used : MyPdxSerializer(com.examples.snapshot.MyPdxSerializer) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) CacheFactory(org.apache.geode.cache.CacheFactory) Cache(org.apache.geode.cache.Cache)

Example 7 with SerializableCallable

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

the class SnapshotDUnitTest method testExportAndImport.

@Test
public void testExportAndImport() throws Exception {
    File dir = new File(getDiskDirs()[0], "snap");
    dir.mkdir();
    // save all regions
    getCache().getSnapshotService().save(dir, SnapshotFormat.GEMFIRE);
    // update regions with data to be overwritten by import
    updateRegions();
    SerializableCallable callbacks = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            for (final RegionType rt : RegionType.values()) {
                for (final SerializationType st : SerializationType.values()) {
                    String name = "test-" + rt.name() + "-" + st.name();
                    Cache c = getCache();
                    Region<Integer, MyObject> region = c.getRegion(name);
                    region.getAttributesMutator().setCacheWriter(new CacheWriterAdapter<Integer, MyObject>() {

                        @Override
                        public void beforeUpdate(EntryEvent<Integer, MyObject> event) {
                            fail("CacheWriter invoked during import");
                        }
                    });
                    region.getAttributesMutator().addCacheListener(new CacheListenerAdapter<Integer, MyObject>() {

                        @Override
                        public void afterUpdate(EntryEvent<Integer, MyObject> event) {
                            fail("CacheListener invoked during import");
                        }
                    });
                }
            }
            return null;
        }
    };
    // add callbacks
    forEachVm(callbacks, true);
    // load all regions
    loadRegions(dir, null);
}
Also used : RegionType(org.apache.geode.cache.snapshot.RegionGenerator.RegionType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) MyObject(com.examples.snapshot.MyObject) File(java.io.File) SerializationType(org.apache.geode.cache.snapshot.RegionGenerator.SerializationType) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 8 with SerializableCallable

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

the class SnapshotDUnitTest method testExportAndImportWithInvokeCallbacksEnabled.

@Test
public void testExportAndImportWithInvokeCallbacksEnabled() throws Exception {
    File dir = new File(getDiskDirs()[0], "callbacks");
    dir.mkdir();
    // save all regions
    CacheSnapshotService service = getCache().getSnapshotService();
    service.save(dir, SnapshotFormat.GEMFIRE);
    // update regions with data to be overwritten by import
    updateRegions();
    SerializableCallable callbacks = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            for (final RegionType rt : RegionType.values()) {
                for (final SerializationType st : SerializationType.values()) {
                    String name = "test-" + rt.name() + "-" + st.name();
                    Cache cache = getCache();
                    Region<Integer, MyObject> region = cache.getRegion(name);
                    // add CacheWriter and CacheListener
                    AttributesMutator mutator = region.getAttributesMutator();
                    mutator.setCacheWriter(new CountingCacheWriter());
                    mutator.addCacheListener(new CountingCacheListener());
                    // add AsyncEventQueue
                    addAsyncEventQueue(region, name);
                }
            }
            return null;
        }
    };
    // add callbacks
    forEachVm(callbacks, true);
    // load all regions with invoke callbacks enabled
    SnapshotOptions options = service.createOptions();
    options.invokeCallbacks(true);
    loadRegions(dir, options);
    // verify callbacks were invoked
    verifyCallbacksInvoked();
}
Also used : RegionType(org.apache.geode.cache.snapshot.RegionGenerator.RegionType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) MyObject(com.examples.snapshot.MyObject) File(java.io.File) SerializationType(org.apache.geode.cache.snapshot.RegionGenerator.SerializationType) Cache(org.apache.geode.cache.Cache) AttributesMutator(org.apache.geode.cache.AttributesMutator) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 9 with SerializableCallable

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

the class ConnectionPoolAndLoaderDUnitTest method testPoolLoadAndPeer.

/**
   * Test that we can have a peer, a server, and a bridge loader for the same region.
   * 
   * Expected order 1 localCache 2 peer 3 server 4 loader
   */
@Test
public void testPoolLoadAndPeer() throws Exception {
    final String regionName = this.getName();
    final Host host = Host.getHost(0);
    VM server = host.getVM(0);
    VM client1 = host.getVM(1);
    VM client2 = host.getVM(2);
    final int serverPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    server.invoke(new SerializableCallable() {

        public Object call() throws IOException {
            Cache cache = getCache();
            AttributesFactory af = new AttributesFactory();
            RegionAttributes attrs = af.create();
            cache.createRegion(regionName, attrs);
            startBridgeServer(serverPort, true);
            return null;
        }
    });
    SerializableCallable createClient1 = new SerializableCallable() {

        public Object call() {
            // Make sure we get a distributed system that has the locator
            useLocator = true;
            Cache cache = getCache();
            useLocator = false;
            PoolFactory factory = PoolManager.createFactory();
            factory.addServer(NetworkUtils.getServerHostName(host), serverPort);
            factory.create("pool1");
            AttributesFactory af = new AttributesFactory();
            af.setDataPolicy(DataPolicy.NORMAL);
            af.setScope(Scope.DISTRIBUTED_ACK);
            af.setPoolName("pool1");
            af.setCacheLoader(new MyCacheLoader("loaded1"));
            RegionAttributes attrs = af.create();
            cache.createRegion(regionName, attrs);
            return null;
        }
    };
    client1.invoke(createClient1);
    SerializableCallable createClient2 = new SerializableCallable() {

        public Object call() {
            // Make sure we get a distributed system that has the locator
            useLocator = true;
            Cache cache = getCache();
            useLocator = false;
            PoolFactory factory = PoolManager.createFactory();
            factory.addServer(NetworkUtils.getServerHostName(host), serverPort);
            factory.create("pool1");
            AttributesFactory af = new AttributesFactory();
            af.setDataPolicy(DataPolicy.NORMAL);
            af.setScope(Scope.DISTRIBUTED_ACK);
            af.setCacheLoader(new MyCacheLoader("loaded2"));
            af.setPoolName("pool1");
            RegionAttributes attrs = af.create();
            cache.createRegion(regionName, attrs);
            return null;
        }
    };
    client2.invoke(createClient2);
    // We need to test what happens when
    // we do a load in client1 in each of these cases:
    // Case On Server On Client 1 On Client 2 Expected
    // a X server
    // b X X client1
    // c X X X client1
    // d X X client2
    // e X client1
    // f X X client1
    // g X client2 (loader does a netSearch)
    // h client1 loader
    // Setup scenarios
    client1.invoke(new SerializableRunnable() {

        public void run() {
            Region region = getRootRegion(regionName);
            region.put("b", "client1-b");
            region.put("c", "client1-c");
            region.put("e", "client1-e");
            region.put("f", "client1-f");
        }
    });
    client2.invoke(new SerializableRunnable() {

        public void run() {
            Region region = getRootRegion(regionName);
            assertEquals("client1-c", region.get("c"));
            region.put("d", "client2-d");
            assertEquals("client1-f", region.get("f"));
            region.put("g", "client2-g");
        }
    });
    server.invoke(new SerializableRunnable() {

        public void run() {
            Region region = getRootRegion(regionName);
            region.put("a", "server-a");
            region.localDestroy("e");
            region.localDestroy("f");
            region.localDestroy("g");
        }
    });
    // Test the scenarios
    client1.invoke(new SerializableRunnable() {

        public void run() {
            Region region = getRootRegion(regionName);
            assertEquals("server-a", region.get("a"));
            assertEquals("client1-b", region.get("b"));
            assertEquals("client1-c", region.get("c"));
            assertEquals("client2-d", region.get("d"));
            assertEquals("client1-e", region.get("e"));
            assertEquals("client1-f", region.get("f"));
            assertEquals("client2-g", region.get("g"));
            assertEquals("loaded1-h", region.get("h"));
        }
    });
    server.invoke(new SerializableRunnable() {

        public void run() {
            Region region = getRootRegion(regionName);
            assertEquals("server-a", region.get("a"));
            assertEquals("client1-b", region.get("b"));
            assertEquals("client1-c", region.get("c"));
            assertEquals("client2-d", region.get("d"));
            assertEquals(null, region.get("e"));
            assertEquals(null, region.get("f"));
            // dsmith - This result seems somewhat suspect. client1 did a net load
            // which found a value in client2, but it never propagated that result
            // to the server. After talking with Darrel we decided to keep it this
            // way for now.
            assertEquals(null, region.get("g"));
            assertEquals("loaded1-h", region.get("h"));
        }
    });
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) PoolFactory(org.apache.geode.cache.client.PoolFactory) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 10 with SerializableCallable

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

the class SelectStarQueryDUnitTest method startReplicatedCacheServer.

private int startReplicatedCacheServer(VM vm) {
    final int port = (Integer) vm.invoke(new SerializableCallable("Create Server1") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regName);
            Region r2 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regName2);
            // put domain objects
            for (int i = 0; i < 10; i++) {
                r1.put("key-" + i, new PortfolioPdx(i));
                r2.put("key-" + i, new PortfolioPdx(i));
            }
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    return port;
}
Also used : SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) CacheServer(org.apache.geode.cache.server.CacheServer) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx)

Aggregations

SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)502 VM (org.apache.geode.test.dunit.VM)326 Test (org.junit.Test)314 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)311 Host (org.apache.geode.test.dunit.Host)306 Region (org.apache.geode.cache.Region)224 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)157 IgnoredException (org.apache.geode.test.dunit.IgnoredException)155 AttributesFactory (org.apache.geode.cache.AttributesFactory)139 Cache (org.apache.geode.cache.Cache)109 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)95 FunctionException (org.apache.geode.cache.execute.FunctionException)88 ArrayList (java.util.ArrayList)83 HashSet (java.util.HashSet)83 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)77 Execution (org.apache.geode.cache.execute.Execution)74 CommitConflictException (org.apache.geode.cache.CommitConflictException)70 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)66 Function (org.apache.geode.cache.execute.Function)63 IOException (java.io.IOException)62