use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class QueueCommandsDUnitTest method testCreateUpdatesSharedConfig.
/**
* Asserts that creating async event queues correctly updates the shared configuration.
*/
// GEODE-1976
@Category(FlakyTest.class)
@Test
public void testCreateUpdatesSharedConfig() throws IOException {
disconnectAllFromDS();
final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(2);
jmxPort = ports[0];
httpPort = ports[1];
try {
jmxHost = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException ignore) {
jmxHost = "localhost";
}
final String queueName = "testAsyncEventQueueQueue";
final String groupName = "testAsyncEventQueueSharedConfigGroup";
final Properties locatorProps = new Properties();
locatorProps.setProperty(NAME, "Locator");
locatorProps.setProperty(MCAST_PORT, "0");
locatorProps.setProperty(LOG_LEVEL, "fine");
locatorProps.setProperty(ENABLE_CLUSTER_CONFIGURATION, "true");
locatorProps.setProperty(JMX_MANAGER, "true");
locatorProps.setProperty(JMX_MANAGER_START, "true");
locatorProps.setProperty(JMX_MANAGER_BIND_ADDRESS, String.valueOf(jmxHost));
locatorProps.setProperty(JMX_MANAGER_PORT, String.valueOf(jmxPort));
locatorProps.setProperty(HTTP_SERVICE_PORT, String.valueOf(httpPort));
// Start the Locator and wait for shared configuration to be available
final int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {
@Override
public void run() {
final File locatorLogFile = new File("locator-" + locatorPort + ".log");
try {
final InternalLocator locator = (InternalLocator) Locator.startLocatorAndDS(locatorPort, locatorLogFile, null, locatorProps);
WaitCriterion wc = new WaitCriterion() {
@Override
public boolean done() {
return locator.isSharedConfigurationRunning();
}
@Override
public String description() {
return "Waiting for shared configuration to be started";
}
};
waitForCriterion(wc, 5000, 500, true);
} catch (IOException ioex) {
fail("Unable to create a locator with a shared configuration");
}
}
});
connect(jmxHost, jmxPort, httpPort, getDefaultShell());
// Create a cache in VM 1
VM vm = Host.getHost(0).getVM(1);
vm.invoke(new SerializableRunnable() {
@Override
public void run() {
Properties localProps = new Properties();
localProps.setProperty(MCAST_PORT, "0");
localProps.setProperty(LOCATORS, "localhost[" + locatorPort + "]");
localProps.setProperty(GROUPS, groupName);
getSystem(localProps);
assertNotNull(getCache());
}
});
// Deploy a JAR file with an AsyncEventListener that can be instantiated on each server
final File jarFile = new File(new File(".").getAbsolutePath(), "QueueCommandsDUnit.jar");
QueueCommandsDUnitTest.this.filesToBeDeleted.add(jarFile.getAbsolutePath());
ClassBuilder classBuilder = new ClassBuilder();
byte[] jarBytes = classBuilder.createJarFromClassContent("com/qcdunit/QueueCommandsDUnitTestListener", "package com.qcdunit;" + "import java.util.List; import java.util.Properties;" + "import org.apache.geode.internal.cache.xmlcache.Declarable2; import org.apache.geode.cache.asyncqueue.AsyncEvent;" + "import org.apache.geode.cache.asyncqueue.AsyncEventListener;" + "public class QueueCommandsDUnitTestListener implements Declarable2, AsyncEventListener {" + "Properties props;" + "public boolean processEvents(List<AsyncEvent> events) { return true; }" + "public void close() {}" + "public void init(final Properties props) {this.props = props;}" + "public Properties getConfig() {return this.props;}}");
writeJarBytesToFile(jarFile, jarBytes);
CommandResult cmdResult = executeCommand("deploy --jar=QueueCommandsDUnit.jar");
assertEquals(Result.Status.OK, cmdResult.getStatus());
// Test creating the queue
CommandStringBuilder commandStringBuilder = new CommandStringBuilder(CliStrings.CREATE_ASYNC_EVENT_QUEUE);
commandStringBuilder.addOption(CliStrings.CREATE_ASYNC_EVENT_QUEUE__ID, queueName);
commandStringBuilder.addOption(CliStrings.CREATE_ASYNC_EVENT_QUEUE__GROUP, groupName);
commandStringBuilder.addOption(CliStrings.CREATE_ASYNC_EVENT_QUEUE__LISTENER, "com.qcdunit.QueueCommandsDUnitTestListener");
cmdResult = executeCommand(commandStringBuilder.toString());
assertEquals(Result.Status.OK, cmdResult.getStatus());
// Make sure the queue exists in the shared config
Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {
@Override
public void run() {
ClusterConfigurationService sharedConfig = ((InternalLocator) Locator.getLocator()).getSharedConfiguration();
String xmlFromConfig;
try {
xmlFromConfig = sharedConfig.getConfiguration(groupName).getCacheXmlContent();
assertTrue(xmlFromConfig.contains(queueName));
} catch (Exception e) {
fail("Error occurred in cluster configuration service", e);
}
}
});
// Close cache in the vm1 and restart it to get the shared configuration
vm = Host.getHost(0).getVM(1);
vm.invoke(new SerializableRunnable() {
@Override
public void run() {
Cache cache = getCache();
assertNotNull(cache);
cache.close();
assertTrue(cache.isClosed());
Properties localProps = new Properties();
localProps.setProperty(MCAST_PORT, "0");
localProps.setProperty(LOCATORS, "localhost[" + locatorPort + "]");
localProps.setProperty(GROUPS, groupName);
localProps.setProperty(USE_CLUSTER_CONFIGURATION, "true");
getSystem(localProps);
cache = getCache();
assertNotNull(cache);
AsyncEventQueue aeq = cache.getAsyncEventQueue(queueName);
assertNotNull(aeq);
}
});
}
use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class DiskRegionDUnitTest method testRemoteUpdates.
/**
* Makes sure that updates from other VMs cause existing entries to be written to disk.
*/
@Test
public void testRemoteUpdates() throws Exception {
final String name = this.getUniqueName();
SerializableRunnable create = new CacheSerializableRunnable("Create region") {
public void run2() throws CacheException {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_NO_ACK);
factory.setEvictionAttributes(EvictionAttributes.createLRUMemoryAttributes(2, null, EvictionAction.OVERFLOW_TO_DISK));
File d = new File("DiskRegions" + OSProcess.getId());
d.mkdirs();
DiskStoreFactory dsf = getCache().createDiskStoreFactory();
dsf.setDiskDirs(new File[] { d });
DiskStore ds = dsf.create(name);
factory.setDiskStoreName(ds.getName());
createRegion(name, factory.create());
}
};
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
vm0.invoke(create);
vm1.invoke(create);
vm0.invoke(new CacheSerializableRunnable("Fill Region") {
public void run2() throws CacheException {
LocalRegion region = (LocalRegion) getRootRegion().getSubregion(name);
// DiskRegion dr = region.getDiskRegion();
LRUStatistics lruStats = getLRUStats(region);
int i;
for (i = 0; lruStats.getEvictions() <= 0; i++) {
region.put(new Integer(i), new short[250]);
}
assertTrue(i > 5);
}
});
vm1.invoke(new CacheSerializableRunnable("Update Region") {
public void run2() throws CacheException {
LocalRegion region = (LocalRegion) getRootRegion().getSubregion(name);
// LRUStatistics lruStats = getLRUStats(region);
for (int i = 0; i < 10; i++) {
region.put(new Integer(i), new int[250]);
}
}
});
vm0.invoke(new CacheSerializableRunnable("Verify overflow") {
public void run2() throws CacheException {
LocalRegion region = (LocalRegion) getRootRegion().getSubregion(name);
// DiskRegion dr = region.getDiskRegion();
final LRUStatistics lruStats = getLRUStats(region);
WaitCriterion ev = new WaitCriterion() {
public boolean done() {
return lruStats.getEvictions() > 6;
}
public String description() {
return "waiting for evictions to exceed 6";
}
};
Wait.waitForCriterion(ev, 5 * 1000, 200, true);
// DiskRegionStats diskStats = dr.getStats();
// assertTrue(diskStats.getWrites() > 6);
}
});
vm0.invoke(new CacheSerializableRunnable("Populate with byte[]") {
public void run2() throws CacheException {
LocalRegion region = (LocalRegion) getRootRegion().getSubregion(name);
// LRUStatistics lruStats = getLRUStats(region);
for (int i = 0; i < 10000; i++) {
region.put(String.valueOf(i), String.valueOf(i).getBytes());
}
}
});
vm1.invoke(new CacheSerializableRunnable("Get with byte[]") {
public void run2() throws CacheException {
LocalRegion region = (LocalRegion) getRootRegion().getSubregion(name);
// LRUStatistics lruStats = getLRUStats(region);
for (int i = 0; i < 10000; i++) {
byte[] bytes = (byte[]) region.get(String.valueOf(i));
assertEquals(String.valueOf(i), new String(bytes));
}
}
});
}
use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class ClientMembershipDUnitTest method testClientMembershipEventsInServer.
/**
* Tests notification of events in server process. Bridge servers detect client joins when the
* client connects to the server.
*/
@Test
public void testClientMembershipEventsInServer() throws Exception {
final boolean[] fired = new boolean[3];
final DistributedMember[] member = new DistributedMember[3];
final String[] memberId = new String[3];
final boolean[] isClient = new boolean[3];
// create and register ClientMembershipListener in controller vm...
ClientMembershipListener listener = new ClientMembershipListener() {
public void memberJoined(ClientMembershipEvent event) {
System.out.println("[testClientMembershipEventsInServer] memberJoined: " + event);
fired[JOINED] = true;
member[JOINED] = event.getMember();
memberId[JOINED] = event.getMemberId();
isClient[JOINED] = event.isClient();
assertFalse(fired[LEFT] || fired[CRASHED]);
}
public void memberLeft(ClientMembershipEvent event) {
System.out.println("[testClientMembershipEventsInServer] memberLeft: " + event);
fired[LEFT] = true;
member[LEFT] = event.getMember();
memberId[LEFT] = event.getMemberId();
isClient[LEFT] = event.isClient();
assertFalse(fired[JOINED] || fired[CRASHED]);
}
public void memberCrashed(ClientMembershipEvent event) {
System.out.println("[testClientMembershipEventsInServer] memberCrashed: " + event);
fired[CRASHED] = true;
member[CRASHED] = event.getMember();
memberId[CRASHED] = event.getMemberId();
isClient[CRASHED] = event.isClient();
assertFalse(fired[JOINED] || fired[LEFT]);
}
};
ClientMembership.registerClientMembershipListener(listener);
final VM vm0 = Host.getHost(0).getVM(0);
final String name = this.getUniqueName();
final int[] ports = new int[1];
// create BridgeServer in controller vm...
System.out.println("[testClientMembershipEventsInServer] Create BridgeServer");
getSystem();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
Region region = createRegion(name, factory.create());
assertNotNull(region);
assertNotNull(getRootRegion().getSubregion(name));
ports[0] = startBridgeServer(0);
assertTrue(ports[0] != 0);
DistributedMember serverMember = getMemberId();
String serverMemberId = serverMember.toString();
System.out.println("[testClientMembershipEventsInServer] ports[0]=" + ports[0]);
System.out.println("[testClientMembershipEventsInServer] serverMemberId=" + serverMemberId);
System.out.println("[testClientMembershipEventsInServer] serverMember=" + serverMember);
assertFalse(fired[JOINED]);
assertNull(member[JOINED]);
assertNull(memberId[JOINED]);
assertFalse(isClient[JOINED]);
assertFalse(fired[LEFT]);
assertNull(member[LEFT]);
assertNull(memberId[LEFT]);
assertFalse(isClient[LEFT]);
assertFalse(fired[CRASHED]);
assertNull(member[CRASHED]);
assertNull(memberId[CRASHED]);
assertFalse(isClient[CRASHED]);
// sanity check...
System.out.println("[testClientMembershipEventsInServer] sanity check");
DistributedMember test = new TestDistributedMember("test");
InternalClientMembership.notifyClientJoined(test);
Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
return fired[JOINED] || fired[LEFT] || fired[CRASHED];
});
assertTrue(fired[JOINED]);
assertEquals(test, member[JOINED]);
assertEquals(test.getId(), memberId[JOINED]);
assertTrue(isClient[JOINED]);
assertFalse(fired[LEFT]);
assertNull(member[LEFT]);
assertNull(memberId[LEFT]);
assertFalse(isClient[LEFT]);
assertFalse(fired[CRASHED]);
assertNull(member[CRASHED]);
assertNull(memberId[CRASHED]);
assertFalse(isClient[CRASHED]);
resetArraysForTesting(fired, member, memberId, isClient);
final Host host = Host.getHost(0);
SerializableCallable createConnectionPool = new SerializableCallable("Create connectionPool") {
public Object call() {
System.out.println("[testClientMembershipEventsInServer] create bridge client");
Properties config = new Properties();
config.setProperty(MCAST_PORT, "0");
config.setProperty(LOCATORS, "");
config.setProperty(ENABLE_NETWORK_PARTITION_DETECTION, "false");
properties = config;
DistributedSystem s = getSystem(config);
AttributesFactory factory = new AttributesFactory();
Pool pool = ClientServerTestCase.configureConnectionPool(factory, NetworkUtils.getServerHostName(host), ports, true, -1, 2, null);
createRegion(name, factory.create());
assertNotNull(getRootRegion().getSubregion(name));
// see geode-1078
assertTrue(s == basicGetSystem());
return getMemberId();
}
};
// create bridge client in vm0...
DistributedMember clientMember = (DistributedMember) vm0.invoke(createConnectionPool);
String clientMemberId = clientMember.toString();
Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
return fired[JOINED] || fired[LEFT] || fired[CRASHED];
});
System.out.println("[testClientMembershipEventsInServer] assert server detected client join");
assertTrue(fired[JOINED]);
assertEquals(member[JOINED] + " should equal " + clientMember, clientMember, member[JOINED]);
assertEquals(memberId[JOINED] + " should equal " + clientMemberId, clientMemberId, memberId[JOINED]);
assertTrue(isClient[JOINED]);
assertFalse(fired[LEFT]);
assertNull(member[LEFT]);
assertNull(memberId[LEFT]);
assertFalse(isClient[LEFT]);
assertFalse(fired[CRASHED]);
assertNull(member[CRASHED]);
assertNull(memberId[CRASHED]);
assertFalse(isClient[CRASHED]);
resetArraysForTesting(fired, member, memberId, isClient);
pauseForClientToJoin();
vm0.invoke(new SerializableRunnable("Stop bridge client") {
public void run() {
System.out.println("[testClientMembershipEventsInServer] Stop bridge client");
getRootRegion().getSubregion(name).close();
Map m = PoolManager.getAll();
Iterator mit = m.values().iterator();
while (mit.hasNext()) {
Pool p = (Pool) mit.next();
p.destroy();
}
}
});
Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
return fired[JOINED] || fired[LEFT] || fired[CRASHED];
});
System.out.println("[testClientMembershipEventsInServer] assert server detected client left");
assertFalse(fired[JOINED]);
assertNull(member[JOINED]);
assertNull(memberId[JOINED]);
assertFalse(isClient[JOINED]);
assertTrue(fired[LEFT]);
assertEquals(clientMember, member[LEFT]);
assertEquals(clientMemberId, memberId[LEFT]);
assertTrue(isClient[LEFT]);
assertFalse(fired[CRASHED]);
assertNull(member[CRASHED]);
assertNull(memberId[CRASHED]);
assertFalse(isClient[CRASHED]);
resetArraysForTesting(fired, member, memberId, isClient);
// reconnect bridge client to test for crashed event
clientMemberId = vm0.invoke(createConnectionPool).toString();
Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
return fired[JOINED] || fired[LEFT] || fired[CRASHED];
});
System.out.println("[testClientMembershipEventsInServer] assert server detected client re-join");
assertTrue(fired[JOINED]);
assertEquals(clientMember, member[JOINED]);
assertEquals(clientMemberId, memberId[JOINED]);
assertTrue(isClient[JOINED]);
assertFalse(fired[LEFT]);
assertNull(member[LEFT]);
assertNull(memberId[LEFT]);
assertFalse(isClient[LEFT]);
assertFalse(fired[CRASHED]);
assertNull(member[CRASHED]);
assertNull(memberId[CRASHED]);
assertFalse(isClient[CRASHED]);
resetArraysForTesting(fired, member, memberId, isClient);
pauseForClientToJoin();
ServerConnection.setForceClientCrashEvent(true);
try {
vm0.invoke(new SerializableRunnable("Stop bridge client") {
public void run() {
System.out.println("[testClientMembershipEventsInServer] Stop bridge client");
getRootRegion().getSubregion(name).close();
Map m = PoolManager.getAll();
Iterator mit = m.values().iterator();
while (mit.hasNext()) {
Pool p = (Pool) mit.next();
p.destroy();
}
}
});
Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
return fired[JOINED] || fired[LEFT] || fired[CRASHED];
});
System.out.println("[testClientMembershipEventsInServer] assert server detected client crashed");
assertFalse(fired[JOINED]);
assertNull(member[JOINED]);
assertNull(memberId[JOINED]);
assertFalse(isClient[JOINED]);
assertFalse(fired[LEFT]);
assertNull(member[LEFT]);
assertNull(memberId[LEFT]);
assertFalse(isClient[LEFT]);
assertTrue(fired[CRASHED]);
assertEquals(clientMember, member[CRASHED]);
assertEquals(clientMemberId, memberId[CRASHED]);
assertTrue(isClient[CRASHED]);
} finally {
ServerConnection.setForceClientCrashEvent(false);
}
}
use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class ClearMultiVmDUnitTest method testGiiandClear.
// end of testClearExceptions
@Test
public void testGiiandClear() throws Throwable {
if (false) {
getSystem().getLogWriter().severe("testGiiandClear skipped because of bug 34963");
} else {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
SerializableRunnable create = new CacheSerializableRunnable("create mirrored region") {
public void run2() throws CacheException {
AttributesFactory factory1 = new AttributesFactory();
factory1.setScope(Scope.DISTRIBUTED_ACK);
factory1.setDataPolicy(DataPolicy.REPLICATE);
RegionAttributes attr1 = factory1.create();
mirroredRegion = cache.createRegion("mirrored", attr1);
// reset slow
org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = 0;
}
};
vm0.invoke(create);
vm0.invoke(new CacheSerializableRunnable("put initial data") {
public void run2() throws CacheException {
for (int i = 0; i < 1000; i++) {
mirroredRegion.put(new Integer(i), (new Integer(i)).toString());
}
}
});
// slow down image processing to make it more likely to get async updates
vm1.invoke(new SerializableRunnable("set slow image processing") {
public void run() {
// if this is a no_ack test, then we need to slow down more because of the
// pauses in the nonblocking operations
int pause = 50;
org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = pause;
}
});
// now do the get initial image in vm1
AsyncInvocation async1 = vm1.invokeAsync(create);
// try to time a distributed clear to happen in the middle of gii
vm0.invoke(new SerializableRunnable("call clear when gii") {
public void run() {
try {
Thread.sleep(3 * 1000);
} catch (InterruptedException ex) {
fail("interrupted");
}
mirroredRegion.clear();
assertEquals(0, mirroredRegion.size());
}
});
ThreadUtils.join(async1, 30 * 1000);
if (async1.exceptionOccurred()) {
Assert.fail("async1 failed", async1.getException());
}
SerializableRunnable validate = new CacheSerializableRunnable("validate for region size") {
public void run2() throws CacheException {
assertEquals(0, mirroredRegion.size());
}
};
vm0.invoke(validate);
vm1.invoke(validate);
}
}
use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class DiskRegionDUnitTest method testDistributedInvalidate.
/**
* Tests that invalidates and updates received from different VMs are handled appropriately by
* overflow regions.
*/
@Test
public void testDistributedInvalidate() throws Exception {
final String name = this.getUniqueName();
SerializableRunnable create = new CacheSerializableRunnable("Create region") {
public void run2() throws CacheException {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setEarlyAck(false);
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(100, EvictionAction.OVERFLOW_TO_DISK));
File d = new File("DiskRegions" + OSProcess.getId());
d.mkdirs();
DiskStoreFactory dsf = getCache().createDiskStoreFactory();
dsf.setDiskDirs(new File[] { d });
DiskStore ds = dsf.create(name);
factory.setDiskStoreName(ds.getName());
createRegion(name, factory.create());
}
};
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
vm0.invoke(create);
vm1.invoke(create);
vm0.invoke(new CacheSerializableRunnable("Fill Region") {
public void run2() throws CacheException {
LocalRegion region = (LocalRegion) getRootRegion().getSubregion(name);
// DiskRegion dr = region.getDiskRegion();
LRUStatistics lruStats = getLRUStats(region);
for (int i = 0; lruStats.getEvictions() < 10; i++) {
LogWriterUtils.getLogWriter().info("Put " + i);
region.put(new Integer(i), new byte[1]);
}
assertEquals(10, lruStats.getEvictions());
}
});
final Object key = new Integer(20);
vm1.invoke(new CacheSerializableRunnable("Invalidate entry") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
assertNotNull(region.get(key));
region.invalidate(key);
}
});
vm0.invoke(new CacheSerializableRunnable("Verify invalidate") {
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
WaitCriterion ev = new WaitCriterion() {
public boolean done() {
return region.get(key) == null;
}
public String description() {
return "value for key remains: " + key;
}
};
Wait.waitForCriterion(ev, 500, 200, true);
}
});
final String newValue = "NEW VALUE";
vm1.invoke(new CacheSerializableRunnable("Update entry") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.put(key, newValue);
}
});
vm0.invoke(new CacheSerializableRunnable("Verify update") {
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
WaitCriterion ev = new WaitCriterion() {
public boolean done() {
return newValue.equals(region.get(key));
}
public String description() {
return "verify update";
}
};
Wait.waitForCriterion(ev, 500, 200, true);
}
});
}
Aggregations