416321 improved test harnesses

This commit is contained in:
Greg Wilkins 2013-09-02 12:24:51 +10:00
parent d51268977b
commit 317a3f27df
7 changed files with 95 additions and 8 deletions

View File

@ -143,6 +143,26 @@ public class ServerConnector extends AbstractNetworkConnector
this(server,null,null,null,0,0,AbstractConnectionFactory.getFactories(sslContextFactory,new HttpConnectionFactory()));
}
/* ------------------------------------------------------------ */
/** HTTP Server Connection.
* <p>Construct a ServerConnector with a private instance of {@link HttpConnectionFactory} as the primary protocol</p>.
* @param server The {@link Server} this connector will accept connection for.
* @param sslContextFactory If non null, then a {@link SslConnectionFactory} is instantiated and prepended to the
* list of HTTP Connection Factory.
* @param acceptors
* the number of acceptor threads to use, or 0 for a default value. Acceptors accept new TCP/IP connections.
* @param selectors
* the number of selector threads, or 0 for a default value. Selectors notice and schedule established connection that can make IO progress.
*/
public ServerConnector(
@Name("server") Server server,
@Name("acceptors") int acceptors,
@Name("selectors") int selectors,
@Name("sslContextFactory") SslContextFactory sslContextFactory)
{
this(server,null,null,null,acceptors,selectors,AbstractConnectionFactory.getFactories(sslContextFactory,new HttpConnectionFactory()));
}
/* ------------------------------------------------------------ */
/** Generic SSL Server Connection.
* @param server The {@link Server} this connector will accept connection for.

View File

@ -18,8 +18,10 @@
package org.eclipse.jetty.server;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
@ -33,10 +35,13 @@ import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URI;
import java.net.URL;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.servlet.ServletException;
@ -48,7 +53,6 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
@ -658,6 +662,64 @@ public abstract class HttpServerTestBase extends HttpServerTestFixture
}
}
@Test
public void testCloseWhileWriteBlocked() throws Exception
{
configureServer(new DataHandler());
try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort()))
{
OutputStream os = client.getOutputStream();
InputStream is = client.getInputStream();
os.write((
"GET /data?encoding=iso-8859-1&writes=100&block=100000 HTTP/1.1\r\n" +
"host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" +
"connection: close\r\n" +
"content-type: unknown\r\n" +
"\r\n"
).getBytes());
os.flush();
// Read the first part of the response
byte[] buf = new byte[1024 * 8];
is.read(buf);
// sleep to ensure server is blocking
Thread.sleep(250);
System.err.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
Thread.sleep(20000);
System.err.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
// Close the client
client.close();
}
Thread.sleep(20000);
System.err.println("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
Thread.sleep(200);
// check server is still handling requests quickly
try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort()))
{
client.setSoTimeout(500);
OutputStream os = client.getOutputStream();
InputStream is = client.getInputStream();
os.write(("GET /data?writes=1&block=1024 HTTP/1.1\r\n" +
"host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" +
"connection: close\r\n" +
"content-type: unknown\r\n" +
"\r\n"
).getBytes());
os.flush();
String response = IO.toString(is);
assertThat(response,startsWith("HTTP/1.1 200 OK"));
}
}
@Test
public void testBigBlocks() throws Exception
{
@ -1435,7 +1497,7 @@ public abstract class HttpServerTestBase extends HttpServerTestFixture
os.flush();
String response = readResponse(client);
assertThat(response, JUnitMatchers.containsString("RESUMEDHTTP/1.1 200 OK"));
assertThat(response, containsString("RESUMEDHTTP/1.1 200 OK"));
assertThat((System.currentTimeMillis() - start), greaterThanOrEqualTo(1999L));
// TODO This test should also check that that the CPU did not spin during the suspend.

View File

@ -151,6 +151,7 @@ public class HttpServerTestFixture
protected static class HelloWorldHandler extends AbstractHandler
{
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
baseRequest.setHandled(true);
@ -195,20 +196,24 @@ public class HttpServerTestFixture
else if ("true".equals(chars))
{
response.setCharacterEncoding(encoding);
Writer out=response.getWriter();
PrintWriter out=response.getWriter();
char[] c=chunk.toCharArray();
for (int i=0;i<writes;i++)
{
out.write(c);
if (out.checkError())
break;
}
}
else
{
response.setCharacterEncoding(encoding);
Writer out=response.getWriter();
PrintWriter out=response.getWriter();
for (int i=0;i<writes;i++)
{
out.write(chunk);
if (out.checkError())
break;
}
}

View File

@ -28,6 +28,6 @@ public class SelectChannelServerTest extends HttpServerTestBase
@Before
public void init() throws Exception
{
startServer(new ServerConnector(_server));
startServer(new ServerConnector(_server,1,1));
}
}

View File

@ -36,7 +36,7 @@ public class SelectChannelTimeoutTest extends ConnectorTimeoutTest
@Before
public void init() throws Exception
{
ServerConnector connector = new ServerConnector(_server);
ServerConnector connector = new ServerConnector(_server,1,1);
connector.setIdleTimeout(MAX_IDLE_TIME); // 250 msec max idle
startServer(connector);
}

View File

@ -106,7 +106,7 @@ public class SelectChannelServerSslTest extends HttpServerTestBase
sslContextFactory.setKeyManagerPassword("keypwd");
sslContextFactory.setTrustStorePath(keystorePath);
sslContextFactory.setTrustStorePassword("storepwd");
ServerConnector connector = new ServerConnector(_server, sslContextFactory);
ServerConnector connector = new ServerConnector(_server, 1, 1, sslContextFactory);
startServer(connector);

View File

@ -50,7 +50,7 @@ public class SslSelectChannelTimeoutTest extends ConnectorTimeoutTest
sslContextFactory.setKeyManagerPassword("keypwd");
sslContextFactory.setTrustStorePath(keystorePath);
sslContextFactory.setTrustStorePassword("storepwd");
ServerConnector connector = new ServerConnector(_server, sslContextFactory);
ServerConnector connector = new ServerConnector(_server, 1, 1, sslContextFactory);
connector.setIdleTimeout(MAX_IDLE_TIME); //250 msec max idle
startServer(connector);