Thanks Swapnil and Bruce,
I have modified the test ( But I think missing something ), It would help
me to review my Test Case.
package io.ampool.monarch.table;
import java.util.Properties;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheClosedException;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionFactory;
import com.gemstone.gemfire.cache.RegionShortcut;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
import com.gemstone.gemfire.cache.client.ClientRegionFactory;
import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
import com.gemstone.gemfire.cache.server.CacheServer;
import com.gemstone.gemfire.cache30.CacheTestCase;
import com.gemstone.gemfire.distributed.internal.DistributionConfig;
import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
import
com.gemstone.gemfire.distributed.internal.membership.gms.MembershipManagerHelper;
import com.gemstone.gemfire.internal.AvailablePortHelper;
import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
import com.gemstone.gemfire.internal.logging.LogService;
import com.gemstone.gemfire.test.dunit.Host;
import com.gemstone.gemfire.test.dunit.SerializableCallable;
import com.gemstone.gemfire.test.dunit.VM;
import com.gemstone.gemfire.test.dunit.standalone.DUnitLauncher;
import org.apache.logging.log4j.Logger;
/**
* Created by adongre on 12/7/16.
*/
public class GeodeForcedDisconnectAndReconnectDUnitTest extends CacheTestCase {
public GeodeForcedDisconnectAndReconnectDUnitTest(final String name) {
super(name);
}
private static final Logger logger = LogService.getLogger();
Host host = Host.getHost(0);
private VM vm0 = host.getVM(0);
private static final String REGION_NAME = "TEST_REGION";
private static Cache cacheBeforeReconnect = null;
@Override
public void preSetUp() throws Exception {
super.preSetUp();
vm0.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Properties props = new Properties();
props.setProperty(DistributionConfig.LOG_LEVEL_NAME, "info");
props.setProperty(DistributionConfig.LOG_FILE_NAME, "system.log");
props.setProperty(DistributionConfig.MCAST_PORT_NAME,
String.valueOf(0));
props.setProperty(DistributionConfig.LOCATORS_NAME,
DUnitLauncher.getLocatorString());
props.setProperty(DistributionConfig.STATISTIC_ARCHIVE_FILE_NAME,
"Stats");
props.setProperty(DistributionConfig.DISABLE_AUTO_RECONNECT_NAME,
"false");
props.setProperty(DistributionConfig.USE_CLUSTER_CONFIGURATION_NAME,
"true");
Cache c = null;
try {
c = CacheFactory.getAnyInstance();
c.close();
}
catch (CacheClosedException cce) {
}
c = CacheFactory.create(getSystem(props));
CacheServer s = c.addCacheServer();
int port = AvailablePortHelper.getRandomAvailableTCPPort();
s.setPort(port);
s.start();
return port;
}
});
}
@Override
public void preTearDownCacheTestCase() throws Exception {
super.preTearDownCacheTestCase();
vm0.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
CacheFactory.getAnyInstance().close();
return null;
}
});
}
private void updateServerCacheAfterReconnect(final Cache
cacheBeforeReconnect) {
vm0.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
cacheBeforeReconnect.getReconnectedCache();
return null;
}
});
}
private void createRegionOnSever() {
vm0.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Cache cache = CacheFactory.getAnyInstance();
RegionFactory<String, String> rf =
cache.createRegionFactory(RegionShortcut.PARTITION_PERSISTENT);
final Region<String, String> test_pr_region = rf.create(REGION_NAME);
assertNotNull(test_pr_region);
return null;
}
});
}
private void storeCacheReferenceBeforForceDisconnect() {
vm0.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
cacheBeforeReconnect = CacheFactory.getAnyInstance();
return null;
}
});
}
private void performForceDisconnect() {
vm0.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
GemFireCacheImpl oldCache = (GemFireCacheImpl)
CacheFactory.getAnyInstance();
assertNotNull(oldCache);
InternalDistributedSystem distributedSystem =
(InternalDistributedSystem) oldCache.getDistributedSystem();
assertNotNull(distributedSystem);
MembershipManagerHelper.crashDistributedSystem(distributedSystem);
return null;
}
});
}
private void reconnectCacheAfterForceDisconnect() {
vm0.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
cacheBeforeReconnect.getReconnectedCache();
return null;
}
});
}
private void createRegionAgainOnServer() {
vm0.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
GemFireCacheImpl cache = (GemFireCacheImpl)
CacheFactory.getAnyInstance();
assertNotNull(cache);
RegionFactory<String, String> rf =
cache.createRegionFactory(RegionShortcut.PARTITION_PERSISTENT);
final Region<String, String> test_pr_region = rf.create(REGION_NAME);
assertNotNull(test_pr_region);
return null;
}
});
}
public void testForcedDisconnectException() {
createRegionOnSever();
// Create a client cache
String locatorString = DUnitLauncher.getLocatorString();
int index = locatorString.indexOf("[");
int locatorPort = Integer.parseInt(locatorString.substring(index +
1, locatorString.length() - 1));
final ClientCache clientCache = new
ClientCacheFactory().addPoolLocator("127.0.0.1",
locatorPort).create();
ClientRegionFactory<String, String> crf =
clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY);
Region<String, String> clientRegion = crf.create(REGION_NAME);
for (int i = 0; i < 10; i++) {
clientRegion.put("Key" + i, "Value" + i);
}
storeCacheReferenceBeforForceDisconnect();
performForceDisconnect();
reconnectCacheAfterForceDisconnect();
createRegionAgainOnServer();
for (int i = 0; i < 10; i++) {
final String s = clientRegion.get("Key" + i);
assertEquals("Value" + i, s);
}
}
}
I am getting following exception :
com.gemstone.gemfire.cache.client.NoAvailableServersException
at
com.gemstone.gemfire.cache.client.internal.pooling.ConnectionManagerImpl.borrowConnection(ConnectionManagerImpl.java:236)
at
com.gemstone.gemfire.cache.client.internal.OpExecutorImpl.execute(OpExecutorImpl.java:130)
at
com.gemstone.gemfire.cache.client.internal.OpExecutorImpl.execute(OpExecutorImpl.java:109)
at
com.gemstone.gemfire.cache.client.internal.PoolImpl.execute(PoolImpl.java:697)
at com.gemstone.gemfire.cache.client.internal.GetOp.execute(GetOp.java:96)
at
com.gemstone.gemfire.cache.client.internal.ServerRegionProxy.get(ServerRegionProxy.java:101)
at
com.gemstone.gemfire.internal.cache.LocalRegion.findObjectInSystem(LocalRegion.java:2906)
at
com.gemstone.gemfire.internal.cache.LocalRegion.nonTxnFindObject(LocalRegion.java:1504)
at
com.gemstone.gemfire.internal.cache.LocalRegionDataView.findObject(LocalRegionDataView.java:140)
at
com.gemstone.gemfire.internal.cache.LocalRegion.get(LocalRegion.java:1381)
at
com.gemstone.gemfire.internal.cache.LocalRegion.get(LocalRegion.java:1317)
at
com.gemstone.gemfire.internal.cache.LocalRegion.get(LocalRegion.java:1304)
at
com.gemstone.gemfire.internal.cache.AbstractRegion.get(AbstractRegion.java:290)
at
io.ampool.monarch.table.GeodeForcedDisconnectAndReconnectDUnitTest.testForcedDisconnectException(GeodeForcedDisconnectAndReconnectDUnitTest.java:187)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at
org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Thanks
Avinash
On Tue, Jul 12, 2016 at 12:22 AM, Bruce Schuchardt <[email protected]>
wrote:
> You won't be able to fish for the old cache like this. Once it is
> disrupted with a ForceDisconnectException the old cache won't be returned
> by CacheFactory.getAnyInstance(). You'll need to retain a reference to the
> old cache in the test and use that instead.
>
> Client caches shouldn't need to be rebuilt.
>
>
> Le 7/11/2016 à 11:16 AM, Swapnil Bawaskar a écrit :
>
>> private void updateServerCacheAfterReconnect() {
>> > new ArrayList<>(Arrays.asList(vm0/*, vm1, vm2*/)).
>> > forEach((VM) -> VM.invoke(new SerializableCallable() {
>> > @Override
>> > public Object call() throws Exception {
>> > Cache cache = CacheFactory.getAnyInstance();
>> > cache = cache.getReconnectedCache();
>> > return null;
>> > }
>> > }));
>> > }
>> >
>>
>
>