Skip to content

Commit

Permalink
#10226 - restore leak tracking in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <lorban@bitronix.be>
  • Loading branch information
lorban committed Aug 16, 2023
1 parent 24792db commit 39ae011
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,18 @@

package org.eclipse.jetty.fcgi.server;

import java.util.concurrent.atomic.AtomicLong;

import org.eclipse.jetty.client.Connection;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpClientTransport;
import org.eclipse.jetty.client.LeakTrackingConnectionPool;
import org.eclipse.jetty.fcgi.client.transport.HttpClientTransportOverFCGI;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.io.ArrayByteBufferPool;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.ClientConnector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.LeakDetector;
import org.eclipse.jetty.util.ProcessorUtils;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -38,9 +33,8 @@

public abstract class AbstractHttpClientServerTest
{
private ByteBufferPool serverBufferPool;
protected ByteBufferPool clientBufferPool;
private final AtomicLong connectionLeaks = new AtomicLong();
private ArrayByteBufferPool.Tracking serverBufferPool;
protected ArrayByteBufferPool.Tracking clientBufferPool;
protected Server server;
protected ServerConnector connector;
protected HttpClient client;
Expand All @@ -52,8 +46,7 @@ public void start(Handler handler) throws Exception
serverThreads.setName("server");
server = new Server(serverThreads);
ServerFCGIConnectionFactory fcgiConnectionFactory = new ServerFCGIConnectionFactory(new HttpConfiguration());
// TODO: restore leak tracking.
serverBufferPool = new ArrayByteBufferPool();
serverBufferPool = new ArrayByteBufferPool.Tracking();
connector = new ServerConnector(server, null, null, serverBufferPool,
1, Math.max(1, ProcessorUtils.availableProcessors() / 2), fcgiConnectionFactory);
server.addConnector(connector);
Expand All @@ -65,33 +58,28 @@ public void start(Handler handler) throws Exception
QueuedThreadPool clientThreads = new QueuedThreadPool();
clientThreads.setName("client");
clientConnector.setExecutor(clientThreads);
// TODO: restore leak tracking.
if (clientBufferPool == null)
clientBufferPool = new ArrayByteBufferPool();
clientBufferPool = new ArrayByteBufferPool.Tracking();
clientConnector.setByteBufferPool(clientBufferPool);
HttpClientTransport transport = new HttpClientTransportOverFCGI(clientConnector, "");
transport.setConnectionPoolFactory(destination -> new LeakTrackingConnectionPool(destination, client.getMaxConnectionsPerDestination())
{
@Override
protected void leaked(LeakDetector<Connection>.LeakInfo leakInfo)
{
connectionLeaks.incrementAndGet();
}
});
client = new HttpClient(transport);
client.start();
}

