Skip to content

Commit

Permalink
fix for issue hazelcast#3458 on master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
serkanozal committed Sep 1, 2014
1 parent 22398fe commit 4dc9955
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.instance.Node;
import com.hazelcast.instance.TestUtil;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.TestEnvironment;
import org.apache.http.HttpEntity;
Expand All @@ -31,13 +33,15 @@
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

Expand Down Expand Up @@ -76,14 +80,19 @@ protected enum RequestType {

protected static final RequestType DEFAULT_REQUEST_TYPE = RequestType.GET_REQUEST;

protected static final String DEFAULT_MAP_NAME = "default";

protected static final Map<Class<? extends AbstractWebFilterTest>, ContainerContext> CONTAINER_CONTEXT_MAP =
new HashMap<Class<? extends AbstractWebFilterTest>, ContainerContext>();

protected String serverXml1;
protected String serverXml2;

protected int serverPort1;
protected int serverPort2;
protected ServletContainer server1;
protected ServletContainer server2;
protected HazelcastInstance hz;
protected volatile HazelcastInstance hz;

protected AbstractWebFilterTest(String serverXml1) {
this.serverXml1 = serverXml1;
Expand All @@ -96,6 +105,46 @@ protected AbstractWebFilterTest(String serverXml1, String serverXml2) {

@Before
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)) {
// Build a new instance
buildInstance();
CONTAINER_CONTEXT_MAP.put(
getClass(),
new ContainerContext(
this,
serverXml1,
serverXml2,
serverPort1,
serverPort2,
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);
}
// 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";
Expand All @@ -109,15 +158,53 @@ public void setup() throws Exception {
}
}

@After
public void teardown() throws Exception {
server1.stop();
if (server2 != null) {
server2.stop();
protected boolean isInstanceNotActive(HazelcastInstance hz) {
Node node = TestUtil.getNode(hz);
if (node == null) {
return true;
} else {
return !node.isActive();
}
}

@AfterClass
public static void teardownClass() throws Exception {
for (Map.Entry<Class<? extends AbstractWebFilterTest>, ContainerContext> ccEntry :
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) {
cc.server2.stop();
}
} catch (Throwable t) {
t.printStackTrace();
}
}
// Shutdown all instances
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 @@ -172,7 +259,7 @@ protected HttpResponse request(RequestType reqType,
throw new IllegalArgumentException("Request type paramater cannot be empty !");
}
HttpClient client = HttpClientBuilder.create().disableRedirectHandling().setDefaultCookieStore(cookieStore).build();
HttpUriRequest request = null;
HttpUriRequest request;
switch (reqType) {
case GET_REQUEST:
request = new HttpGet("http://localhost:" + serverPort + "/" + context);
Expand All @@ -183,12 +270,53 @@ protected HttpResponse request(RequestType reqType,
default:
throw new IllegalArgumentException(reqType + " typed request is not supported");
}
HttpResponse response = client.execute(request);
return response;
return client.execute(request);
}

protected abstract ServletContainer getServletContainer(int port,
String sourceDir,
String serverXml) throws Exception;

protected static class ContainerContext {

protected AbstractWebFilterTest test;

protected String serverXml1;
protected String serverXml2;
protected int serverPort1;
protected int serverPort2;
protected ServletContainer server1;
protected ServletContainer server2;
protected HazelcastInstance hz;

public ContainerContext(AbstractWebFilterTest test,
String serverXml1,
String serverXml2,
int serverPort1,
int serverPort2,
ServletContainer server1,
ServletContainer server2,
HazelcastInstance hz) {
this.test = test;
this.serverXml1 = serverXml1;
this.serverXml2 = serverXml2;
this.serverPort1 = serverPort1;
this.serverPort2 = serverPort2;
this.server1 = server1;
this.server2 = server2;
this.hz = hz;
}

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

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public JettyWebFilterTest(String name, String serverXml1, String serverXml2) {
super(serverXml1, serverXml2);
}


@Override
protected ServletContainer getServletContainer(int port, String sourceDir, String serverXml) throws Exception{
return new JettyServer(port,sourceDir,serverXml);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@

package com.hazelcast.wm.test;

import com.hazelcast.config.FileSystemXmlConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.annotation.QuickTest;
import java.io.File;
import java.net.URL;

import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.BasicCookieStore;
import org.junit.After;
import org.junit.AfterClass;

import static org.junit.Assert.assertEquals;
import org.junit.Before;

import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
Expand All @@ -45,10 +40,6 @@
@Category(QuickTest.class)
public class WebFilterBasicTest extends AbstractWebFilterTest {

public static boolean isSetup;
private static int serverPort1, serverPort2;
private static HazelcastInstance hz;

public WebFilterBasicTest() {
super("node1-node.xml", "node2-node.xml");
}
Expand Down Expand Up @@ -95,7 +86,7 @@ public void test_removeAttribute() throws Exception {
@Test(timeout = 20000)
public void test_clusterMapSize() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
IMap<String, Object> map = hz.getMap("default");
IMap<String, Object> map = hz.getMap(DEFAULT_MAP_NAME);
executeRequest("write", serverPort1, cookieStore);

assertEquals(2, map.size());
Expand All @@ -104,7 +95,7 @@ public void test_clusterMapSize() throws Exception {
@Test(timeout = 20000)
public void test_clusterMapSizeAfterRemove() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
IMap<String, Object> map = hz.getMap("default");
IMap<String, Object> map = hz.getMap(DEFAULT_MAP_NAME);

executeRequest("write", serverPort1, cookieStore);
executeRequest("remove", serverPort2, cookieStore);
Expand All @@ -114,7 +105,7 @@ public void test_clusterMapSizeAfterRemove() throws Exception {

@Test(timeout = 20000)
public void test_updateAttribute() throws Exception {
IMap<String, Object> map = hz.getMap("default");
IMap<String, Object> map = hz.getMap(DEFAULT_MAP_NAME);
CookieStore cookieStore = new BasicCookieStore();

executeRequest("write", serverPort1, cookieStore);
Expand All @@ -126,7 +117,7 @@ public void test_updateAttribute() throws Exception {

@Test(timeout = 20000)
public void test_invalidateSession() throws Exception {
IMap<String, Object> map = hz.getMap("default");
IMap<String, Object> map = hz.getMap(DEFAULT_MAP_NAME);
CookieStore cookieStore = new BasicCookieStore();

executeRequest("write", serverPort1, cookieStore);
Expand All @@ -146,7 +137,7 @@ public void test_isNew() throws Exception {
@Test(timeout = 20000)
public void test_sessionTimeout() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
IMap<String, Object> map = hz.getMap("default");
IMap<String, Object> map = hz.getMap(DEFAULT_MAP_NAME);

executeRequest("write", serverPort1, cookieStore);
executeRequest("timeout", serverPort1, cookieStore);
Expand All @@ -157,34 +148,4 @@ public void test_sessionTimeout() throws Exception {
protected ServletContainer getServletContainer(int port, String sourceDir, String serverXml) throws Exception {
return new JettyServer(port, sourceDir, serverXml);
}

@Before
public void setup() throws Exception {
if (isSetup == true) {
return;
}
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);
if (serverXml2 != null) {
serverPort2 = availablePort();
server2 = getServletContainer(serverPort2, sourceDir, serverXml2);
}
isSetup = true;
}

@After
public void teardown() throws Exception {
IMap<String, Object> map = hz.getMap("default");
map.clear();
}

@AfterClass
public static void shutdownAll() {
Hazelcast.shutdownAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public WebFilterSessionCleanupTest() {

@Test(timeout = 130000)
public void testSessionTimeout() throws Exception {
IMap<String, Object> map = hz.getMap("default");
IMap<String, Object> map = hz.getMap(DEFAULT_MAP_NAME);
CookieStore cookieStore = new BasicCookieStore();

// Write a value into the session on one server
Expand Down
Loading

0 comments on commit 4dc9955

Please sign in to comment.