Search in sources :

Example 46 with ClientCache

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

the class PdxLocalQueryVersionedClassDUnitTest method testIsRemoteFlagForRemoteQueries.

/**
   * Testing the isRemote flag which could be inconsistent when bind queries are being executed in
   * multiple threads. Bug #49662 is caused because of this inconsistent behavior.
   * 
   * @throws Exception
   */
@Test
public void testIsRemoteFlagForRemoteQueries() throws Exception {
    final Host host = Host.getHost(0);
    final VM server = host.getVM(0);
    final VM client = host.getVM(1);
    final int numberOfEntries = 1000;
    final String name = "/" + regionName;
    final String query = "select distinct * from " + name + " where id > $1 and id < $2 and status = 'active'";
    // Start server
    final int port1 = (Integer) server.invoke(new SerializableCallable("Create Server") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // Start client and put version1 objects on server
    // Server does not have version1 classes in classpath
    client.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.addPoolServer(NetworkUtils.getServerHostName(server.getHost()), port1);
            ClientCache cache = getClientCache(cf);
            Region region = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
            for (int i = 0; i < numberOfEntries; i++) {
                PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedNewPortfolio", false);
                pdxInstanceFactory.writeInt("id", i);
                pdxInstanceFactory.writeString("status", (i % 2 == 0 ? "active" : "inactive"));
                PdxInstance pdxInstance = pdxInstanceFactory.create();
                region.put("key-" + i, pdxInstance);
            }
            return null;
        }
    });
    // Execute same query remotely from client using 2 threads
    // Since this is a bind query, the query object will be shared
    // between the 2 threads.
    AsyncInvocation a1 = client.invokeAsync(new SerializableCallable("Query from client") {

        @Override
        public Object call() throws Exception {
            QueryService qs = null;
            SelectResults sr = null;
            // Execute query remotely
            try {
                qs = getCache().getQueryService();
            } catch (Exception e) {
                Assert.fail("Failed to get QueryService.", e);
            }
            try {
                for (int i = 0; i < 100; i++) {
                    sr = (SelectResults) qs.newQuery(query).execute(new Object[] { 1, 1000 });
                }
                Assert.assertTrue("Size of resultset should be greater than 0 for query: " + query, sr.size() > 0);
            } catch (Exception e) {
                Assert.fail("Failed executing query " + query, e);
            }
            return null;
        }
    });
    AsyncInvocation a2 = client.invokeAsync(new SerializableCallable("Query from client") {

        @Override
        public Object call() throws Exception {
            QueryService qs = null;
            SelectResults sr = null;
            // Execute query remotely
            try {
                qs = getCache().getQueryService();
            } catch (Exception e) {
                Assert.fail("Failed to get QueryService.", e);
            }
            try {
                for (int i = 0; i < 100; i++) {
                    sr = (SelectResults) qs.newQuery(query).execute(new Object[] { 997, 1000 });
                }
                Assert.assertTrue("Size of resultset should be greater than 0 for query: " + query, sr.size() > 0);
            } catch (Exception e) {
                Assert.fail("Failed executing query " + query, e);
            }
            return null;
        }
    });
    ThreadUtils.join(a1, 60 * 1000);
    ThreadUtils.join(a2, 60 * 1000);
    if (a1.exceptionOccurred()) {
        Assert.fail("Failed query execution " + a1.getException().getMessage());
    }
    if (a2.exceptionOccurred()) {
        Assert.fail("Failed query execution " + a2.getException());
    }
    this.closeClient(client);
    this.closeClient(server);
}
Also used : PdxInstanceFactory(org.apache.geode.pdx.PdxInstanceFactory) Host(org.apache.geode.test.dunit.Host) ClientCache(org.apache.geode.cache.client.ClientCache) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) SelectResults(org.apache.geode.cache.query.SelectResults) PdxInstance(org.apache.geode.pdx.PdxInstance) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) CacheServer(org.apache.geode.cache.server.CacheServer) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 47 with ClientCache

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

