Skip to content

Commit

Permalink
#10226 - dump the heap when a buffer leak is detected
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 23, 2023
1 parent 67bdf8d commit 0bf1277
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@

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

import java.io.IOException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyStore;
import java.util.Collection;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import javax.management.MBeanServer;

import com.sun.management.HotSpotDiagnosticMXBean;
import org.awaitility.Awaitility;
import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory;
import org.eclipse.jetty.client.HttpClient;
Expand Down Expand Up @@ -62,6 +68,7 @@
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand Down Expand Up @@ -113,7 +120,7 @@ public static Collection<Transport> transportsTCP()
}

@AfterEach
public void dispose()
public void dispose(TestInfo testInfo) throws Exception
{
try
{
Expand All @@ -125,6 +132,8 @@ public void dispose()
}
catch (Exception e)
{
String className = testInfo.getTestClass().orElseThrow().getName();
dumpHeap("server-" + className);
fail(e.getMessage() + "\n---\nServer Leaks: " + serverBufferPool.dumpLeaks() + "---\n");
}
}
Expand All @@ -136,6 +145,8 @@ public void dispose()
}
catch (Exception e)
{
String className = testInfo.getTestClass().orElseThrow().getName();
dumpHeap("client-" + className);
fail(e.getMessage() + "\n---\nClient Leaks: " + clientBufferPool.dumpLeaks() + "---\n");
}
}
Expand All @@ -147,6 +158,24 @@ public void dispose()
}
}

private static void dumpHeap(String testMethodName) throws IOException
{
Path targetDir = Path.of("target/leaks");
try (Stream<Path> stream = Files.walk(targetDir))
{
stream.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(java.io.File::delete);
}
Files.createDirectories(targetDir);
String dumpName = targetDir.resolve(testMethodName + ".hprof").toString();

MBeanServer server = ManagementFactory.getPlatformMBeanServer();
HotSpotDiagnosticMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy(
server, "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);
mxBean.dumpHeap(dumpName, true);
}

protected void start(Transport transport, Handler handler) throws Exception
{
startServer(transport, handler);
Expand Down

0 comments on commit 0bf1277

Please sign in to comment.