Skip to content
Merged
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
46 changes: 28 additions & 18 deletions gradle/dump_hanging_test.gradle
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit

project.ext.tasksDumps = new ConcurrentHashMap<String, ScheduledFuture<?>>()
project.ext.taskDumpsScheduler = Executors.newScheduledThreadPool(4, { r ->
Thread t = new Thread(r, "dump-scheduler")
t.daemon = true
t
})

// Schedule thread and heap dumps collection near test timeout.
tasks.withType(Test).configureEach { testTask ->
doFirst {
def scheduler = Executors.newSingleThreadScheduledExecutor({ r ->
Thread t = new Thread(r, 'dump-scheduler')
t.daemon = true
t
})
if (project.ext.tasksDumps.containsKey(testTask.path)) {
logger.warn("Taking dumps already scheduled: ${testTask.path}; skipping.")
return
}

if (!testTask.timeout.present) {
logger.info("No timeout for ${testTask.path}; skipping dumps scheduler.")
return
}

// Calculate delay for taking dumps as test timeout minus 1 minutes, but no less than 1 minute.
def delayMinutes = Math.max(1L, timeout.get().minusMinutes(1).toMinutes())
def delayMinutes = Math.max(1L, testTask.timeout.get().minusMinutes(1).toMinutes())

def future = scheduler.schedule({
logger.warn("Taking dumps for: ${testTask.getPath()} after ${delayMinutes} minutes.")
def future = project.ext.taskDumpsScheduler.schedule({
logger.warn("Taking dumps for: ${testTask.path} after ${delayMinutes} minutes.")

try {
// Use Gradle's build dir and adjust for CI artifacts collection if needed.
def dumpsDir = layout.buildDirectory.dir('dumps').map {
if (providers.environmentVariable("CI").isPresent()) {
// Move reports into the folder collected by the collect_reports.sh script.
new File(it.getAsFile().absolutePath.replace('dd-trace-java/dd-java-agent', 'dd-trace-java/workspace/dd-java-agent'))
new File(it.asFile.absolutePath.replace('dd-trace-java/dd-java-agent', 'dd-trace-java/workspace/dd-java-agent'))
} else {
it.asFile
}
Expand Down Expand Up @@ -62,22 +75,19 @@ tasks.withType(Test).configureEach { testTask ->
} catch (Throwable e) {
logger.warn("Dumping failed: ${e.message}")
}
finally {
scheduler.shutdown()
}
}, delayMinutes, TimeUnit.MINUTES)

// Store handles for cancellation in doLast.
testTask.ext.dumpFuture = future
testTask.ext.dumpScheduler = scheduler
// Store future for cancellation in doLast.
project.ext.tasksDumps.put(testTask.path, future)
}

doLast {
// Cancel if the task finished before the scheduled dump.
try {
testTask.ext.dumpFuture?.cancel(false)
} finally {
testTask.ext.dumpScheduler?.shutdownNow()
def future = project.ext.tasksDumps.remove(testTask.path)
future?.cancel(false)
} catch(Throwable e) {
logger.warn("Failed to cancel dump future for ${testTask.path}: ${e.message}")
}
}
}