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);
}
}
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();
}
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);
}
}
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();
}
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);
}
Aggregations