forked from LittleProxy/LittleProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add debugging info for investigating flaky tests
* store thread dumps to "target" folder 3 seconds after every test start. * generate unique archive names for uploaded artifacts on different OSes
- Loading branch information
Showing
8 changed files
with
151 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/test/java/org/littleshoot/proxy/test/ThreadDumpExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package org.littleshoot.proxy.test; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.junit.jupiter.api.extension.AfterAllCallback; | ||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ExtensionContext.Namespace; | ||
import org.opentest4j.TestAbortedException; | ||
import org.slf4j.Logger; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.ThreadInfo; | ||
import java.lang.management.ThreadMXBean; | ||
import java.lang.reflect.Method; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.util.concurrent.Executors.newScheduledThreadPool; | ||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
public class ThreadDumpExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback { | ||
private static final Namespace NAMESPACE = Namespace.create("ThreadDumpExtension"); | ||
private static final int INITIAL_DELAY_MS = 5000; | ||
private static final int DELAY_MS = 1000; | ||
|
||
@Override | ||
public void beforeAll(ExtensionContext context) { | ||
getLogger(context.getDisplayName()).info("Starting tests ({})", memory()); | ||
} | ||
|
||
@Override | ||
public void afterAll(ExtensionContext context) { | ||
getLogger(context.getDisplayName()).info("Finished tests - {} ({})", verdict(context), memory()); | ||
} | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext context) { | ||
Logger log = logger(context); | ||
log.info("starting {} ({})...", context.getDisplayName(), memory()); | ||
ScheduledExecutorService executor = newScheduledThreadPool(1); | ||
executor.scheduleWithFixedDelay(() -> takeThreadDump(log), INITIAL_DELAY_MS, DELAY_MS, MILLISECONDS); | ||
context.getStore(NAMESPACE).put("executor", executor); | ||
} | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) { | ||
logger(context).info("finished {} - {} ({})", context.getDisplayName(), verdict(context), memory()); | ||
ScheduledExecutorService executor = (ScheduledExecutorService) context.getStore(NAMESPACE).remove("executor"); | ||
executor.shutdown(); | ||
} | ||
|
||
private static Logger logger(ExtensionContext context) { | ||
return getLogger(context.getRequiredTestClass().getSimpleName() + '.' + context.getTestMethod().map(Method::getName).orElse("?")); | ||
} | ||
|
||
private String verdict(ExtensionContext context) { | ||
return context.getExecutionException().isPresent() ? | ||
(context.getExecutionException().get() instanceof TestAbortedException ? "skipped" : "NOK") : | ||
"OK"; | ||
} | ||
|
||
private String memory() { | ||
long freeMemory = Runtime.getRuntime().freeMemory(); | ||
long maxMemory = Runtime.getRuntime().maxMemory(); | ||
long totalMemory = Runtime.getRuntime().totalMemory(); | ||
long usedMemory = totalMemory - freeMemory; | ||
return "memory used:" + mb(usedMemory) + ", free:" + mb(freeMemory) + ", total:" + mb(totalMemory) + ", max:" + mb(maxMemory); | ||
} | ||
|
||
private long mb(long bytes) { | ||
return bytes / 1024 / 1024; | ||
} | ||
|
||
private void takeThreadDump(Logger log) { | ||
StringBuilder threadDump = new StringBuilder(); | ||
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); | ||
for (ThreadInfo threadInfo : threadMXBean.dumpAllThreads(true, true, 50)) { | ||
threadDump.append(threadInfo.toString()); | ||
} | ||
File dump = new File("target/thread-dump-" + System.currentTimeMillis() + ".txt"); | ||
try { | ||
FileUtils.writeStringToFile(dump, threadDump.toString(), UTF_8); | ||
log.info("Saved thread dump to file {}", dump); | ||
} | ||
catch (IOException e) { | ||
log.error("Failed to save thread dump to file {}", dump, e); | ||
} | ||
} | ||
} |
Oops, something went wrong.