Search in sources :

Example 91 with CacheFactory

use of org.apache.geode.cache.CacheFactory in project geode by apache.

the class CacheLifecycleListenerJUnitTest method testCallbacksRepeat.

@Test
public void testCallbacksRepeat() throws Exception {
    List<CacheLifecycleCallback> cacheCreatedCallbacks = new ArrayList<>();
    List<CacheLifecycleCallback> cacheClosedCallbacks = new ArrayList<>();
    TestCacheLifecycleListener listener = new TestCacheLifecycleListener(cacheCreatedCallbacks, cacheClosedCallbacks);
    try {
        GemFireCacheImpl.addCacheLifecycleListener(listener);
        // assert no create callback
        assertTrue(cacheCreatedCallbacks.isEmpty());
        // assert no close callback
        assertTrue(cacheClosedCallbacks.isEmpty());
        Properties props = new Properties();
        props.setProperty(MCAST_PORT, "0");
        props.setProperty(LOCATORS, "");
        InternalCache cache1 = (InternalCache) new CacheFactory(props).create();
        try {
            // assert one create callback
            assertFalse(cacheCreatedCallbacks.isEmpty());
            assertEquals(1, cacheCreatedCallbacks.size());
            assertEquals(cache1, cacheCreatedCallbacks.get(0).getCache());
            // assert no close callback
            assertTrue(cacheClosedCallbacks.isEmpty());
        } finally {
            cache1.close();
        }
        // assert one create callback
        assertFalse(cacheCreatedCallbacks.isEmpty());
        assertEquals(1, cacheCreatedCallbacks.size());
        assertEquals(cache1, cacheCreatedCallbacks.get(0).getCache());
        // assert one close callback
        assertFalse(cacheClosedCallbacks.isEmpty());
        assertEquals(1, cacheClosedCallbacks.size());
        assertEquals(cache1, cacheClosedCallbacks.get(0).getCache());
        InternalCache cache2 = (InternalCache) new CacheFactory(props).create();
        try {
            // assert two create callback
            assertFalse(cacheCreatedCallbacks.isEmpty());
            assertEquals(2, cacheCreatedCallbacks.size());
            assertEquals(cache1, cacheCreatedCallbacks.get(0).getCache());
            assertEquals(cache2, cacheCreatedCallbacks.get(1).getCache());
            // assert one close callback
            assertFalse(cacheClosedCallbacks.isEmpty());
            assertEquals(1, cacheClosedCallbacks.size());
            assertEquals(cache1, cacheClosedCallbacks.get(0).getCache());
        } finally {
            cache2.close();
        }
        // assert two create callbacks
        assertFalse(cacheCreatedCallbacks.isEmpty());
        assertEquals(2, cacheCreatedCallbacks.size());
        assertEquals(cache1, cacheCreatedCallbacks.get(0).getCache());
        assertEquals(cache2, cacheCreatedCallbacks.get(1).getCache());
        // assert two close callbacks
        assertFalse(cacheClosedCallbacks.isEmpty());
        assertEquals(2, cacheClosedCallbacks.size());
        assertEquals(cache1, cacheClosedCallbacks.get(0).getCache());
        assertEquals(cache2, cacheClosedCallbacks.get(1).getCache());
    } finally {
        GemFireCacheImpl.removeCacheLifecycleListener(listener);
    }
}
Also used : ArrayList(java.util.ArrayList) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) CacheFactory(org.apache.geode.cache.CacheFactory) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 92 with CacheFactory

use of org.apache.geode.cache.CacheFactory in project geode by apache.

the class ClientFunctionTimeoutRegressionTest method createServerCache.

private Integer createServerCache(Boolean createPR) throws IOException {
    Properties props = new Properties();
    props.setProperty(LOCATORS, "localhost[" + DistributedTestUtils.getDUnitLocatorPort() + "]");
    serverCache = (InternalCache) new CacheFactory(props).create();
    RegionFactory<String, String> rf;
    if (createPR) {
        rf = serverCache.createRegionFactory(RegionShortcut.PARTITION);
        rf.setPartitionAttributes(new PartitionAttributesFactory<String, String>().setRedundantCopies(1).setTotalNumBuckets(4).create());
    } else {
        rf = serverCache.createRegionFactory(RegionShortcut.REPLICATE);
    }
    rf.create(REGION_NAME);
    CacheServer server = serverCache.addCacheServer();
    server.setPort(AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET));
    server.start();
    return server.getPort();
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheServer(org.apache.geode.cache.server.CacheServer) Properties(java.util.Properties) DistributedRestoreSystemProperties(org.apache.geode.test.dunit.rules.DistributedRestoreSystemProperties) CacheFactory(org.apache.geode.cache.CacheFactory) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory)

Example 93 with CacheFactory

use of org.apache.geode.cache.CacheFactory in project geode by apache.

the class AcceptorImplDUnitTest method testAcceptorImplCloseCleansUpWithHangingConnection.

/**
   * GEODE-2324. There was a bug where, due to an uncaught exception, `AcceptorImpl.close()` was
   * short-circuiting and failing to clean up properly.
   *
   * What this test does is start a Cache and hook the Acceptor to interrupt the thread before the
   * place where an InterruptedException could be thrown. It interrupts the thread, and checks that
   * the thread has terminated normally without short-circuiting. It doesn't check that every part
   * of the AcceptorImpl has shut down properly -- that seems both difficult to check (especially
   * since the fields are private) and implementation-dependent.
   */
