-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathDumpHangedTestIntegrationTest.kt
More file actions
123 lines (98 loc) · 3.72 KB
/
Copy pathDumpHangedTestIntegrationTest.kt
File metadata and controls
123 lines (98 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package datadog.gradle.plugin.version
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.UnexpectedBuildFailure
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertNotNull
import org.junit.jupiter.api.io.TempDir
import java.io.File
import java.nio.file.Paths
class DumpHangedTestIntegrationTest {
@Test
fun `should not take dumps`(@TempDir projectDir: File) {
val output = runGradleTest(projectDir, testSleep = 1000)
// Assert Gradle output has no evidence of taking dumps.
assertFalse(output.contains("Taking dumps after 15 seconds delay for :test"))
assertFalse(output.contains("Requesting stop of task ':test' as it has exceeded its configured timeout of 20s."))
assertTrue(file(projectDir, "build").exists()) // Assert build happened.
assertFalse(file(projectDir, "build", "dumps").exists()) // Assert no dumps created.
}
@Test
fun `should take dumps`(@TempDir projectDir: File) {
val output = runGradleTest(projectDir, testSleep = 25_0000)
// Assert Gradle output has evidence of taking dumps.
assertTrue(output.contains("Taking dumps after 15 seconds delay for :test"))
assertTrue(output.contains("Requesting stop of task ':test' as it has exceeded its configured timeout of 20s."))
assertTrue(file(projectDir, "build").exists()) // Assert build happened.
val dumps = file(projectDir, "build", "dumps")
assertTrue(dumps.exists()) // Assert dumps created.
// Assert actual dumps created.
val dumpFiles = dumps.list()
assertNotNull(dumpFiles.find { it.endsWith(".hprof") })
assertNotNull(dumpFiles.find { it.startsWith("all-thread-dumps") })
}
private fun runGradleTest(projectDir: File, testSleep: Long): List<String> {
file(projectDir, "settings.gradle.kts").writeText(
"""
rootProject.name = "test-project"
""".trimIndent()
)
file(projectDir, "build.gradle.kts").writeText(
"""
import java.time.Duration
plugins {
id("java")
id("datadog.dump-hanged-test")
}
group = "datadog.dump.test"
repositories {
mavenCentral()
}
dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
dumpHangedTest {
// Set the dump offset for 5 seconds to trigger taking dumps after 15 seconds.
dumpOffset.set(5)
}
tasks.withType<Test>().configureEach {
// Set test timeout after 20 seconds.
timeout.set(Duration.ofSeconds(20))
useJUnitPlatform()
}
""".trimIndent()
)
file(projectDir, "src", "test", "java", "SimpleTest.java", makeDirectory = true).writeText(
"""
import org.junit.jupiter.api.Test;
public class SimpleTest {
@Test
public void test() throws InterruptedException {
Thread.sleep($testSleep);
}
}
""".trimIndent()
)
try {
val buildResult = GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("test")
.withProjectDir(projectDir)
.build()
return buildResult.output.lines()
} catch (e: UnexpectedBuildFailure) {
return e.buildResult.output.lines()
}
}
private fun file(projectDir: File, vararg parts: String, makeDirectory: Boolean = false): File {
val f = Paths.get(projectDir.absolutePath, *parts).toFile()
if (makeDirectory) {
f.parentFile.mkdirs()
}
return f
}
}