LabelerTest.kt
TLDR
This file contains the LabelerTest
class, which is a test class for the Labeler
class in the shark
package. It includes three test methods: stringContentAsLabel
, labelOnUnreachableObject
, and threadNameLabel
. These methods test different functionalities of the Labeler
class.
Methods
stringContentAsLabel
This test method checks if the Labeler
class correctly labels a leaking object in a heap analysis. It writes a string path to a temporary HPROF file, creates an ObjectInspector
with a specific label for instances of the java.lang.String
class, performs a heap analysis on the HPROF file using the labeler
object inspector, and asserts that the leaking object in the heap analysis results has the expected label.
labelOnUnreachableObject
This test method checks if the Labeler
class correctly labels an unreachable object in a heap dump. It creates a heap dump with an instance of the com.example.SomeClass
class, creates an ObjectInspector
with a label based on the class name of instances of java.lang.Object
, performs a heap analysis on the heap dump using the labeler
object inspector, and asserts that the unreachable object in the heap analysis results has the expected label.
threadNameLabel
This test method checks if the Labeler
class correctly labels a thread object in a heap analysis. It writes a Java local leak with a specified thread class and thread name to a temporary HPROF file, performs a heap analysis on the HPROF file using the ObjectInspectors.THREAD
object inspector, and asserts that the origin object (first object in the leak trace) in the heap analysis results has the expected label.
Classes
LabelerTest
This test class is used to test the functionality of the Labeler
class. It includes three test methods: stringContentAsLabel
, labelOnUnreachableObject
, and threadNameLabel
. These methods test different functionalities of the Labeler
class by performing heap analysis and asserting the expected labels.
package shark
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File
class LabelerTest {
@get:Rule
var testFolder = TemporaryFolder()
private lateinit var hprofFile: File
@Before
fun setUp() {
hprofFile = testFolder.newFile("temp.hprof")
}
@Test fun stringContentAsLabel() {
hprofFile.writeSinglePathToString("World")
val labeler = ObjectInspector { reporter ->
reporter.whenInstanceOf("java.lang.String") { instance ->
labels += "Hello ${instance.readAsJavaString()}"
}
}
val analysis = hprofFile.checkForLeaks<HeapAnalysisSuccess>(objectInspectors = listOf(labeler))
val leakTrace = analysis.applicationLeaks[0].leakTraces.first()
assertThat(leakTrace.leakingObject.labels).contains("Hello World")
}
@Test fun labelOnUnreachableObject() {
val heapDump = dump {
"com.example.SomeClass" watchedInstance {
}
}
val labeler = ObjectInspector { reporter ->
reporter.whenInstanceOf("java.lang.Object") { instance ->
labels += instance.instanceClassSimpleName
}
}
val analysis = heapDump.checkForLeaks<HeapAnalysisSuccess>(objectInspectors = listOf(labeler))
assertThat(analysis.unreachableObjects[0].labels).contains("SomeClass")
}
@Test fun threadNameLabel() {
hprofFile.writeJavaLocalLeak(threadClass = "MyThread", threadName = "kroutine")
val analysis =
hprofFile.checkForLeaks<HeapAnalysisSuccess>(
objectInspectors = listOf(ObjectInspectors.THREAD)
)
val leak = analysis.applicationLeaks[0]
assertThat(leak.leakTraces.first().referencePath.first().originObject.labels).contains(
"Thread name: 'kroutine'"
)
}
}