From 4c4097a29b85915da6f2510a0ff6e9ad89093e9b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 12 May 2020 22:41:56 +0100 Subject: [PATCH] Address flakiness in TomcatServletWebServerFactoryTests --- .../TomcatServletWebServerFactoryTests.java | 9 +++--- .../AbstractServletWebServerFactoryTests.java | 28 +++++++------------ 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java index aec1b7fa00..4e7e23ef00 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java @@ -574,10 +574,12 @@ class TomcatServletWebServerFactoryTests extends AbstractServletWebServerFactory blockingServlet.awaitQueue(); this.webServer.shutDownGracefully((result) -> { }); - Future unconnectableRequest = initiateGetRequest(HttpClients.createDefault(), port, "/"); + Object unconnectableRequest = Awaitility.await().until( + () -> initiateGetRequest(HttpClients.createDefault(), port, "/").get(), + (result) -> result instanceof Exception); + assertThat(unconnectableRequest).isInstanceOf(HttpHostConnectException.class); blockingServlet.admitOne(); assertThat(request.get()).isInstanceOf(HttpResponse.class); - assertThat(unconnectableRequest.get()).isInstanceOf(HttpHostConnectException.class); this.webServer.stop(); } @@ -600,11 +602,10 @@ class TomcatServletWebServerFactoryTests extends AbstractServletWebServerFactory assertThat(keepAliveRequest.get()).isInstanceOf(HttpResponse.class); Future request = initiateGetRequest(port, "/blocking"); blockingServlet.awaitQueue(); - blockingServlet.setBlocking(false); this.webServer.shutDownGracefully((result) -> { }); Object idleConnectionRequestResult = Awaitility.await().until(() -> { - Future idleConnectionRequest = initiateGetRequest(httpClient, port, "/blocking"); + Future idleConnectionRequest = initiateGetRequest(httpClient, port, "/"); Object result = idleConnectionRequest.get(); return result; }, (result) -> result instanceof Exception); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java index 655617df78..c51f1f0f19 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java @@ -1445,26 +1445,22 @@ public abstract class AbstractServletWebServerFactoryTests { private final BlockingQueue barriers = new ArrayBlockingQueue<>(10); - protected volatile boolean blocking = true; - public BlockingServlet() { } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - if (this.blocking) { - CyclicBarrier barrier = new CyclicBarrier(2); - this.barriers.add(barrier); - try { - barrier.await(); - } - catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - catch (BrokenBarrierException ex) { - throw new ServletException(ex); - } + CyclicBarrier barrier = new CyclicBarrier(2); + this.barriers.add(barrier); + try { + barrier.await(); + } + catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + catch (BrokenBarrierException ex) { + throw new ServletException(ex); } } @@ -1495,10 +1491,6 @@ public abstract class AbstractServletWebServerFactoryTests { } } - public void setBlocking(boolean blocking) { - this.blocking = blocking; - } - } static class BlockingAsyncServlet extends HttpServlet {