Skip to content

Commit

Permalink
refactor as reviews from @mesutcelik for issue hazelcast#3458
Browse files Browse the repository at this point in the history
  • Loading branch information
serkanozal committed Sep 2, 2014
1 parent 4dc9955 commit 59f9d53
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.URL;
import java.util.HashMap;
Expand Down Expand Up @@ -74,6 +75,15 @@ protected enum RequestType {
int g2 = rand.nextInt(255);
int g3 = rand.nextInt(255);
System.setProperty("hazelcast.multicast.group", "224." + g1 + "." + g2 + "." + g3);

try {
final URL root = new URL(TestServlet.class.getResource("/"), "../test-classes");
final String baseDir = new File(root.getFile().replaceAll("%20", " ")).toString();
sourceDir = baseDir + "/../../src/test/webapp";
} catch (MalformedURLException e) {
e.printStackTrace();
throw new IllegalStateException("Couldn't initialize AbstractWebFilterTest");
}
}

protected static final String HAZELCAST_SESSION_ATTRIBUTE_SEPARATOR = "::hz::";
Expand All @@ -85,6 +95,8 @@ protected enum RequestType {
protected static final Map<Class<? extends AbstractWebFilterTest>, ContainerContext> CONTAINER_CONTEXT_MAP =
new HashMap<Class<? extends AbstractWebFilterTest>, ContainerContext>();

protected static final String sourceDir;

protected String serverXml1;
protected String serverXml2;

Expand All @@ -108,9 +120,9 @@ public void setup() throws Exception {
ContainerContext cc = CONTAINER_CONTEXT_MAP.get(getClass());
// If container is not exist yet or
// Hazelcast instance is not active (because of such as server shutdown)
if (cc == null || isInstanceNotActive(cc.hz)) {
if (cc == null) {
// Build a new instance
buildInstance();
ensureInstanceIsUp();
CONTAINER_CONTEXT_MAP.put(
getClass(),
new ContainerContext(
Expand All @@ -122,43 +134,54 @@ public void setup() throws Exception {
server1,
server2,
hz));
// If there is no previously created container
// Instance maybe shutdown but container may be created before.
// So "onTestStart" method must be called only for case that there is no container
// Otherwise, it maybe called several times
// since this code block is executed for every instance creation.
if (cc == null) {
onTestStart();
}
// New container created and started
onContainerStart();
} else {
// For every test method a different test class can be constructed for parallel runs by JUnit.
// So container can be exist, but configurations of current test may not be exist.
// For this reason, we should copy container context information (such as ports, servers, ...)
// to current test.
cc.copyInto(this);

// Ensure that instance is up and running
ensureInstanceIsUp();

// After ensuring that system is up, new containers or instance may be created.
// So, we should copy current information from test to current context.
cc.copyFrom(this);
}
// Clear map
IMap<String, Object> map = hz.getMap(DEFAULT_MAP_NAME);
map.clear();
}

protected void buildInstance() throws Exception {
final URL root = new URL(TestServlet.class.getResource("/"), "../test-classes");
final String baseDir = new File(root.getFile().replaceAll("%20", " ")).toString();
final String sourceDir = baseDir + "/../../src/test/webapp";
hz = Hazelcast.newHazelcastInstance(
new FileSystemXmlConfig(new File(sourceDir + "/WEB-INF/", "hazelcast.xml")));
serverPort1 = availablePort();
server1 = getServletContainer(serverPort1, sourceDir, serverXml1);
protected void ensureInstanceIsUp() throws Exception {
if (isInstanceNotActive(hz)) {
hz = Hazelcast.newHazelcastInstance(
new FileSystemXmlConfig(new File(sourceDir + "/WEB-INF/", "hazelcast.xml")));
}
if (serverXml1 != null) {
if (server1 == null) {
serverPort1 = availablePort();
server1 = getServletContainer(serverPort1, sourceDir, serverXml1);
}
else if (!server1.isRunning()) {
server1.start();
}
}
if (serverXml2 != null) {
serverPort2 = availablePort();
server2 = getServletContainer(serverPort2, sourceDir, serverXml2);
if (server2 == null) {
serverPort2 = availablePort();
server2 = getServletContainer(serverPort2, sourceDir, serverXml2);
}
else if (!server2.isRunning()) {
server2.start();
}
}
}

protected boolean isInstanceNotActive(HazelcastInstance hz) {
if (hz == null) {
return true;
}
Node node = TestUtil.getNode(hz);
if (node == null) {
return true;
Expand All @@ -173,10 +196,6 @@ public static void teardownClass() throws Exception {
CONTAINER_CONTEXT_MAP.entrySet()) {
ContainerContext cc = ccEntry.getValue();
try {
// Call "onTestFinish" methods of all tests.
if (cc.test != null) {
cc.test.onTestFinish();
}
// Stop servers
cc.server1.stop();
if (cc.server2 != null) {
Expand All @@ -190,21 +209,6 @@ public static void teardownClass() throws Exception {
Hazelcast.shutdownAll();
}

// Called on test class startup
protected void onTestStart() {

}

// Called on every container/hz instance creation
protected void onContainerStart() {

}

// Called on test class terminate
protected void onTestFinish() {

}

protected int availablePort() throws IOException {
while (true) {
int port = (int) (65536 * Math.random());
Expand Down Expand Up @@ -317,6 +321,16 @@ protected void copyInto(AbstractWebFilterTest awft) {
awft.hz = hz;
}

protected void copyFrom(AbstractWebFilterTest awft) {
serverXml1 = awft.serverXml1;
serverXml2 = awft.serverXml2;
serverPort1 = awft.serverPort1;
serverPort2 = awft.serverPort2;
server1 = awft.server1;
server2 = awft.server2;
hz = awft.hz;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.eclipse.jetty.webapp.WebAppContext;

public class JettyServer implements ServletContainer {

Server server;

public JettyServer(int port, String sourceDir, String serverXml) throws Exception {
Expand Down Expand Up @@ -43,6 +44,14 @@ public void buildJetty(int port, String sourceDir, String webXmlFile) throws Exc
server.setHandler(context);

server.start();
}

@Override
public boolean isRunning() {
if (server == null) {
return false;
} else {
return server.isRunning();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ public interface ServletContainer {
public void stop() throws Exception;

public void start() throws Exception;

public boolean isRunning();

}
11 changes: 11 additions & 0 deletions hazelcast-wm/src/test/java/com/hazelcast/wm/test/TomcatServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import java.io.File;

public class TomcatServer implements ServletContainer {

Tomcat tomcat;
String serverXml;
String sourceDir;
volatile boolean running;

public TomcatServer(int port, String sourceDir, String serverXml) throws Exception {
this.serverXml = serverXml;
Expand All @@ -22,21 +24,25 @@ public TomcatServer(int port, String sourceDir, String serverXml) throws Excepti
@Override
public void stop() throws LifecycleException {
tomcat.stop();
running = false;
}

@Override
public void start() throws Exception {
tomcat.start();
running = true;
}

@Override
public void restart() throws LifecycleException, InterruptedException {
int port = tomcat.getConnector().getLocalPort();
tomcat.stop();
running = false;
tomcat.destroy();
Thread.sleep(5000);
buildTomcat(port, sourceDir, serverXml);
Thread.sleep(5000);
running = true;
}

public void buildTomcat(int port, String sourceDir, String serverXml) throws LifecycleException {
Expand All @@ -54,6 +60,11 @@ public void buildTomcat(int port, String sourceDir, String serverXml) throws Lif
tomcat.getEngine().setJvmRoute("tomcat" + port);
tomcat.getEngine().setName("tomcat-test" + port);
tomcat.start();
running = true;
}

@Override
public boolean isRunning() {
return running;
}
}

0 comments on commit 59f9d53

Please sign in to comment.