Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky assertions in ConnectionPoolTest #12328

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

package org.eclipse.jetty.test.client.transport;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;

import org.eclipse.jetty.client.Destination;
Expand All @@ -28,24 +28,25 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import static org.awaitility.Awaitility.await;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.is;

public class ConnectionPoolTest extends AbstractTest
{
@ParameterizedTest
@MethodSource("transports")
public void testPreCreateConnections(Transport transport) throws Exception
{
int maxConnectionsPerDestination = 8;
prepareServer(transport, new EmptyServerHandler());
ConnectionListener serverConnections = new ConnectionListener();
connector.addBean(serverConnections);
server.start();

startClient(transport);
client.setMaxConnectionsPerDestination(8);
client.setMaxConnectionsPerDestination(maxConnectionsPerDestination);
if (transport == Transport.HTTPS)
((HttpClientTransportOverHTTP)client.getTransport()).setInitializeConnections(true);

Expand All @@ -54,15 +55,17 @@ public void testPreCreateConnections(Transport transport) throws Exception
destination.getConnectionPool().preCreateConnections(client.getMaxConnectionsPerDestination())
.get(5, TimeUnit.SECONDS);

// Verify that connections have been created.
List<Connection> connections = switch (transport)
// Verify that server connections have been created.
await().atMost(5, TimeUnit.SECONDS).untilAsserted(() ->
{
case HTTP, HTTPS -> serverConnections.filter(HttpConnection.class);
case H2C, H2 -> serverConnections.filter(HTTP2ServerConnection.class);
case H3 -> serverConnections.filter(ServerQuicConnection.class);
case FCGI -> serverConnections.filter(ServerFCGIConnection.class);
};
assertThat(connections, not(empty()));
switch (transport)
{
case HTTP, HTTPS -> assertThat(serverConnections.filter(HttpConnection.class).size(), is(maxConnectionsPerDestination));
case H2C, H2 -> assertThat(serverConnections.filter(HTTP2ServerConnection.class).size(), is(maxConnectionsPerDestination));
case H3 -> assertThat(serverConnections.filter(ServerQuicConnection.class).size(), is(1));
case FCGI -> assertThat(serverConnections.filter(ServerFCGIConnection.class).size(), is(maxConnectionsPerDestination));
}
});

// Verify that TLS was performed.
List<Connection> sslConnections = switch (transport)
Expand All @@ -72,15 +75,15 @@ public void testPreCreateConnections(Transport transport) throws Exception
};
if (sslConnections != null)
{
assertThat(sslConnections.size(), greaterThan(0));
assertThat(sslConnections.size(), is(maxConnectionsPerDestination));
sslConnections.forEach(c -> assertThat(c.getBytesIn(), greaterThan(0L)));
sslConnections.forEach(c -> assertThat(c.getBytesOut(), greaterThan(0L)));
}
}

private static class ConnectionListener implements Connection.Listener
{
private final List<Connection> connections = new ArrayList<>();
private final List<Connection> connections = new CopyOnWriteArrayList<>();

@Override
public void onOpened(Connection connection)
Expand Down
Loading