Search in sources :

Example 16 with ClientCache

use of org.apache.geode.cache.client.ClientCache in project geode by apache.

the class OnGroupsFunctionExecutionDUnitTest method testOnServer.

@Test
public void testOnServer() {
    Host host = Host.getHost(0);
    VM server0 = host.getVM(0);
    VM server1 = host.getVM(1);
    VM server2 = host.getVM(2);
    VM client = host.getVM(3);
    VM locator = Host.getLocator();
    final String regionName = getName();
    initVM(server0, "mg,g0", regionName, true);
    initVM(server1, "g1", regionName, true);
    initVM(server2, "g0,g1,g2", regionName, true);
    final int locatorPort = getLocatorPort(locator);
    final String hostName = host.getHostName();
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            try {
                Cache c = CacheFactory.getAnyInstance();
                c.close();
            } catch (CacheClosedException cce) {
            }
            disconnectFromDS();
            LogWriterUtils.getLogWriter().fine("SWAP:creating client cache");
            ClientCacheFactory ccf = new ClientCacheFactory();
            ccf.addPoolLocator(hostName, locatorPort);
            ccf.setPoolServerGroup("mg");
            ccf.set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
            ClientCache c = ccf.create();
            IgnoredException ex = IgnoredException.addIgnoredException("No member found");
            try {
                InternalFunctionService.onServer(c, "no such group").execute(new OnGroupsFunction()).getResult();
                fail("expected exception not thrown");
            } catch (FunctionException e) {
            } finally {
                ex.remove();
            }
            InternalFunctionService.onServer(c, "g1").execute(new OnGroupsFunction()).getResult();
            return null;
        }
    });
    int c0 = getAndResetInvocationCount(server0);
    int c1 = getAndResetInvocationCount(server1);
    int c2 = getAndResetInvocationCount(server2);
    assertEquals(1, c0 + c1 + c2);
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            ClientCache c = ClientCacheFactory.getAnyInstance();
            InternalFunctionService.onServer(c, "g0").execute(new OnGroupsFunction()).getResult();
            return null;
        }
    });
    verifyAndResetInvocationCount(server0, 1);
    verifyAndResetInvocationCount(server1, 0);
    verifyAndResetInvocationCount(server2, 0);
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            ClientCache c = ClientCacheFactory.getAnyInstance();
            InternalFunctionService.onServer(c, "mg", "g1").execute(new OnGroupsFunction()).getResult();
            return null;
        }
    });
    c0 = getAndResetInvocationCount(server0);
    c1 = getAndResetInvocationCount(server1);
    c2 = getAndResetInvocationCount(server2);
    assertEquals(2, c0 + c1 + c2);
}
Also used : FunctionException(org.apache.geode.cache.execute.FunctionException) Host(org.apache.geode.test.dunit.Host) CacheClosedException(org.apache.geode.cache.CacheClosedException) ClientCache(org.apache.geode.cache.client.ClientCache) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) IgnoredException(org.apache.geode.test.dunit.IgnoredException) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 17 with ClientCache

use of org.apache.geode.cache.client.ClientCache in project geode by apache.

the class OnGroupsFunctionExecutionDUnitTest method dotestBasicClientServerFunction.

