Introduced leak tracking for buffers and connections in tests.

This commit is contained in:
Simone Bordet 2014-01-14 12:44:55 +01:00
parent d3126d8345
commit 0492e34c9e
2 changed files with 70 additions and 12 deletions

View File

@ -18,23 +18,34 @@
package org.eclipse.jetty.fcgi.client.http;
import java.util.concurrent.atomic.AtomicLong;
import org.eclipse.jetty.client.ConnectionPool;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpDestination;
import org.eclipse.jetty.client.LeakTrackingConnectionPool;
import org.eclipse.jetty.client.Origin;
import org.eclipse.jetty.fcgi.server.ServerFCGIConnectionFactory;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.io.ArrayByteBufferPool;
import org.eclipse.jetty.io.LeakTrackingByteBufferPool;
import org.eclipse.jetty.io.MappedByteBufferPool;
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.toolchain.test.TestTracker;
import org.eclipse.jetty.util.LeakDetector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
public abstract class AbstractHttpClientServerTest
{
@Rule
public final TestTracker tracker = new TestTracker();
private final AtomicLong leaks = new AtomicLong();
protected Server server;
protected ServerConnector connector;
protected HttpClient client;
@ -45,7 +56,16 @@ public abstract class AbstractHttpClientServerTest
server = new Server();
ServerFCGIConnectionFactory fcgiConnectionFactory = new ServerFCGIConnectionFactory(new HttpConfiguration());
connector = new ServerConnector(server, fcgiConnectionFactory);
connector = new ServerConnector(server, null, null,
new LeakTrackingByteBufferPool(new ArrayByteBufferPool())
{
@Override
protected void leaked(LeakDetector.LeakInfo leakInfo)
{
leaks.incrementAndGet();
}
}, 1, Math.max(1, Runtime.getRuntime().availableProcessors() / 2), fcgiConnectionFactory);
// connector.setPort(9000);
server.addConnector(connector);
server.setHandler(handler);
@ -54,14 +74,46 @@ public abstract class AbstractHttpClientServerTest
QueuedThreadPool executor = new QueuedThreadPool();
executor.setName(executor.getName() + "-client");
client = new HttpClient(new HttpClientTransportOverFCGI(""), null);
client = new HttpClient(new HttpClientTransportOverFCGI(1, false, "")
{
@Override
public HttpDestination newHttpDestination(Origin origin)
{
return new HttpDestinationOverFCGI(client, origin)
{
@Override
protected ConnectionPool newConnectionPool(HttpClient client)
{
return new LeakTrackingConnectionPool(this, client.getMaxConnectionsPerDestination(), this)
{
@Override
protected void leaked(LeakDetector.LeakInfo leakInfo)
{
leaks.incrementAndGet();
}
};
}
};
}
}, null);
client.setExecutor(executor);
client.setByteBufferPool(new LeakTrackingByteBufferPool(new MappedByteBufferPool())
{
@Override
protected void leaked(LeakDetector.LeakInfo leakInfo)
{
leaks.incrementAndGet();
}
});
client.start();
}
@After
public void dispose() throws Exception
{
System.gc();
Assert.assertEquals(0, leaks.get());
if (client != null)
client.stop();
if (server != null)

View File

@ -74,7 +74,10 @@ public class HttpClientTest extends AbstractHttpClientServerTest
}
});
for (int i = 0; i < 2; ++i)
int maxConnections = 256;
client.setMaxConnectionsPerDestination(maxConnections);
for (int i = 0; i < maxConnections + 1; ++i)
{
ContentResponse response = client.GET(scheme + "://localhost:" + connector.getLocalPort());
Assert.assertNotNull(response);
@ -269,6 +272,8 @@ public class HttpClientTest extends AbstractHttpClientServerTest
}
});
for (int i = 0; i < 256; ++i)
{
ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort() + "/?b=1")
.param(paramName, paramValue)
.content(new BytesContentProvider(content))
@ -279,6 +284,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(content, response.getContent());
}
}
@Test
public void testPOSTWithContentNotifiesRequestContentListener() throws Exception