the class RestAPIsWithSSLDUnitTest method createRegionInClientCache.

private void createRegionInClientCache() {
    ClientCache clientCache = ClientCacheFactory.getAnyInstance();
    assertNotNull(clientCache);
    clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(PEOPLE_REGION_NAME);
    clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(REGION_NAME);
}
Also used : ClientCache(org.apache.geode.cache.client.ClientCache)

Example 48 with ClientCache

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

the class RestAPIsAndInterOpsDUnitTest method createRegionInClientCache.

public void createRegionInClientCache() {
    ClientCache cache = GemFireCacheImpl.getInstance();
    assertNotNull(cache);
    ClientRegionFactory<String, Object> crf = cache.createClientRegionFactory(ClientRegionShortcut.PROXY);
    Region<String, Object> region = crf.create(PEOPLE_REGION_NAME);
}
Also used : JSONObject(org.json.JSONObject) ClientCache(org.apache.geode.cache.client.ClientCache)

Example 49 with ClientCache

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

the class QueryParamsAuthorizationDUnitTest method testQueryParamsInAuthCallback.

@Ignore("Bug 51079")
@Test
public void testQueryParamsInAuthCallback() throws Exception {
    final Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM client = host.getVM(1);
    // create servers and regions
    final int port = (Integer) server1.invoke(new SerializableCallable("Create Server1") {

        @Override
        public Object call() throws Exception {
            CacheFactory cf = new CacheFactory().set(MCAST_PORT, "0").set(SECURITY_CLIENT_ACCESSOR, "org.apache.geode.cache.query.dunit.QueryAuthorization.create").set(SECURITY_CLIENT_AUTHENTICATOR, DummyAuthenticator.class.getName() + ".create");
            Cache cache = getCache(cf);
            cache.createRegionFactory(RegionShortcut.REPLICATE).create(regName);
            CacheServer server = cache.addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // create client
    client.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory ccf = new ClientCacheFactory().addPoolServer(NetworkUtils.getServerHostName(server1.getHost()), port).set(SECURITY_CLIENT_AUTH_INIT, UserPasswordAuthInit.class.getName() + ".create").set(SECURITY_PREFIX + "username", "root").set(SECURITY_PREFIX + "password", "root");
            ClientCache cache = getClientCache(ccf);
            Region r1 = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regName);
            for (int i = 0; i < 20; i++) {
                r1.put("key-" + i, new Portfolio(i));
            }
            QueryService qs = cache.getQueryService();
            Object[] params = new Object[] { "active", 0 };
            SelectResults sr = (SelectResults) qs.newQuery("select * from " + r1.getFullPath() + " where status = $1 and ID > $2 ").execute(params);
            assertTrue("Result size should be greater than 0 ", sr.size() > 0);
            return null;
        }
    });
}
Also used : Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) ClientCache(org.apache.geode.cache.client.ClientCache) UserPasswordAuthInit(org.apache.geode.security.templates.UserPasswordAuthInit) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) CacheServer(org.apache.geode.cache.server.CacheServer) Region(org.apache.geode.cache.Region) CacheFactory(org.apache.geode.cache.CacheFactory) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) Ignore(org.junit.Ignore) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 50 with ClientCache

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

the class QueryUsingFunctionContextDUnitTest method createProxyRegs.

private void createProxyRegs() {
    ClientCache cache = (ClientCache) CacheFactory.getAnyInstance();
    cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(repRegionName);
    cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(localRegionName);
    cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(PartitionedRegionName1);
    cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(PartitionedRegionName2);
    cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(PartitionedRegionName3);
    cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(PartitionedRegionName4);
    cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(PartitionedRegionName5);
}
Also used : ClientCache(org.apache.geode.cache.client.ClientCache)

Aggregations

ClientCache (org.apache.geode.cache.client.ClientCache)112 Test (org.junit.Test)74 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)73 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)65 Region (org.apache.geode.cache.Region)64 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)26 SelectResults (org.apache.geode.cache.query.SelectResults)25 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