+ integration test forking a VM for a cache server to test the CQ functionality
This commit is contained in:
Costin Leau
2011-08-19 15:00:11 +03:00
parent 2d0d148bc4
commit da8dfd30d7
9 changed files with 358 additions and 68 deletions

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Utility for forking Java processes.
*
* @author Costin Leau
*/
public class ForkUtil {
public static OutputStream cloneJVM(String argument) {
String cp = System.getProperty("java.class.path");
String home = System.getProperty("java.home");
Process proc = null;
String java = home + "\\bin\\java ";
String argCp = "-cp " + cp;
String argClass = argument;
String cmd = java + argCp + " " + argClass;
try {
//ProcessBuilder builder = new ProcessBuilder(cmd, argCp, argClass);
//builder.redirectErrorStream(true);
proc = Runtime.getRuntime().exec(cmd);
} catch (IOException ioe) {
throw new IllegalStateException("Cannot start command " + cmd, ioe);
}
System.out.println("Started fork");
final Process p = proc;
final BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
final AtomicBoolean run = new AtomicBoolean(true);
Thread reader = new Thread(new Runnable() {
public void run() {
try {
String line = null;
do {
while ((line = br.readLine()) != null) {
System.out.println("[FORK] " + line);
}
Thread.sleep(200);
} while (run.get());
} catch (Exception ex) {
// ignore and exit
}
}
});
reader.start();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("Stopping fork...");
run.set(false);
if (p != null)
p.destroy();
try {
p.waitFor();
} catch (InterruptedException e) {
// ignore
}
System.out.println("Fork stopped");
}
});
return proc.getOutputStream();
}
public static OutputStream cacheServer() {
OutputStream os = cloneJVM("org.springframework.data.gemfire.fork.CacheServerProcess");
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
// ignore and move on
}
return os;
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.fork;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import com.gemstone.gemfire.cache.AttributesFactory;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.Scope;
import com.gemstone.gemfire.cache.server.CacheServer;
import com.gemstone.gemfire.distributed.DistributedSystem;
/**
* @author Costin Leau
*/
public class CacheServerProcess {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.setProperty("name", "CqServer");
props.setProperty("log-level", "warning");
System.out.println("\nConnecting to the distributed system and creating the cache.");
DistributedSystem ds = DistributedSystem.connect(props);
Cache cache = CacheFactory.create(ds);
// Create region.
AttributesFactory factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.REPLICATE);
factory.setScope(Scope.DISTRIBUTED_ACK);
Region testRegion = cache.createRegion("test", factory.create());
System.out.println("Test region, " + testRegion.getFullPath() + ", created in cache.");
// Start Cache Server.
CacheServer server = cache.addCacheServer();
server.setPort(40404);
server.setNotifyBySubscription(true);
server.start();
System.out.println("Waiting for signal");
// wait for signal
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
bufferedReader.readLine();
System.out.println("Received signal");
testRegion.put("one", 1);
testRegion.put("two", 2);
testRegion.put("three", 3);
System.out.println("Waiting for shutdown");
bufferedReader.readLine();
}
}

View File

@@ -0,0 +1,127 @@
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.listener;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.data.gemfire.ForkUtil;
import org.springframework.data.gemfire.listener.adapter.QueryListenerAdapter;
import com.gemstone.gemfire.cache.RegionService;
import com.gemstone.gemfire.cache.client.Pool;
import com.gemstone.gemfire.cache.client.PoolFactory;
import com.gemstone.gemfire.cache.client.PoolManager;
import com.gemstone.gemfire.cache.query.CqEvent;
/**
* @author Costin Leau
*/
public class ListenerContainerTests {
private final BlockingDeque<CqEvent> bag = new LinkedBlockingDeque<CqEvent>();
protected QueryListenerContainer container;
private static RegionService cache = null;
private static Pool pool = null;
private static OutputStream os = null;
private final Object handler = new Object() {
public void handleEvent(CqEvent event) {
bag.add(event);
}
};
private final QueryListenerAdapter adapter = new QueryListenerAdapter(handler);
@BeforeClass
public static void startUp() throws Exception {
os = ForkUtil.cacheServer();
Properties props = new Properties();
props.put("mcast-port", "0");
props.put("name", "cq-client");
props.put("log-level", "warning");
CacheFactoryBean cacheFB = new CacheFactoryBean();
cacheFB.setBeanName("gemfire-cache");
cacheFB.setUseBeanFactoryLocator(false);
cacheFB.setProperties(props);
cacheFB.afterPropertiesSet();
cache = cacheFB.getObject();
PoolFactory pf = PoolManager.createFactory();
pf.addServer("localhost", 40404);
pf.setSubscriptionEnabled(true);
pool = pf.create("client");
}
@AfterClass
public static void cleanUp() {
sendSignal();
if (pool != null) {
pool.destroy();
pool = null;
}
if (cache != null) {
cache.close();
}
cache = null;
}
@Before
public void setUp() throws Exception {
String query = "SELECT * from /test";
container = new QueryListenerContainer();
container.setQueryService(pool.getQueryService());
container.setBeanName("container");
container.addListener(new CqQueryDefinition("test", query, adapter));
container.afterPropertiesSet();
}
private static void sendSignal() {
try {
os.write("\n".getBytes());
os.flush();
} catch (IOException ex) {
throw new IllegalStateException("Cannot communicate with forked VM", ex);
}
}
@Test
public void testContainer() throws Exception {
sendSignal();
Thread.sleep(3000);
System.out.println("Bag is " + bag);
sendSignal();
}
}