Skip to content

Commit

Permalink
Fixes #11327 - Flaky test ServletTest.testSimpleIdleRead().
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
sbordet committed Sep 10, 2024
1 parent fa0502d commit 24f0ac2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,8 +25,13 @@
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;
Expand All @@ -33,6 +41,7 @@
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
{
Expand Down Expand Up @@ -125,7 +134,7 @@ public void testSimpleIdleRead() throws Exception
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");

Expand All @@ -138,24 +147,67 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
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()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
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;
Expand All @@ -29,7 +33,9 @@

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
{
Expand Down Expand Up @@ -122,7 +128,7 @@ public void testSimpleIdleRead() throws Exception
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");

Expand All @@ -135,24 +141,25 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
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()));
}
}

Expand Down

0 comments on commit 24f0ac2

Please sign in to comment.