diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/ConnectionPoolTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/ConnectionPoolTest.java index ccecb188b969..31838e0eb7a5 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/ConnectionPoolTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/ConnectionPoolTest.java @@ -54,6 +54,8 @@ import org.junit.jupiter.params.provider.MethodSource; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.lessThanOrEqualTo; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -75,11 +77,6 @@ public static Stream pools() return Stream.of(DUPLEX, MULTIPLEX, RANDOM, DUPLEX_MAX_DURATION, ROUND_ROBIN); } - public static Stream poolsNoMaxDuration() - { - return Stream.of(DUPLEX, MULTIPLEX, RANDOM, ROUND_ROBIN); - } - public static Stream poolsNoRoundRobin() { return Stream.of(DUPLEX, MULTIPLEX, RANDOM, DUPLEX_MAX_DURATION); @@ -316,7 +313,10 @@ public void resolve(String host, int port, Promise> prom assertEquals(1, destinations.size()); HttpDestination destination = (HttpDestination)destinations.get(0); AbstractConnectionPool connectionPool = (AbstractConnectionPool)destination.getConnectionPool(); - assertEquals(2, connectionPool.getConnectionCount()); + if (DUPLEX_MAX_DURATION == factory) + assertThat(connectionPool.getConnectionCount(), lessThanOrEqualTo(2)); // The connections can expire upon release. + else + assertThat(connectionPool.getConnectionCount(), is(2)); } @ParameterizedTest @@ -376,8 +376,7 @@ public void resolve(String host, int port, Promise> prom } @ParameterizedTest - // Connection pool aggressively closes expired connections upon release, which interferes with this test's assertion. - @MethodSource("poolsNoMaxDuration") + @MethodSource("pools") public void testConcurrentRequestsAllBlockedOnServerWithLargeConnectionPool(ConnectionPoolFactory factory) throws Exception { int count = 50; @@ -385,8 +384,7 @@ public void testConcurrentRequestsAllBlockedOnServerWithLargeConnectionPool(Conn } @ParameterizedTest - // Connection pool aggressively closes expired connections upon release, which interferes with this test's assertion. - @MethodSource("poolsNoMaxDuration") + @MethodSource("pools") public void testConcurrentRequestsAllBlockedOnServerWithExactConnectionPool(ConnectionPoolFactory factory) throws Exception { int count = 50; @@ -449,9 +447,13 @@ protected void service(String target, org.eclipse.jetty.server.Request jettyRequ assertTrue(latch.await(5, TimeUnit.SECONDS), "server requests " + barrier.getNumberWaiting() + "<" + count + " - client: " + client.dump()); List destinations = client.getDestinations(); assertEquals(1, destinations.size()); - HttpDestination destination = (HttpDestination)destinations.get(0); - AbstractConnectionPool connectionPool = (AbstractConnectionPool)destination.getConnectionPool(); - assertThat(connectionPool.getConnectionCount(), Matchers.greaterThanOrEqualTo(count)); + // The max duration connection pool aggressively closes expired connections upon release, which interferes with this assertion. + if (DUPLEX_MAX_DURATION != factory) + { + HttpDestination destination = (HttpDestination)destinations.get(0); + AbstractConnectionPool connectionPool = (AbstractConnectionPool)destination.getConnectionPool(); + assertThat(connectionPool.getConnectionCount(), Matchers.greaterThanOrEqualTo(count)); + } } @Test @@ -588,8 +590,17 @@ public void testConnectionMaxUsage(ConnectionPoolFactory factory) throws Excepti AbstractConnectionPool connectionPool = (AbstractConnectionPool)destination.getConnectionPool(); assertEquals(0, connectionPool.getActiveConnectionCount()); - assertEquals(1, connectionPool.getIdleConnectionCount()); - assertEquals(1, connectionPool.getConnectionCount()); + if (DUPLEX_MAX_DURATION == factory) + { + // The connections can expire upon release. + assertThat(connectionPool.getIdleConnectionCount(), lessThanOrEqualTo(1)); + assertThat(connectionPool.getConnectionCount(), lessThanOrEqualTo(1)); + } + else + { + assertThat(connectionPool.getIdleConnectionCount(), is(1)); + assertThat(connectionPool.getConnectionCount(), is(1)); + } // Send second request, max usage count will be reached, // the only connection must be closed. @@ -625,7 +636,10 @@ public void testIdleTimeoutNoRequests(ConnectionPoolFactory factory) throws Exce // Trigger the creation of a destination, that will create the connection pool. HttpDestination destination = client.resolveDestination(new Origin("http", "localhost", connector.getLocalPort())); AbstractConnectionPool connectionPool = (AbstractConnectionPool)destination.getConnectionPool(); - assertEquals(1, connectionPool.getConnectionCount()); + if (DUPLEX_MAX_DURATION == factory) + assertThat(connectionPool.getConnectionCount(), lessThanOrEqualTo(1)); // The connections can expire upon release. + else + assertThat(connectionPool.getConnectionCount(), is(1)); // Wait for the pre-created connections to idle timeout. Thread.sleep(idleTimeout + idleTimeout / 2);