use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class RequiredRolesDUnitTest method testWaitForRequiredRoles.
/**
* Tests RequiredRoles.waitForRequiredRoles().
*/
@Test
public void testWaitForRequiredRoles() throws Exception {
final String name = this.getUniqueName();
final int vm0 = 0;
final int vm1 = 1;
final int vm2 = 2;
final int vm3 = 3;
final String roleA = name + "-A";
final String roleC = name + "-C";
final String roleD = name + "-D";
// assign names to 4 vms...
final String[] requiredRoles = { roleA, roleC, roleD };
final String[] rolesProp = { "", roleA, roleA, roleC + "," + roleD };
final String[][] vmRoles = new String[][] { {}, { roleA }, { roleA }, { roleC, roleD } };
for (int i = 0; i < vmRoles.length; i++) {
final int vm = i;
Host.getHost(0).getVM(vm).invoke(new SerializableRunnable() {
public void run() {
Properties config = new Properties();
config.setProperty(ROLES, rolesProp[vm]);
getSystem(config);
}
});
}
// connect controller to system...
Properties config = new Properties();
config.setProperty(ROLES, "");
getSystem(config);
// create region in controller...
MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.FULL_ACCESS, ResumptionAction.NONE);
AttributesFactory fac = new AttributesFactory();
fac.setMembershipAttributes(ra);
fac.setScope(Scope.DISTRIBUTED_ACK);
RegionAttributes attr = fac.create();
final Region region = createRootRegion(name, attr);
// wait for memberTimeout to expire
waitForMemberTimeout();
// assert in state of role loss... test all are missing according to RequiredRoles
assertMissingRoles(name, requiredRoles);
// create thread to call waitForRequiredRoles
Runnable runWaitForRequiredRoles = new Runnable() {
public void run() {
startTestWaitForRequiredRoles = true;
try {
rolesTestWaitForRequiredRoles = RequiredRoles.waitForRequiredRoles(region, -1);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
failTestWaitForRequiredRoles = true;
}
finishTestWaitForRequiredRoles = true;
}
};
// assert thread is waiting
Thread threadA = new Thread(group, runWaitForRequiredRoles);
threadA.start();
WaitCriterion ev = new WaitCriterion() {
public boolean done() {
return RequiredRolesDUnitTest.this.startTestWaitForRequiredRoles;
}
public String description() {
return "waiting for test start";
}
};
Wait.waitForCriterion(ev, 60 * 1000, 200, true);
assertTrue(this.startTestWaitForRequiredRoles);
assertFalse(this.finishTestWaitForRequiredRoles);
// create region in vms and assert impact on threadA
SerializableRunnable create = new CacheSerializableRunnable("Create Region") {
public void run2() throws CacheException {
AttributesFactory fac = new AttributesFactory();
fac.setScope(Scope.DISTRIBUTED_ACK);
RegionAttributes attr = fac.create();
createRootRegion(name, attr);
}
};
// create region in vm0... no gain for no role
Host.getHost(0).getVM(vm0).invoke(create);
assertFalse(this.finishTestWaitForRequiredRoles);
// create region in vm1... gain for 1st instance of redundant role
Host.getHost(0).getVM(vm1).invoke(create);
assertFalse(this.finishTestWaitForRequiredRoles);
// create region in vm2... no gain for 2nd instance of redundant role
Host.getHost(0).getVM(vm2).invoke(create);
assertFalse(this.finishTestWaitForRequiredRoles);
// create region in vm3... gain for 2 roles
Host.getHost(0).getVM(vm3).invoke(create);
ThreadUtils.join(threadA, 30 * 1000);
assertTrue(this.finishTestWaitForRequiredRoles);
assertTrue(this.rolesTestWaitForRequiredRoles.isEmpty());
// assert loss is fired...
SerializableRunnable destroy = new CacheSerializableRunnable("Destroy Region") {
public void run2() throws CacheException {
Region region = getRootRegion(name);
region.localDestroyRegion();
}
};
// destroy region in vm0... no loss of any role
Host.getHost(0).getVM(vm0).invoke(destroy);
// assert new call to RequiredRoles doesn't wait (no role in vm0)
this.startTestWaitForRequiredRoles = false;
this.finishTestWaitForRequiredRoles = false;
threadA = new Thread(group, runWaitForRequiredRoles);
threadA.start();
ThreadUtils.join(threadA, 30 * 1000);
assertTrue(this.startTestWaitForRequiredRoles);
assertTrue(this.finishTestWaitForRequiredRoles);
assertTrue(this.rolesTestWaitForRequiredRoles.isEmpty());
// destroy region in vm1... nothing happens in 1st removal of redundant role
Host.getHost(0).getVM(vm1).invoke(destroy);
// assert new call to RequiredRoles doesn't wait (redundant role in vm1)
this.startTestWaitForRequiredRoles = false;
this.finishTestWaitForRequiredRoles = false;
threadA = new Thread(group, runWaitForRequiredRoles);
threadA.start();
ThreadUtils.join(threadA, 30 * 1000);
assertTrue(this.startTestWaitForRequiredRoles);
assertTrue(this.finishTestWaitForRequiredRoles);
assertTrue(this.rolesTestWaitForRequiredRoles.isEmpty());
// destroy region in vm2... 2nd removal of redundant role is loss
Host.getHost(0).getVM(vm2).invoke(destroy);
// assert new call to RequiredRoles does wait (lost role in vm2)
this.startTestWaitForRequiredRoles = false;
this.finishTestWaitForRequiredRoles = false;
threadA = new Thread(group, runWaitForRequiredRoles);
threadA.start();
// assert thread is waiting
ev = new WaitCriterion() {
public boolean done() {
return RequiredRolesDUnitTest.this.startTestWaitForRequiredRoles;
}
public String description() {
return "waiting for test start";
}
};
Wait.waitForCriterion(ev, 60 * 1000, 200, true);
assertTrue(this.startTestWaitForRequiredRoles);
assertFalse(this.finishTestWaitForRequiredRoles);
assertMissingRoles(name, vmRoles[vm2]);
// end the wait and make sure no roles are missing
Host.getHost(0).getVM(vm2).invoke(create);
ThreadUtils.join(threadA, 30 * 1000);
assertTrue(this.startTestWaitForRequiredRoles);
assertTrue(this.finishTestWaitForRequiredRoles);
assertTrue(this.rolesTestWaitForRequiredRoles.isEmpty());
assertMissingRoles(name, new String[] {});
assertFalse(failTestWaitForRequiredRoles);
}
use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class SearchAndLoadDUnitTest method testNetSearch.
@Test
public void testNetSearch() throws CacheException, InterruptedException {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
final String name = this.getUniqueName() + "-ACK";
final String objectName = "NetSearchKey";
final Integer value = new Integer(440);
vm0.invoke(new SerializableRunnable("Create ACK Region") {
public void run() {
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setEarlyAck(false);
factory.setStatisticsEnabled(true);
Region region = createRegion(name, factory.create());
region.create(objectName, null);
} catch (CacheException ex) {
Assert.fail("While creating ACK region", ex);
}
}
});
vm1.invoke(new SerializableRunnable("Create ACK Region") {
public void run() {
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setEarlyAck(false);
factory.setStatisticsEnabled(true);
Region region = createRegion(name, factory.create());
region.put(objectName, value);
} catch (CacheException ex) {
Assert.fail("While creating ACK region", ex);
}
}
});
vm2.invoke(new SerializableRunnable("Create ACK Region") {
public void run() {
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setEarlyAck(false);
factory.setStatisticsEnabled(true);
Region region = createRegion(name, factory.create());
region.create(objectName, null);
} catch (CacheException ex) {
Assert.fail("While creating ACK region", ex);
}
}
});
vm0.invoke(new SerializableRunnable("Get a value") {
public void run() {
try {
Object result = null;
result = getRootRegion().getSubregion(name).get(objectName);
assertEquals(value, result);
// System.err.println("Results is " + result.toString() + " Key is " +
// objectName.toString());
} catch (CacheLoaderException cle) {
Assert.fail("While Get a value", cle);
} catch (TimeoutException te) {
Assert.fail("While Get a value", te);
}
}
});
}
use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class SearchAndLoadDUnitTest method testEmptyNetLoad.
/**
* Confirm that a netLoad that returns null will NOT allow other netLoad methods to be called.
*/
@Test
public void testEmptyNetLoad() throws CacheException, InterruptedException {
disconnectAllFromDS();
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
final String name = this.getUniqueName() + "-ACK";
final String objectName = "B";
loaderInvoked = false;
remoteLoaderInvoked = false;
remoteLoaderInvokedCount = 0;
vm0.invoke(new SerializableRunnable("Create ACK Region") {
public void run() {
loaderInvoked = false;
remoteLoaderInvoked = false;
remoteLoaderInvokedCount = 0;
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setEarlyAck(false);
// factory.setCacheLoader(new CacheLoader() {
// public Object load(LoaderHelper helper) {
/// loaderInvoked = true;
// return value;
// }
//
// public void close() {
//
// }
// });
Region region = createRegion(name, factory.create());
region.create(objectName, null);
} catch (CacheException ex) {
Assert.fail("While creating ACK region", ex);
}
}
});
SerializableRunnable installLoader = new SerializableRunnable("Create ACK Region") {
public void run() {
loaderInvoked = false;
remoteLoaderInvoked = false;
remoteLoaderInvokedCount = 0;
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setEarlyAck(false);
factory.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) {
remoteLoaderInvoked = true;
remoteLoaderInvokedCount++;
return null;
}
public void close() {
}
});
createRegion(name, factory.create());
} catch (CacheException ex) {
Assert.fail("While creating ACK region", ex);
}
}
};
vm1.invoke(installLoader);
vm2.invoke(installLoader);
vm0.invoke(new SerializableRunnable("Get a value from remote loader") {
public void run() {
for (int i = 0; i < 1; i++) {
try {
Object result = getRootRegion().getSubregion(name).get(objectName);
assertEquals(null, result);
assertEquals(false, loaderInvoked);
// getRootRegion().getSubregion(name).invalidate(objectName);
} catch (CacheLoaderException cle) {
Assert.fail("While getting value for ACK region", cle);
}/*
* catch(EntryNotFoundException enfe) { fail("While getting value for ACK region", enfe);
*
* }
*/
catch (TimeoutException te) {
Assert.fail("While getting value for ACK region", te);
}
}
}
});
// we only invoke one netLoad loader even when they return null.
boolean xor = vmRemoteLoaderInvoked(vm1) ^ vmRemoteLoaderInvoked(vm2);
assertEquals("vm1=" + vmRemoteLoaderInvoked(vm1) + " vm2=" + vmRemoteLoaderInvoked(vm2) + " vm1Count=" + vmRemoteLoaderInvokedCount(vm1) + " vm2Count=" + vmRemoteLoaderInvokedCount(vm2), true, xor);
int total = vmRemoteLoaderInvokedCount(vm1) + vmRemoteLoaderInvokedCount(vm2);
assertEquals("vm1=" + vmRemoteLoaderInvokedCount(vm1) + " vm2=" + vmRemoteLoaderInvokedCount(vm2), 1, total);
}
use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class HostedLocatorsDUnitTest method testGetAllHostedLocatorsUsingPortZero.
@Test
public void testGetAllHostedLocatorsUsingPortZero() throws Exception {
final InternalDistributedSystem system = getSystem();
final String dunitLocator = system.getConfig().getLocators();
assertNotNull(dunitLocator);
assertFalse(dunitLocator.isEmpty());
// This will eventually contain the ports used by locators
final int[] ports = new int[] { 0, 0, 0, 0 };
final String uniqueName = getUniqueName();
for (int i = 0; i < 4; i++) {
final int whichvm = i;
Integer port = (Integer) Host.getHost(0).getVM(whichvm).invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
try {
System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "locators", dunitLocator);
System.setProperty(DistributionConfig.GEMFIRE_PREFIX + MCAST_PORT, "0");
final String name = uniqueName + "-" + whichvm;
final File subdir = new File(name);
subdir.mkdir();
assertTrue(subdir.exists() && subdir.isDirectory());
final Builder builder = new Builder().setMemberName(name).setPort(ports[whichvm]).setRedirectOutput(true).setWorkingDirectory(name);
launcher = builder.build();
assertEquals(Status.ONLINE, launcher.start().getStatus());
waitForLocatorToStart(launcher, TIMEOUT_MILLISECONDS, 10, true);
return launcher.getPort();
} finally {
System.clearProperty(DistributionConfig.GEMFIRE_PREFIX + "locators");
System.clearProperty(DistributionConfig.GEMFIRE_PREFIX + MCAST_PORT);
}
}
});
ports[i] = port;
}
final String host = SocketCreator.getLocalHost().getHostAddress();
final Set<String> locators = new HashSet<String>();
locators.add(host + "[" + dunitLocator.substring(dunitLocator.indexOf("[") + 1, dunitLocator.indexOf("]")) + "]");
for (int port : ports) {
locators.add(host + "[" + port + "]");
}
// validation within non-locator
final DistributionManager dm = (DistributionManager) system.getDistributionManager();
final Set<InternalDistributedMember> locatorIds = dm.getLocatorDistributionManagerIds();
assertEquals(5, locatorIds.size());
final Map<InternalDistributedMember, Collection<String>> hostedLocators = dm.getAllHostedLocators();
assertTrue(!hostedLocators.isEmpty());
assertEquals(5, hostedLocators.size());
for (InternalDistributedMember member : hostedLocators.keySet()) {
assertEquals(1, hostedLocators.get(member).size());
final String hostedLocator = hostedLocators.get(member).iterator().next();
assertTrue(locators + " does not contain " + hostedLocator, locators.contains(hostedLocator));
}
// validate fix for #46324
for (int whichvm = 0; whichvm < 4; whichvm++) {
Host.getHost(0).getVM(whichvm).invoke(new SerializableRunnable() {
@Override
public void run() {
final DistributionManager dm = (DistributionManager) InternalDistributedSystem.getAnyInstance().getDistributionManager();
final InternalDistributedMember self = dm.getDistributionManagerId();
final Set<InternalDistributedMember> locatorIds = dm.getLocatorDistributionManagerIds();
assertTrue(locatorIds.contains(self));
final Map<InternalDistributedMember, Collection<String>> hostedLocators = dm.getAllHostedLocators();
assertTrue("hit bug #46324: " + hostedLocators + " is missing " + InternalLocator.getLocatorStrings() + " for " + self, hostedLocators.containsKey(self));
}
});
}
// validation with locators
for (int whichvm = 0; whichvm < 4; whichvm++) {
Host.getHost(0).getVM(whichvm).invoke(new SerializableRunnable() {
@Override
public void run() {
final DistributionManager dm = (DistributionManager) InternalDistributedSystem.getAnyInstance().getDistributionManager();
final Set<InternalDistributedMember> locatorIds = dm.getLocatorDistributionManagerIds();
assertEquals(5, locatorIds.size());
final Map<InternalDistributedMember, Collection<String>> hostedLocators = dm.getAllHostedLocators();
assertTrue(!hostedLocators.isEmpty());
assertEquals(5, hostedLocators.size());
for (InternalDistributedMember member : hostedLocators.keySet()) {
assertEquals(1, hostedLocators.get(member).size());
final String hostedLocator = hostedLocators.get(member).iterator().next();
assertTrue(locators + " does not contain " + hostedLocator, locators.contains(hostedLocator));
}
}
});
}
}
use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class DistributedMemberDUnitTest method testGroupsInAllVMs.
/**
* Tests the configuration of one unique group in each of four vms. Verifies that each vm is aware
* of the other vms' groups.
*/
@Test
public void testGroupsInAllVMs() {
// or assertion on # members fails when run-dunit-tests
disconnectFromDS();
// connect all four vms...
for (int i = 0; i < 4; i++) {
final int vm = i;
Host.getHost(0).getVM(vm).invoke(new SerializableRunnable() {
public void run() {
// disconnectFromDS();
Properties config = new Properties();
config.setProperty(GROUPS, makeGroupsString(vm));
getSystem(config);
}
});
}
// validate group from each vm...
for (int i = 0; i < 4; i++) {
final int vm = i;
Host.getHost(0).getVM(vm).invoke(new SerializableRunnable() {
public void run() {
InternalDistributedSystem sys = getSystem();
final String expectedMyGroup = makeGroupsString(vm);
assertEquals(expectedMyGroup, sys.getConfig().getGroups());
DM dm = sys.getDistributionManager();
DistributedMember self = sys.getDistributedMember();
List<String> myGroups = self.getGroups();
assertEquals(Arrays.asList("" + vm, makeOddEvenString(vm)), myGroups);
Set<DistributedMember> members = null;
for (int i = 1; i <= 3; i++) {
try {
members = dm.getOtherNormalDistributionManagerIds();
assertEquals(3, members.size());
break;
} catch (AssertionError e) {
// TODO: delete this
if (i < 3) {
sleep(200);
} else {
throw e;
}
}
}
// Make sure getAllOtherMembers returns a set
// containing our three peers plus an admin member.
Set<DistributedMember> others = sys.getAllOtherMembers();
assertEquals(4, others.size());
others.removeAll(dm.getOtherNormalDistributionManagerIds());
assertEquals(1, others.size());
// test getGroupMembers
HashSet<DistributedMember> evens = new HashSet<DistributedMember>();
HashSet<DistributedMember> odds = new HashSet<DistributedMember>();
boolean isEvens = true;
for (String groupName : Arrays.asList("0", "1", "2", "3")) {
Set<DistributedMember> gm = sys.getGroupMembers(groupName);
if (isEvens) {
evens.addAll(gm);
} else {
odds.addAll(gm);
}
isEvens = !isEvens;
if (groupName.equals("" + vm)) {
assertEquals(Collections.singleton(self), gm);
} else {
assertEquals(1, gm.size());
assertEquals("members=" + members + " gm=" + gm, true, members.removeAll(gm));
}
}
assertEquals(Collections.emptySet(), members);
assertEquals(evens, sys.getGroupMembers("EVENS"));
assertEquals(odds, sys.getGroupMembers("ODDS"));
}
});
}
}
Aggregations