@Test
public void testAcceptorImplCloseCleansUpWithHangingConnection() throws Exception {
    final String hostname = Host.getHost(0).getHostName();
    final VM clientVM = Host.getHost(0).getVM(0);
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    try (InternalCache cache = (InternalCache) new CacheFactory(props).create()) {
        RegionFactory<Object, Object> regionFactory = cache.createRegionFactory(RegionShortcut.PARTITION);
        SleepyCacheWriter<Object, Object> sleepyCacheWriter = new SleepyCacheWriter<>();
        regionFactory.setCacheWriter(sleepyCacheWriter);
        final CacheServer server = cache.addCacheServer();
        final int port = AvailablePortHelper.getRandomAvailableTCPPort();
        server.setPort(port);
        server.start();
        regionFactory.create("region1");
        assertTrue(cache.isServer());
        assertFalse(cache.isClosed());
        Awaitility.await("Acceptor is up and running").atMost(10, SECONDS).until(() -> getAcceptorImplFromCache(cache) != null);
        AcceptorImpl acceptorImpl = getAcceptorImplFromCache(cache);
        clientVM.invokeAsync(() -> {
            // System.setProperty("gemfire.PoolImpl.TRY_SERVERS_ONCE", "true");
            ClientCacheFactory clientCacheFactory = new ClientCacheFactory();
            clientCacheFactory.addPoolServer(hostname, port);
            clientCacheFactory.setPoolReadTimeout(5000);
            clientCacheFactory.setPoolRetryAttempts(1);
            clientCacheFactory.setPoolMaxConnections(1);
            clientCacheFactory.setPoolFreeConnectionTimeout(1000);
            ClientCache clientCache = clientCacheFactory.create();
            Region<Object, Object> clientRegion1 = clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY).create("region1");
            clientRegion1.put("foo", "bar");
        });
        Awaitility.await("Cache writer starts").atMost(10, SECONDS).until(sleepyCacheWriter::isStarted);
        cache.close();
        Awaitility.await("Cache writer interrupted").atMost(10, SECONDS).until(sleepyCacheWriter::isInterrupted);
        sleepyCacheWriter.stopWaiting();
        Awaitility.await("Acceptor shuts down properly").atMost(10, SECONDS).until(() -> acceptorImpl.isShutdownProperly());
        // for debugging.
        ThreadUtils.dumpMyThreads();
        regionFactory.setCacheWriter(null);
    }
}
Also used : InternalCache(org.apache.geode.internal.cache.InternalCache) ClientCache(org.apache.geode.cache.client.ClientCache) Properties(java.util.Properties) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) VM(org.apache.geode.test.dunit.VM) CacheServer(org.apache.geode.cache.server.CacheServer) CacheFactory(org.apache.geode.cache.CacheFactory) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 94 with CacheFactory

use of org.apache.geode.cache.CacheFactory in project geode by apache.

the class RollingUpgrade2DUnitTest method createCache.

public static Cache createCache(Properties systemProperties) throws Exception {
    systemProperties.setProperty(DistributionConfig.USE_CLUSTER_CONFIGURATION_NAME, "false");
    CacheFactory cf = new CacheFactory(systemProperties);
    return cf.create();
}
Also used : CacheFactory(org.apache.geode.cache.CacheFactory) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory)

Example 95 with CacheFactory

use of org.apache.geode.cache.CacheFactory in project geode by apache.

the class PdxSerializableDUnitTest method testPersistenceExplicitDiskStore.

@Test
public void testPersistenceExplicitDiskStore() throws Throwable {
    SerializableCallable createRegion = new SerializableCallable() {

        public Object call() throws Exception {
            // Make sure the type registry is persistent
            CacheFactory cf = new CacheFactory();
            cf.setPdxPersistent(true);
            cf.setPdxDiskStore("store1");
            Cache cache = getCache(cf);
            cache.createDiskStoreFactory().setMaxOplogSize(1).setDiskDirs(getDiskDirs()).create("store1");
            AttributesFactory af = new AttributesFactory();
            af.setScope(Scope.DISTRIBUTED_ACK);
            af.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
            af.setDiskStoreName("store1");
            createRootRegion("testSimplePdx", af.create());
            return null;
        }
    };
    persistenceTest(createRegion);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) CacheFactory(org.apache.geode.cache.CacheFactory) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

CacheFactory (org.apache.geode.cache.CacheFactory)125 Properties (java.util.Properties)51 Test (org.junit.Test)51 Cache (org.apache.geode.cache.Cache)44 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)38 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)28 Region (org.apache.geode.cache.Region)21 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)18 CacheServer (org.apache.geode.cache.server.CacheServer)17 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)17 File (java.io.File)15 Before (org.junit.Before)14 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)13 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)12 ClientCache (org.apache.geode.cache.client.ClientCache)11 Host (org.apache.geode.test.dunit.Host)11 IOException (java.io.IOException)10 AttributesFactory (org.apache.geode.cache.AttributesFactory)10 PdxType (org.apache.geode.pdx.internal.PdxType)9 MyPdxSerializer (com.examples.snapshot.MyPdxSerializer)8