|
1 | 1 | package shark |
2 | 2 |
|
| 3 | +import java.io.File |
3 | 4 | import org.assertj.core.api.Assertions.assertThat |
4 | 5 | import org.junit.Test |
| 6 | +import shark.HprofHeapGraph.Companion.openHeapGraph |
5 | 7 | import shark.LeakTraceObject.LeakingStatus.LEAKING |
6 | 8 | import shark.LeakTraceObject.LeakingStatus.NOT_LEAKING |
7 | 9 | import shark.LeakTraceObject.ObjectType.INSTANCE |
| 10 | +import shark.ValueHolder.BooleanHolder |
| 11 | +import shark.ValueHolder.IntHolder |
8 | 12 |
|
9 | 13 | class AndroidObjectInspectorsTest { |
10 | 14 |
|
@@ -65,4 +69,59 @@ class AndroidObjectInspectorsTest { |
65 | 69 | assertThat(recomposerNode.originObject.leakingStatus).isEqualTo(LEAKING) |
66 | 70 | assertThat(recomposerNode.originObject.leakingStatusReason).isEqualTo("Composition disposed") |
67 | 71 | } |
| 72 | + |
| 73 | + @Test fun `COMPOSITION_IMPL with old disposed field true should be leaking`() { |
| 74 | + val analysis = analyzeCompositionImpl(mapOf("disposed" to BooleanHolder(true))) |
| 75 | + val unreachableObject = analysis.unreachableObjects.single() |
| 76 | + |
| 77 | + assertThat(unreachableObject.leakingStatus).isEqualTo(LEAKING) |
| 78 | + assertThat(unreachableObject.leakingStatusReason) |
| 79 | + .contains("Composition disposed") |
| 80 | + } |
| 81 | + |
| 82 | + @Test fun `COMPOSITION_IMPL with new state field DISPOSED should be leaking`() { |
| 83 | + val analysis = analyzeCompositionImpl(mapOf("state" to IntHolder(3))) // DISPOSED = 3 |
| 84 | + val unreachableObject = analysis.unreachableObjects.single() |
| 85 | + |
| 86 | + assertThat(unreachableObject.leakingStatus).isEqualTo(LEAKING) |
| 87 | + assertThat(unreachableObject.leakingStatusReason) |
| 88 | + .contains("Composition disposed") |
| 89 | + } |
| 90 | + |
| 91 | + @Test fun `COMPOSITION_IMPL with new state field RUNNING should not be leaking`() { |
| 92 | + val analysis = analyzeCompositionImpl(mapOf("state" to IntHolder(0))) // RUNNING = 0 |
| 93 | + val unreachableObject = analysis.unreachableObjects.single() |
| 94 | + |
| 95 | + // Note: Status is still LEAKING because this is a watchedInstance (tracked by ObjectWatcher) |
| 96 | + // but the inspector provides the correct reason explaining why it's not actually leaking |
| 97 | + assertThat(unreachableObject.leakingStatus).isEqualTo(LEAKING) |
| 98 | + assertThat(unreachableObject.leakingStatusReason) |
| 99 | + .contains("Composition running") |
| 100 | + } |
| 101 | + |
| 102 | + private fun analyzeCompositionImpl(fields: Map<String, ValueHolder>): HeapAnalysisSuccess { |
| 103 | + val heapDump = dump { |
| 104 | + "androidx.compose.runtime.CompositionImpl" watchedInstance { |
| 105 | + fields.forEach { (name, value) -> |
| 106 | + field[name] = value |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + val heapAnalyzer = HeapAnalyzer(OnAnalysisProgressListener.NO_OP) |
| 112 | + |
| 113 | + return heapDump.openHeapGraph().use { graph: HeapGraph -> |
| 114 | + heapAnalyzer.analyze( |
| 115 | + heapDumpFile = File("/no/file"), |
| 116 | + graph = graph, |
| 117 | + leakingObjectFinder = FilteringLeakingObjectFinder( |
| 118 | + ObjectInspectors.jdkLeakingObjectFilters |
| 119 | + ), |
| 120 | + referenceMatchers = JdkReferenceMatchers.defaults, |
| 121 | + computeRetainedHeapSize = false, |
| 122 | + objectInspectors = listOf(ObjectInspectors.KEYED_WEAK_REFERENCE, AndroidObjectInspectors.COMPOSITION_IMPL), |
| 123 | + metadataExtractor = MetadataExtractor.NO_OP |
| 124 | + ) |
| 125 | + } as HeapAnalysisSuccess |
| 126 | + } |
68 | 127 | } |
0 commit comments