private void dotestBasicClientServerFunction(final boolean register, final boolean withArgs) {
    Host host = Host.getHost(0);
    VM server0 = host.getVM(0);
    VM server1 = host.getVM(1);
    VM server2 = host.getVM(2);
    VM client = host.getVM(3);
    VM locator = Host.getLocator();
    final String regionName = getName();
    initVM(server0, "mg,g0", regionName, true);
    initVM(server1, "g1", regionName, true);
    initVM(server2, "g0,g1", regionName, true);
    if (register) {
        registerFunction(server0);
        registerFunction(server1);
        registerFunction(server2);
    }
    final int locatorPort = getLocatorPort(locator);
    final String hostName = host.getHostName();
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            try {
                Cache c = CacheFactory.getAnyInstance();
                c.close();
            } catch (CacheClosedException cce) {
            }
            disconnectFromDS();
            LogWriterUtils.getLogWriter().fine("SWAP:creating client cache");
            ClientCacheFactory ccf = new ClientCacheFactory();
            ccf.addPoolLocator(hostName, locatorPort);
            ccf.setPoolServerGroup("mg");
            ccf.set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
            ClientCache c = ccf.create();
            c.getLogger().info("SWAP:invoking function from client on g0");
            Execution e = InternalFunctionService.onServers(c, "g0");
            if (withArgs) {
                ArrayList<String> args = new ArrayList<String>();
                args.add("g0");
                e = e.setArguments(args);
            }
            if (register) {
                e.execute(OnGroupsFunction.Id).getResult();
            } else {
                e.execute(new OnGroupsFunction()).getResult();
            }
            return null;
        }
    });
    verifyAndResetInvocationCount(server0, 1);
    verifyAndResetInvocationCount(server1, 0);
    verifyAndResetInvocationCount(server2, 1);
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            ClientCache c = ClientCacheFactory.getAnyInstance();
            c.getLogger().fine("SWAP:invoking function from client on mg");
            Execution e = InternalFunctionService.onServers(c, "mg");
            if (withArgs) {
                ArrayList<String> args = new ArrayList<String>();
                args.add("mg");
                e = e.setArguments(args);
            }
            if (register) {
                e.execute(OnGroupsFunction.Id).getResult();
            } else {
                e.execute(new OnGroupsFunction()).getResult();
            }
            return null;
        }
    });
    verifyAndResetInvocationCount(server0, 1);
    verifyAndResetInvocationCount(server1, 0);
    verifyAndResetInvocationCount(server2, 0);
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            ClientCache c = ClientCacheFactory.getAnyInstance();
            c.getLogger().fine("SWAP:invoking function from client on g0 g1");
            Execution e = InternalFunctionService.onServers(c, "g0", "g1");
            if (withArgs) {
                ArrayList<String> args = new ArrayList<String>();
                args.add("g0");
                args.add("g1");
                e = e.setArguments(args);
            }
            if (register) {
                e.execute(OnGroupsFunction.Id).getResult();
            } else {
                e.execute(new OnGroupsFunction()).getResult();
            }
            return null;
        }
    });
    verifyAndResetInvocationCount(server0, 1);
    verifyAndResetInvocationCount(server1, 1);
    verifyAndResetInvocationCount(server2, 1);
}
Also used : ArrayList(java.util.ArrayList) Host(org.apache.geode.test.dunit.Host) CacheClosedException(org.apache.geode.cache.CacheClosedException) ClientCache(org.apache.geode.cache.client.ClientCache) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) Execution(org.apache.geode.cache.execute.Execution) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache)

Example 18 with ClientCache

use of org.apache.geode.cache.client.ClientCache in project geode by apache.

the class OnGroupsFunctionExecutionDUnitTest method testClientServerOneMemberFailure.

@Test
public void testClientServerOneMemberFailure() {
    Host host = Host.getHost(0);
    VM server0 = host.getVM(0);
    VM server1 = host.getVM(1);
    VM server2 = host.getVM(2);
    VM client = host.getVM(3);
    VM locator = Host.getLocator();
    final String regionName = getName();
    initVM(server0, "mg,g0", regionName, true);
    initVM(server1, "g1", regionName, true);
    initVM(server2, "g0,g1,g2", regionName, true);
    final int locatorPort = getLocatorPort(locator);
    final String hostName = host.getHostName();
    client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            try {
                Cache c = CacheFactory.getAnyInstance();
                c.close();
            } catch (CacheClosedException cce) {
            }
            disconnectFromDS();
            LogWriterUtils.getLogWriter().fine("SWAP:creating client cache");
            ClientCacheFactory ccf = new ClientCacheFactory();
            ccf.addPoolLocator(hostName, locatorPort);
            ccf.setPoolServerGroup("mg");
            ccf.set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
            ClientCache c = ccf.create();
            Execution e = InternalFunctionService.onServers(c, "g1");
            ArrayList<String> args = new ArrayList<String>();
            args.add("disconnect");
            args.add("g2");
            e = e.setArguments(args);
            IgnoredException.addIgnoredException("FunctionInvocationTargetException");
            try {
                e.execute(new OnGroupsExceptionFunction()).getResult();
                fail("expected exception not thrown");
            } catch (FunctionException ex) {
                assertTrue(ex.getCause() instanceof FunctionInvocationTargetException);
            }
            return null;
        }
    });
}
Also used : ArrayList(java.util.ArrayList) FunctionException(org.apache.geode.cache.execute.FunctionException) Host(org.apache.geode.test.dunit.Host) CacheClosedException(org.apache.geode.cache.CacheClosedException) ClientCache(org.apache.geode.cache.client.ClientCache) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) Execution(org.apache.geode.cache.execute.Execution) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 19 with ClientCache

