fixed noisy/broken tests

This commit is contained in:
Greg Wilkins 2016-07-19 15:16:14 +10:00
parent cca9027285
commit 86ff9f984d
4 changed files with 53 additions and 33 deletions

View File

@ -199,11 +199,14 @@ public class HttpOutput extends ServletOutputStream implements Runnable
}
case ASYNC:
if (!_state.compareAndSet(state, OutputState.READY))
continue;
break;
case UNREADY:
case PENDING:
{
if (!_state.compareAndSet(state,OutputState.CLOSED))
break;
continue;
IOException ex = new IOException("Closed while Pending/Unready");
LOG.warn(ex.toString());
LOG.debug(ex);
@ -212,7 +215,7 @@ public class HttpOutput extends ServletOutputStream implements Runnable
default:
{
if (!_state.compareAndSet(state,OutputState.CLOSED))
break;
continue;
try
{

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -46,6 +47,8 @@ import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.LocalConnector.LocalEndPoint;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
@ -75,6 +78,7 @@ public class AsyncIOServletTest
{
private Server server;
private ServerConnector connector;
private LocalConnector local;
private ServletContextHandler context;
private String path = "/path";
private static final ThreadLocal<Throwable> scope = new ThreadLocal<>();
@ -83,6 +87,7 @@ public class AsyncIOServletTest
{
startServer(servlet,30000);
}
public void startServer(HttpServlet servlet, long idleTimeout) throws Exception
{
server = new Server();
@ -90,6 +95,8 @@ public class AsyncIOServletTest
connector.setIdleTimeout(idleTimeout);
connector.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration().setDelayDispatchUntilContent(false);
server.addConnector(connector);
local = new LocalConnector(server);
server.addConnector(local);
context = new ServletContextHandler(server, "/", false, false);
ServletHolder holder = new ServletHolder(servlet);
@ -952,11 +959,16 @@ public class AsyncIOServletTest
int read = input.read(buffer);
if (read < 0)
{
asyncContext.complete();
//if (output.isReady())
{
asyncContext.complete();
}
break;
}
if (output.isReady())
{
output.write(buffer, 0, read);
}
else
Assert.fail();
}
@ -978,7 +990,10 @@ public class AsyncIOServletTest
@Override
public void onWritePossible() throws IOException
{
writeLatch.countDown();
if (writeLatch.getCount()==0)
asyncContext.complete();
else
writeLatch.countDown();
}
@Override
@ -992,29 +1007,24 @@ public class AsyncIOServletTest
String content = "0123456789ABCDEF";
try (Socket client = new Socket("localhost", connector.getLocalPort()))
try (LocalEndPoint endp = local.connect())
{
OutputStream output = client.getOutputStream();
String request = "POST " + path + " HTTP/1.1\r\n" +
"Host: localhost:" + connector.getLocalPort() + "\r\n" +
"Transfer-Encoding: chunked\r\n" +
"\r\n" +
"10\r\n" +
Integer.toHexString(content.length())+"\r\n" +
content + "\r\n";
output.write(request.getBytes("UTF-8"));
output.flush();
endp.addInput(ByteBuffer.wrap(request.getBytes("UTF-8")));
assertTrue(writeLatch.await(5, TimeUnit.SECONDS));
request = "" +
"0\r\n" +
"\r\n";
output.write(request.getBytes("UTF-8"));
output.flush();
endp.addInput(ByteBuffer.wrap(request.getBytes("UTF-8")));
HttpTester.Input input = HttpTester.from(client.getInputStream());
HttpTester.Response response = HttpTester.parseResponse(input);
HttpTester.Response response = HttpTester.parseResponse(endp.getResponse());
assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
assertThat(response.getContent(), Matchers.equalTo(content));

View File

@ -200,7 +200,7 @@ public class AsyncServletTest
@Test
public void testAsyncNotSupportedAsync() throws Exception
{
try (StacklessLogging stackless = new StacklessLogging(ServletHandler.class))
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class))
{
_expectedCode="500 ";
String response=process("noasync","start=200",null);

View File

@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Dispatcher;
import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.QuietServletException;
import org.eclipse.jetty.server.Server;
@ -84,7 +85,7 @@ public class ErrorPageTest
@Test
public void testErrorCode() throws Exception
{
String response = _connector.getResponses("GET /fail/code?code=599 HTTP/1.0\r\n\r\n");
String response = _connector.getResponse("GET /fail/code?code=599 HTTP/1.0\r\n\r\n");
assertThat(response,Matchers.containsString("HTTP/1.1 599 599"));
assertThat(response,Matchers.containsString("ERROR_PAGE: /599"));
assertThat(response,Matchers.containsString("ERROR_CODE: 599"));
@ -97,20 +98,23 @@ public class ErrorPageTest
@Test
public void testErrorException() throws Exception
{
String response = _connector.getResponses("GET /fail/exception HTTP/1.0\r\n\r\n");
assertThat(response,Matchers.containsString("HTTP/1.1 500 Server Error"));
assertThat(response,Matchers.containsString("ERROR_PAGE: /TestException"));
assertThat(response,Matchers.containsString("ERROR_CODE: 500"));
assertThat(response,Matchers.containsString("ERROR_EXCEPTION: javax.servlet.ServletException: java.lang.IllegalStateException"));
assertThat(response,Matchers.containsString("ERROR_EXCEPTION_TYPE: class javax.servlet.ServletException"));
assertThat(response,Matchers.containsString("ERROR_SERVLET: org.eclipse.jetty.servlet.ErrorPageTest$FailServlet-"));
assertThat(response,Matchers.containsString("ERROR_REQUEST_URI: /fail/exception"));
try(StacklessLogging stackless = new StacklessLogging(HttpChannel.class))
{
String response = _connector.getResponse("GET /fail/exception HTTP/1.0\r\n\r\n");
assertThat(response,Matchers.containsString("HTTP/1.1 500 Server Error"));
assertThat(response,Matchers.containsString("ERROR_PAGE: /TestException"));
assertThat(response,Matchers.containsString("ERROR_CODE: 500"));
assertThat(response,Matchers.containsString("ERROR_EXCEPTION: javax.servlet.ServletException: java.lang.IllegalStateException"));
assertThat(response,Matchers.containsString("ERROR_EXCEPTION_TYPE: class javax.servlet.ServletException"));
assertThat(response,Matchers.containsString("ERROR_SERVLET: org.eclipse.jetty.servlet.ErrorPageTest$FailServlet-"));
assertThat(response,Matchers.containsString("ERROR_REQUEST_URI: /fail/exception"));
}
}
@Test
public void testGlobalErrorCode() throws Exception
{
String response = _connector.getResponses("GET /fail/global?code=598 HTTP/1.0\r\n\r\n");
String response = _connector.getResponse("GET /fail/global?code=598 HTTP/1.0\r\n\r\n");
assertThat(response,Matchers.containsString("HTTP/1.1 598 598"));
assertThat(response,Matchers.containsString("ERROR_PAGE: /GlobalErrorPage"));
assertThat(response,Matchers.containsString("ERROR_CODE: 598"));
@ -123,14 +127,17 @@ public class ErrorPageTest
@Test
public void testGlobalErrorException() throws Exception
{
String response = _connector.getResponses("GET /fail/global?code=NAN HTTP/1.0\r\n\r\n");
assertThat(response,Matchers.containsString("HTTP/1.1 500 Server Error"));
assertThat(response,Matchers.containsString("ERROR_PAGE: /GlobalErrorPage"));
assertThat(response,Matchers.containsString("ERROR_CODE: 500"));
assertThat(response,Matchers.containsString("ERROR_EXCEPTION: java.lang.NumberFormatException: For input string: \"NAN\""));
assertThat(response,Matchers.containsString("ERROR_EXCEPTION_TYPE: class java.lang.NumberFormatException"));
assertThat(response,Matchers.containsString("ERROR_SERVLET: org.eclipse.jetty.servlet.ErrorPageTest$FailServlet-"));
assertThat(response,Matchers.containsString("ERROR_REQUEST_URI: /fail/global"));
try(StacklessLogging stackless = new StacklessLogging(HttpChannel.class))
{
String response = _connector.getResponse("GET /fail/global?code=NAN HTTP/1.0\r\n\r\n");
assertThat(response,Matchers.containsString("HTTP/1.1 500 Server Error"));
assertThat(response,Matchers.containsString("ERROR_PAGE: /GlobalErrorPage"));
assertThat(response,Matchers.containsString("ERROR_CODE: 500"));
assertThat(response,Matchers.containsString("ERROR_EXCEPTION: java.lang.NumberFormatException: For input string: \"NAN\""));
assertThat(response,Matchers.containsString("ERROR_EXCEPTION_TYPE: class java.lang.NumberFormatException"));
assertThat(response,Matchers.containsString("ERROR_SERVLET: org.eclipse.jetty.servlet.ErrorPageTest$FailServlet-"));
assertThat(response,Matchers.containsString("ERROR_REQUEST_URI: /fail/global"));
}
}
public static class FailServlet extends HttpServlet implements Servlet