mirror of
https://github.com/jetty/jetty.project.git
synced 2025-03-01 03:19:13 +00:00
Made the test more reliable.
git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2932 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
parent
7e8b4cb771
commit
d6627db6b5
@ -13,9 +13,6 @@
|
||||
|
||||
package org.eclipse.jetty.server;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -24,7 +21,6 @@ import java.net.Socket;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@ -37,6 +33,9 @@ import org.eclipse.jetty.server.nio.NetworkTrafficSelectChannelConnector;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class NetworkTrafficListenerTest
|
||||
{
|
||||
private static final byte END_OF_CONTENT = '~';
|
||||
@ -369,12 +368,22 @@ public class NetworkTrafficListenerTest
|
||||
{
|
||||
public void handle(String uri, Request request, HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException, ServletException
|
||||
{
|
||||
// Read and discard the request body to make the test more
|
||||
// reliable, otherwise there is a race between request body
|
||||
// upload and response download
|
||||
InputStream input = servletRequest.getInputStream();
|
||||
byte[] buffer = new byte[4096];
|
||||
while (true)
|
||||
{
|
||||
int read = input.read(buffer);
|
||||
if (read < 0)
|
||||
break;
|
||||
}
|
||||
request.setHandled(true);
|
||||
}
|
||||
});
|
||||
|
||||
final AtomicReference<String> incomingData = new AtomicReference<String>("");
|
||||
final CountDownLatch incomingLatch = new CountDownLatch(4);
|
||||
final AtomicReference<String> outgoingData = new AtomicReference<String>("");
|
||||
final CountDownLatch outgoingLatch = new CountDownLatch(1);
|
||||
connector.addNetworkTrafficListener(new NetworkTrafficListener.Empty()
|
||||
@ -382,7 +391,6 @@ public class NetworkTrafficListenerTest
|
||||
public void incoming(Socket socket, Buffer bytes)
|
||||
{
|
||||
incomingData.set(incomingData.get() + bytes.toString("UTF-8"));
|
||||
incomingLatch.countDown();
|
||||
}
|
||||
|
||||
public void outgoing(Socket socket, Buffer bytes)
|
||||
@ -414,9 +422,6 @@ public class NetworkTrafficListenerTest
|
||||
output.write(request.getBytes("UTF-8"));
|
||||
output.flush();
|
||||
|
||||
assertTrue(incomingLatch.await(1, TimeUnit.SECONDS));
|
||||
assertEquals(request, incomingData.get());
|
||||
|
||||
assertTrue(outgoingLatch.await(1, TimeUnit.SECONDS));
|
||||
assertEquals(expectedResponse, outgoingData.get());
|
||||
|
||||
@ -424,6 +429,8 @@ public class NetworkTrafficListenerTest
|
||||
String response = new String(responseBytes, "UTF-8");
|
||||
assertEquals(expectedResponse, response);
|
||||
|
||||
assertEquals(request, incomingData.get());
|
||||
|
||||
socket.close();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user