use of org.apache.geode.cache.client.ClientCache in project geode by apache.

the class Bug47667DUnitTest method testbug47667.

@Test
public void testbug47667() {
    Host host = Host.getHost(0);
    VM locator = host.getVM(0);
    VM server1 = host.getVM(1);
    VM server2 = host.getVM(2);
    VM client = host.getVM(3);
    final int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    final String locatorHost = NetworkUtils.getServerHostName(host);
    locator.invoke("Start Locator", () -> startLocator(locatorHost, locatorPort, ""));
    String locString = getLocatorString(host, locatorPort);
    server1.invoke("Start BridgeServer", () -> startBridgeServer(new String[] { "R1" }, locString, new String[] { "R1" }));
    server2.invoke("Start BridgeServer", () -> startBridgeServer(new String[] { "R2" }, locString, new String[] { "R2" }));
    client.invoke("create region and insert data in transaction", () -> {
        ClientCacheFactory ccf = new ClientCacheFactory();
        ccf.addPoolLocator(locatorHost, locatorPort);
        ClientCache cache = ccf.create();
        PoolManager.createFactory().addLocator(locatorHost, locatorPort).setServerGroup("R1").create("R1");
        PoolManager.createFactory().addLocator(locatorHost, locatorPort).setServerGroup("R2").create("R2");
        Region region1 = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).setPoolName("R1").create("R1");
        Region region2 = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).setPoolName("R2").create("R2");
        CacheTransactionManager transactionManager = cache.getCacheTransactionManager();
        transactionManager.begin();
        region1.put(1, "value1");
        transactionManager.commit();
        transactionManager.begin();
        region2.put(2, "value2");
        transactionManager.commit();
        return null;
    });
}
Also used : VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) ClientCache(org.apache.geode.cache.client.ClientCache) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 20 with ClientCache

use of org.apache.geode.cache.client.ClientCache in project geode by apache.

the class FunctionServiceClientOnServerBase method createServers.

@Before
public void createServers() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    ClientCache cache = createServersAndClient(numberOfExecutions());
}
Also used : VM(org.apache.geode.test.dunit.VM) Host(org.apache.geode.test.dunit.Host) ClientCache(org.apache.geode.cache.client.ClientCache) Before(org.junit.Before)

Aggregations

ClientCache (org.apache.geode.cache.client.ClientCache)115 Test (org.junit.Test)74 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)73 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)67 Region (org.apache.geode.cache.Region)65 VM (org.apache.geode.test.dunit.VM)43 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)42 Host (org.apache.geode.test.dunit.Host)40 SecurityTest (org.apache.geode.test.junit.categories.SecurityTest)31 QueryService (org.apache.geode.cache.query.QueryService)27 SelectResults (org.apache.geode.cache.query.SelectResults)26 SecurityTestUtil.createClientCache (org.apache.geode.security.SecurityTestUtil.createClientCache)25 SecurityTestUtil.createProxyRegion (org.apache.geode.security.SecurityTestUtil.createProxyRegion)23 CacheServer (org.apache.geode.cache.server.CacheServer)22 Cache (org.apache.geode.cache.Cache)20 CacheException (org.apache.geode.cache.CacheException)15 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)13 IOException (java.io.IOException)12 Properties (java.util.Properties)12 IgnoredException (org.apache.geode.test.dunit.IgnoredException)12