@AfterEach
public void dispose() throws Exception
public void dispose()
{
System.gc();

assertThat("Connection Leaks", connectionLeaks.get(), Matchers.is(0L));

if (client != null)
client.stop();
if (server != null)
server.stop();
try
{
if (serverBufferPool != null)
assertThat("Server Leaks: " + serverBufferPool.getLeaks(), serverBufferPool.getLeaks().size(), Matchers.is(0));
if (clientBufferPool != null)
assertThat("Client Leaks: " + clientBufferPool.getLeaks(), clientBufferPool.getLeaks().size(), Matchers.is(0));
}
finally
{
LifeCycle.stop(client);
LifeCycle.stop(server);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
import javax.net.ssl.SSLContext;

import org.eclipse.jetty.io.ArrayByteBufferPool;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -72,7 +73,12 @@ public static Stream<Arguments> scenarios()
List<Scenario> params = new ArrayList<>();

// HTTP
ConnectorProvider http = ServerConnector::new;
ConnectorProvider http = (server, acceptors, selectors) ->
{
ArrayByteBufferPool.Tracking pool = new ArrayByteBufferPool.Tracking();
HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory();
return new ServerConnector(server, null, null, pool, acceptors, selectors, httpConnectionFactory);
};
ClientSocketProvider httpClient = Socket::new;
params.add(new Scenario("http", http, httpClient));

Expand All @@ -83,9 +89,7 @@ public static Stream<Arguments> scenarios()
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath(keystorePath.toString());
sslContextFactory.setKeyStorePassword("storepwd");
// TODO: restore leak tracking.
// ByteBufferPool pool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged());
ByteBufferPool pool = new ArrayByteBufferPool();
ArrayByteBufferPool.Tracking pool = new ArrayByteBufferPool.Tracking();

HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory();
ServerConnector connector = new ServerConnector(server, null, null, pool, acceptors, selectors,
Expand Down Expand Up @@ -147,7 +151,15 @@ private Server prepareServer(Scenario scenario, Handler handler)
@AfterEach
public void dispose() throws Exception
{
_server.stop();
ArrayByteBufferPool.Tracking byteBufferPool = (ArrayByteBufferPool.Tracking)_server.getConnectors()[0].getByteBufferPool();
try
{
assertThat("Server Leaks: " + byteBufferPool.getLeaks(), byteBufferPool.getLeaks().size(), Matchers.is(0));
}
finally
{
LifeCycle.stop(_server);
}
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
import javax.net.ssl.TrustManagerFactory;

import org.eclipse.jetty.io.ArrayByteBufferPool;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.server.AbstractConnectionFactory;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
Expand Down Expand Up @@ -62,18 +63,15 @@ public void startServer() throws Exception
{
server = new Server();

ServerConnector httpConnector = new ServerConnector(server);
ServerConnector httpConnector = new ServerConnector(server, null, null, new ArrayByteBufferPool.Tracking(), 1, 1, new HttpConnectionFactory());
httpConnector.setPort(0);
server.addConnector(httpConnector);

File keystorePath = MavenTestingUtils.getTestResourceFile("keystore.p12");
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath(keystorePath.getAbsolutePath());
sslContextFactory.setKeyStorePassword("storepwd");
// TODO: restore leak tracking.
// ByteBufferPool pool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged());
ByteBufferPool pool = new ArrayByteBufferPool();
ServerConnector sslConnector = new ServerConnector(server, null, null, pool, 1, 1,
ServerConnector sslConnector = new ServerConnector(server, null, null, new ArrayByteBufferPool.Tracking(), 1, 1,
AbstractConnectionFactory.getFactories(sslContextFactory, new HttpConnectionFactory()));

server.addConnector(sslConnector);
Expand Down Expand Up @@ -130,7 +128,15 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
@AfterEach
public void stopServer() throws Exception
{
server.stop();
ArrayByteBufferPool.Tracking byteBufferPool = (ArrayByteBufferPool.Tracking)server.getConnectors()[0].getByteBufferPool();
try
{
assertThat("Server Leaks: " + byteBufferPool.getLeaks(), byteBufferPool.getLeaks().size(), Matchers.is(0));
}
finally
{
LifeCycle.stop(server);
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import javax.net.ssl.TrustManagerFactory;

import org.eclipse.jetty.io.ArrayByteBufferPool;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.server.AbstractConnectionFactory;
import org.eclipse.jetty.server.Handler;
Expand All @@ -43,6 +42,7 @@
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -73,9 +73,7 @@ public void init() throws Exception
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath(keystorePath);
sslContextFactory.setKeyStorePassword("storepwd");
// TODO: restore leak tracking.
// ByteBufferPool pool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged());
ByteBufferPool pool = new ArrayByteBufferPool();
ArrayByteBufferPool.Tracking pool = new ArrayByteBufferPool.Tracking();

HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory();
ServerConnector connector = new ServerConnector(_server, null, null, pool, 1, 1, AbstractConnectionFactory.getFactories(sslContextFactory, httpConnectionFactory));
Expand Down Expand Up @@ -109,6 +107,13 @@ public void init() throws Exception
}
}

@AfterEach
public void dispose() throws Exception
{
ArrayByteBufferPool.Tracking byteBufferPool = (ArrayByteBufferPool.Tracking)_server.getConnectors()[0].getByteBufferPool();
assertThat("Server Leaks: " + byteBufferPool.getLeaks(), byteBufferPool.getLeaks().size(), Matchers.is(0));
}

@Override
protected Socket newSocket(String host, int port) throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AbstractTest
Expand All @@ -75,6 +77,8 @@ public class AbstractTest
protected AbstractConnector connector;
protected HttpClient client;
protected Path unixDomainPath;
protected ArrayByteBufferPool.Tracking serverBufferPool;
protected ArrayByteBufferPool.Tracking clientBufferPool;

public static Collection<Transport> transports()
{
Expand Down Expand Up @@ -109,8 +113,18 @@ public static Collection<Transport> transportsTCP()
@AfterEach
public void dispose()
{
LifeCycle.stop(client);
LifeCycle.stop(server);
try
{
if (serverBufferPool != null)
assertThat("Server Leaks: " + serverBufferPool.getLeaks(), serverBufferPool.getLeaks().size(), Matchers.is(0));
if (clientBufferPool != null)
assertThat("Client Leaks: " + clientBufferPool.getLeaks(), clientBufferPool.getLeaks().size(), Matchers.is(0));
}
finally
{
LifeCycle.stop(client);
LifeCycle.stop(server);
}
}

protected void start(Transport transport, Handler handler) throws Exception
Expand Down Expand Up @@ -146,8 +160,8 @@ protected Server newServer()
{
QueuedThreadPool serverThreads = new QueuedThreadPool();
serverThreads.setName("server");
// TODO: restore leak tracking.
return new Server(serverThreads, null, new ArrayByteBufferPool());
serverBufferPool = new ArrayByteBufferPool.Tracking();
return new Server(serverThreads, null, serverBufferPool);
}

protected SslContextFactory.Server newSslContextFactoryServer() throws Exception
Expand Down Expand Up @@ -176,6 +190,8 @@ protected void startClient(Transport transport) throws Exception
QueuedThreadPool clientThreads = new QueuedThreadPool();
clientThreads.setName("client");
client = new HttpClient(newHttpClientTransport(transport));
clientBufferPool = new ArrayByteBufferPool.Tracking();
client.setByteBufferPool(clientBufferPool);
client.setExecutor(clientThreads);
client.setSocketAddressResolver(new SocketAddressResolver.Sync());
client.start();
Expand Down
Loading

0 comments on commit 39ae011

Please sign in to comment.