Skip to content

Commit

Permalink
Make sure all tests can handle -Dtest.runs=
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Mar 8, 2016
1 parent 45c90ec commit cdcae68
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 63 deletions.
Expand Up @@ -39,6 +39,7 @@
import org.apache.http.protocol.HttpContext;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -61,6 +62,11 @@ public class ChunkedResponseTrailersTestCase {

private static volatile ServerConnection connection;

@Before
public void reset() {
connection = null;
}

@BeforeClass
public static void setup() {
final BlockingHandler blockingHandler = new BlockingHandler();
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -48,6 +49,11 @@ public class ChunkedResponseTransferCodingTestCase {

private static volatile ServerConnection connection;

@Before
public void reset() {
connection = null;
}

@BeforeClass
public static void setup() {
final BlockingHandler blockingHandler = new BlockingHandler();
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -50,6 +51,11 @@ public class FixedLengthResponseTestCase {

private static volatile ServerConnection connection;

@Before
public void reset() {
connection = null;
}

@BeforeClass
public static void setup() {
DefaultServer.setRootHandler(new HttpHandler() {
Expand Down
Expand Up @@ -28,7 +28,6 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand All @@ -40,21 +39,18 @@
@RunWith(DefaultServer.class)
public class MetricsHandlerTestCase {

private static MetricsHandler metricsHandler;
private static CompletionLatchHandler latchHandler;
@BeforeClass
public static void setup() {
@Test
public void testMetrics() throws IOException, InterruptedException {

MetricsHandler metricsHandler;
CompletionLatchHandler latchHandler;
DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(metricsHandler = new MetricsHandler(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
Thread.sleep(100);
exchange.getResponseSender().send("Hello");
}
})));
}

@Test
public void testMetrics() throws IOException, InterruptedException {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
TestHttpClient client = new TestHttpClient();
try {
Expand Down
Expand Up @@ -34,7 +34,6 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand All @@ -48,10 +47,10 @@
public class CacheHandlerTestCase {


private static final AtomicInteger responseCount = new AtomicInteger();
@Test
public void testBasicPathBasedCaching() throws IOException {

@BeforeClass
public static void setup() {
final AtomicInteger responseCount = new AtomicInteger();

final HttpHandler messageHandler = new HttpHandler() {
@Override
Expand All @@ -66,10 +65,7 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception {
};
final CacheHandler cacheHandler = new CacheHandler(new DirectBufferCache(100, 10, 1000), messageHandler);
DefaultServer.setRootHandler(cacheHandler);
}

@Test
public void testBasicPathBasedCaching() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/io/undertow/testutils/DefaultServer.java
Expand Up @@ -384,10 +384,10 @@ private static void runInternal(final RunNotifier notifier) {

} else {
if(h2) {
log.error("HTTP2 selected but Netty ALPN was not on the boot class path");
throw new RuntimeException("HTTP2 selected but Netty ALPN was not on the boot class path");
}
if(spdy) {
log.error("SPDY selected but Netty ALPN was not on the boot class path");
throw new RuntimeException("SPDY selected but Netty ALPN was not on the boot class path");
}
openListener = new HttpOpenListener(pool, OptionMap.create(UndertowOptions.BUFFER_PIPELINED_DATA, true, UndertowOptions.ENABLE_CONNECTOR_STATISTICS, true));
acceptListener = ChannelListeners.openListenerAdapter(wrapOpenListener(openListener));
Expand Down
Expand Up @@ -29,6 +29,7 @@
import io.undertow.server.handlers.cache.DirectBufferCache;
import io.undertow.server.handlers.resource.CachingResourceManager;
import io.undertow.server.handlers.resource.PathResourceManager;
import io.undertow.server.session.SecureRandomSessionIdGenerator;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;
Expand All @@ -47,6 +48,7 @@
import org.apache.http.client.methods.HttpGet;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -62,6 +64,14 @@ public class DefaultServletCachingTestCase {
public static final String DIR_NAME = "cacheTest";

static Path tmpDir;
static DirectBufferCache dataCache = new DirectBufferCache(1000, 10, 1000 * 10 * 1000, BufferAllocator.DIRECT_BYTE_BUFFER_ALLOCATOR, METADATA_MAX_AGE);

@Before
public void before() {
for(Object k : dataCache.getAllKeys()) {
dataCache.remove(k);
}
}

@BeforeClass
public static void setup() throws ServletException, IOException {
Expand All @@ -76,7 +86,7 @@ public static void setup() throws ServletException, IOException {
.setClassLoader(ServletPathMappingTestCase.class.getClassLoader())
.setContextPath("/servletContext")
.setDeploymentName("servletContext.war")
.setResourceManager(new CachingResourceManager(100, 10000, new DirectBufferCache(1000, 10, 1000 * 10 * 1000, BufferAllocator.DIRECT_BYTE_BUFFER_ALLOCATOR, METADATA_MAX_AGE), new PathResourceManager(tmpDir, 10485760), METADATA_MAX_AGE));
.setResourceManager(new CachingResourceManager(100, 10000, dataCache, new PathResourceManager(tmpDir, 10485760), METADATA_MAX_AGE));

builder.addServlet(new ServletInfo("DefaultTestServlet", PathTestServlet.class)
.addMapping("/path/default"))
Expand All @@ -98,7 +108,7 @@ public static void after() throws IOException{
@Test
public void testFileExistanceCheckCached() throws IOException, InterruptedException {
TestHttpClient client = new TestHttpClient();
String fileName = "doesnotexist.html";
String fileName = new SecureRandomSessionIdGenerator().createSessionId() + ".html";
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/" + fileName);
HttpResponse result = client.execute(get);
Expand All @@ -118,6 +128,7 @@ public void testFileExistanceCheckCached() throws IOException, InterruptedExcept
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Assert.assertEquals("hello", response);
Files.delete(f);
} finally {
client.getConnectionManager().shutdown();
}
Expand Down
Expand Up @@ -18,10 +18,6 @@

package io.undertow.servlet.test.metrics;

import java.io.IOException;
import javax.servlet.DispatcherType;
import javax.servlet.ServletException;

import io.undertow.server.handlers.MetricsHandler;
import io.undertow.server.handlers.PathHandler;
import io.undertow.servlet.api.DeploymentInfo;
Expand All @@ -42,23 +38,24 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.servlet.DispatcherType;

/**
* @author Stuart Douglas
*/
@RunWith(DefaultServer.class)
public class ServletMetricsHandlerTestCase {

private static TestMetricsCollector metricsCollector = new TestMetricsCollector();
@Test
public void testMetrics() throws Exception {

private static CompletionLatchHandler completionLatchHandler;

@BeforeClass
public static void setup() throws ServletException {
final TestMetricsCollector metricsCollector = new TestMetricsCollector();

CompletionLatchHandler completionLatchHandler;
final PathHandler root = new PathHandler();
final ServletContainer container = ServletContainer.Factory.newInstance();

Expand All @@ -81,11 +78,7 @@ public static void setup() throws ServletException {
root.addPrefixPath(builder.getContextPath(), manager.start());

DefaultServer.setRootHandler(completionLatchHandler = new CompletionLatchHandler(root));
}


@Test
public void testMetrics() throws IOException, InterruptedException {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/path/default");
TestHttpClient client = new TestHttpClient();
try {
Expand Down
Expand Up @@ -36,9 +36,9 @@
import io.undertow.util.StatusCodes;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.junit.AfterClass;
import org.junit.After;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -49,13 +49,13 @@
@RunWith(DefaultServer.class)
public class ExecutorPerServletTestCase {

private static ExecutorService executorService;
private ExecutorService executorService;

public static final int NUM_THREADS = 10;
public static final int NUM_REQUESTS = 100;

@BeforeClass
public static void setup() throws ServletException {
@Before
public void setup() throws ServletException {
DeploymentUtils.setupServlet(
new ServletInfo("racey", RaceyAddServlet.class)
.addMapping("/racey"),
Expand All @@ -64,8 +64,8 @@ public static void setup() throws ServletException {
.setExecutor(executorService = Executors.newSingleThreadExecutor()));
}

@AfterClass
public static void after() {
@After
public void after() {
executorService.shutdown();
}

Expand Down
Expand Up @@ -37,7 +37,6 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand All @@ -53,11 +52,8 @@
@RunWith(DefaultServer.class)
public class ServletSessionCrawlerTestCase {


@BeforeClass
public static void setup() throws ServletException {


@Test
public void testCrawlerSessionUsage() throws IOException, InterruptedException {
final PathHandler pathHandler = new PathHandler();
final ServletContainer container = ServletContainer.Factory.newInstance();
DeploymentInfo builder = new DeploymentInfo()
Expand All @@ -77,11 +73,6 @@ public static void setup() throws ServletException {
throw new RuntimeException(e);
}
DefaultServer.setRootHandler(pathHandler);
}


@Test
public void testCrawlerSessionUsage() throws IOException, InterruptedException {
TestHttpClient client = new TestHttpClient();

client.setCookieStore(new CookieStore() {
Expand Down
Expand Up @@ -36,6 +36,7 @@ public class ContentLengthCloseFlushServlet extends HttpServlet {
@Override
protected synchronized void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
if (completed) {
completed = false;
resp.getWriter().write("OK");
} else {
resp.setContentLength(1);
Expand Down
Expand Up @@ -37,7 +37,7 @@
@RunWith(DefaultServer.class)
public class EarlyCloseServlet extends HttpServlet {

private static volatile ServerConnection connection;
private volatile ServerConnection connection;

@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
Expand Down
Expand Up @@ -18,8 +18,6 @@

package io.undertow.servlet.test.streams;

import javax.servlet.ServletException;

import io.undertow.servlet.api.ServletInfo;
import io.undertow.servlet.test.util.DeploymentUtils;
import io.undertow.testutils.DefaultServer;
Expand All @@ -30,7 +28,6 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand All @@ -44,15 +41,12 @@ public class ServletInputStreamEarlyCloseTestCase {

public static final String SERVLET = "servlet";

@BeforeClass
public static void setup() throws ServletException {
@Test
public void testServletInputStreamEarlyClose() throws Exception {

DeploymentUtils.setupServlet(
new ServletInfo(SERVLET, EarlyCloseServlet.class)
.addMapping("/" + SERVLET));
}

@Test
public void testServletInputStreamEarlyClose() throws Exception {
TestHttpClient client = new TestHttpClient();
try {
String uri = DefaultServer.getDefaultServerURL() + "/servletContext/" + SERVLET;
Expand Down
Expand Up @@ -93,6 +93,8 @@ public static void setup() throws ServletException {

@Test
public void testMessagesReceivedInOrder() throws Exception {
stacks.clear();
EchoSocket.receivedEchos = new FutureResult<>();
final ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
final CountDownLatch done = new CountDownLatch(1);
final AtomicReference<String> error = new AtomicReference<>();
Expand Down Expand Up @@ -145,8 +147,8 @@ public void onOpen(final Session session, EndpointConfig endpointConfig) {

@ServerEndpoint("/webSocket")
public static class EchoSocket {
private List<String> echos = new CopyOnWriteArrayList<>();
public static final FutureResult<List<String>> receivedEchos = new FutureResult<>();
private final List<String> echos = new CopyOnWriteArrayList<>();
public static volatile FutureResult<List<String>> receivedEchos = new FutureResult<>();

@OnMessage
public void onMessage(ByteBuffer dataBuffer, Session session) throws IOException {
Expand Down

0 comments on commit cdcae68

Please sign in to comment.