Fixes #11327 - Flaky test ServletTest.testSimpleIdleRead().
Just improving the test code. The flakyness was likely fixed by the work in #12216 and #12237. Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
parent
fa0502db54
commit
24f0ac28ad
|
@ -14,6 +14,9 @@
|
|||
package org.eclipse.jetty.ee10.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import jakarta.servlet.ServletException;
|
||||
|
@ -22,8 +25,13 @@ import jakarta.servlet.http.HttpServletMapping;
|
|||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.MappingMatch;
|
||||
import org.eclipse.jetty.http.HttpHeader;
|
||||
import org.eclipse.jetty.http.HttpHeaderValue;
|
||||
import org.eclipse.jetty.http.HttpStatus;
|
||||
import org.eclipse.jetty.http.HttpTester;
|
||||
import org.eclipse.jetty.server.LocalConnector;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.util.IO;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
|
@ -33,6 +41,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
|||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ServletTest
|
||||
{
|
||||
|
@ -125,7 +134,7 @@ public class ServletTest
|
|||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
|
||||
{
|
||||
String input = IO.toString(req.getInputStream());
|
||||
resp.getWriter().println("Hello " + input);
|
||||
resp.getWriter().print("Hello " + input);
|
||||
}
|
||||
}, "/post");
|
||||
|
||||
|
@ -138,24 +147,67 @@ public class ServletTest
|
|||
POST /ctx/post HTTP/1.1
|
||||
Host: local
|
||||
Content-Length: 10
|
||||
|
||||
|
||||
""";
|
||||
endPoint.addInput(request);
|
||||
endPoint.addInput("1234567890\n");
|
||||
String response = endPoint.getResponse(false, 5, TimeUnit.SECONDS);
|
||||
assertThat(response, containsString(" 200 OK"));
|
||||
assertThat(response, containsString("Hello 1234567890"));
|
||||
endPoint.addInput("1234567890");
|
||||
HttpTester.Response response = HttpTester.parseResponse(endPoint.getResponse(false, 5, TimeUnit.SECONDS));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
assertThat(response.getContent(), is("Hello 1234567890"));
|
||||
|
||||
endPoint.addInputAndExecute(request);
|
||||
endPoint.addInput("1234567890\n");
|
||||
response = endPoint.getResponse(false, 5, TimeUnit.SECONDS);
|
||||
assertThat(response, containsString(" 200 OK"));
|
||||
assertThat(response, containsString("Hello 1234567890"));
|
||||
endPoint.addInput("1234567890");
|
||||
response = HttpTester.parseResponse(endPoint.getResponse(false, 5, TimeUnit.SECONDS));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
assertThat(response.getContent(), is("Hello 1234567890"));
|
||||
|
||||
endPoint.addInputAndExecute(request);
|
||||
response = endPoint.getResponse(false, 2 * idleTimeout, TimeUnit.MILLISECONDS);
|
||||
assertThat(response, containsString(" 500 "));
|
||||
assertThat(response, containsString("Connection: close"));
|
||||
// Do not send the content.
|
||||
response = HttpTester.parseResponse(endPoint.getResponse(false, 2 * idleTimeout, TimeUnit.MILLISECONDS));
|
||||
assertThat(response.getStatus(), is(HttpStatus.INTERNAL_SERVER_ERROR_500));
|
||||
assertTrue(response.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleIdleReadNetwork() throws Exception
|
||||
{
|
||||
ServerConnector networkConnector = new ServerConnector(_server, 1, 1);
|
||||
_server.addConnector(networkConnector);
|
||||
|
||||
long idleTimeout = 1000;
|
||||
_context.addServlet(new HttpServlet()
|
||||
{
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
|
||||
{
|
||||
String input = IO.toString(req.getInputStream());
|
||||
resp.getWriter().print("Hello " + input);
|
||||
}
|
||||
}, "/post");
|
||||
|
||||
networkConnector.setIdleTimeout(idleTimeout);
|
||||
_server.start();
|
||||
|
||||
try (SocketChannel client = SocketChannel.open(new InetSocketAddress("localhost", networkConnector.getLocalPort())))
|
||||
{
|
||||
String request = """
|
||||
POST /ctx/post HTTP/1.1
|
||||
Host: local
|
||||
Content-Length: 10
|
||||
|
||||
""";
|
||||
client.write(StandardCharsets.UTF_8.encode(request));
|
||||
client.write(StandardCharsets.UTF_8.encode("1234567890"));
|
||||
HttpTester.Response response = HttpTester.parseResponse(client);
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
assertThat(response.getContent(), is("Hello 1234567890"));
|
||||
|
||||
client.write(StandardCharsets.UTF_8.encode(request));
|
||||
// Do not send the content.
|
||||
response = HttpTester.parseResponse(client);
|
||||
assertThat(response.getStatus(), is(HttpStatus.INTERNAL_SERVER_ERROR_500));
|
||||
assertTrue(response.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# Jetty Logging using jetty-slf4j-impl
|
||||
#org.eclipse.jetty.LEVEL=DEBUG
|
||||
#org.eclipse.jetty.http.LEVEL=DEBUG
|
||||
#org.eclipse.jetty.server.LEVEL=DEBUG
|
||||
|
|
|
@ -20,6 +20,10 @@ import jakarta.servlet.ServletException;
|
|||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.eclipse.jetty.http.HttpHeader;
|
||||
import org.eclipse.jetty.http.HttpHeaderValue;
|
||||
import org.eclipse.jetty.http.HttpStatus;
|
||||
import org.eclipse.jetty.http.HttpTester;
|
||||
import org.eclipse.jetty.server.LocalConnector;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.util.IO;
|
||||
|
@ -29,7 +33,9 @@ import org.junit.jupiter.api.Test;
|
|||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ServletTest
|
||||
{
|
||||
|
@ -122,7 +128,7 @@ public class ServletTest
|
|||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
|
||||
{
|
||||
String input = IO.toString(req.getInputStream());
|
||||
resp.getWriter().println("Hello " + input);
|
||||
resp.getWriter().print("Hello " + input);
|
||||
}
|
||||
}), "/post");
|
||||
|
||||
|
@ -135,24 +141,25 @@ public class ServletTest
|
|||
POST /ctx/post HTTP/1.1
|
||||
Host: local
|
||||
Content-Length: 10
|
||||
|
||||
|
||||
""";
|
||||
endPoint.addInput(request);
|
||||
endPoint.addInput("1234567890\n");
|
||||
String response = endPoint.getResponse(false, 5, TimeUnit.SECONDS);
|
||||
assertThat(response, containsString(" 200 OK"));
|
||||
assertThat(response, containsString("Hello 1234567890"));
|
||||
endPoint.addInput("1234567890");
|
||||
HttpTester.Response response = HttpTester.parseResponse(endPoint.getResponse(false, 5, TimeUnit.SECONDS));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
assertThat(response.getContent(), is("Hello 1234567890"));
|
||||
|
||||
endPoint.addInputAndExecute(request);
|
||||
endPoint.addInput("1234567890\n");
|
||||
response = endPoint.getResponse(false, 5, TimeUnit.SECONDS);
|
||||
assertThat(response, containsString(" 200 OK"));
|
||||
assertThat(response, containsString("Hello 1234567890"));
|
||||
endPoint.addInput("1234567890");
|
||||
response = HttpTester.parseResponse(endPoint.getResponse(false, 5, TimeUnit.SECONDS));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
assertThat(response.getContent(), is("Hello 1234567890"));
|
||||
|
||||
endPoint.addInputAndExecute(request);
|
||||
response = endPoint.getResponse(false, 2 * idleTimeout, TimeUnit.MILLISECONDS);
|
||||
assertThat(response, containsString(" 500 "));
|
||||
assertThat(response, containsString("Connection: close"));
|
||||
// Do not send the content.
|
||||
response = HttpTester.parseResponse(endPoint.getResponse(false, 2 * idleTimeout, TimeUnit.MILLISECONDS));
|
||||
assertThat(response.getStatus(), is(HttpStatus.INTERNAL_SERVER_ERROR_500));
|
||||
assertTrue(response.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue