Skip to content

Commit bd2dc1f

Browse files
committed
rename and improve zip approach in success case
1 parent afd39cb commit bd2dc1f

File tree

3 files changed

+28
-55
lines changed

3 files changed

+28
-55
lines changed

leakcanary/leakcanary-jvm-test/src/test/java/leakcanary/JvmLiveObjectGrowthDetectorTest.kt

+10-39
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,12 @@ class JvmLiveObjectGrowthDetectorTest {
249249
}
250250

251251
@Test
252-
fun `DeleteOnObjectsNotGrowing does invokes file deletion in between each scenario`() {
252+
fun `KeepHeapDumpsOnObjectsGrowing does invokes file deletion in between each scenario`() {
253253
var didDeleteFile = false
254254
val heapDumpDirectory = tempFolder.newFolder()
255255
val detector = HeapDiff.repeatingJvmInProcessScenario(
256256
objectGrowthDetector = ObjectGrowthDetector.forJvmHeapNoSyntheticRefs(),
257-
heapDumpDeletionStrategy = HeapDumpDeletionStrategy.DeleteOnObjectsNotGrowing {
257+
heapDumpDeletionStrategy = HeapDumpDeletionStrategy.KeepHeapDumpsOnObjectsGrowing {
258258
didDeleteFile = true
259259
it.delete()
260260
},
@@ -271,12 +271,12 @@ class JvmLiveObjectGrowthDetectorTest {
271271
}
272272

273273
@Test
274-
fun `DeleteOnObjectsNotGrowing does not delete any heap dump if objects growing`() {
274+
fun `KeepHeapDumpsOnObjectsGrowing does not delete any heap dump if objects growing`() {
275275
val maxHeapDump = 5
276276
val heapDumpDirectory = tempFolder.newFolder()
277277
val detector = HeapDiff.repeatingJvmInProcessScenario(
278278
objectGrowthDetector = ObjectGrowthDetector.forJvmHeapNoSyntheticRefs(),
279-
heapDumpDeletionStrategy = HeapDumpDeletionStrategy.DeleteOnObjectsNotGrowing(),
279+
heapDumpDeletionStrategy = HeapDumpDeletionStrategy.KeepHeapDumpsOnObjectsGrowing(),
280280
heapDumpDirectoryProvider = { heapDumpDirectory }
281281
)
282282

@@ -292,12 +292,12 @@ class JvmLiveObjectGrowthDetectorTest {
292292
}
293293

294294
@Test
295-
fun `DeleteOnObjectsNotGrowing invokes file deletion on completion if objects not growing`() {
295+
fun `KeepHeapDumpsOnObjectsGrowing invokes file deletion on completion if objects not growing`() {
296296
var filesDeleted = 0
297297
val heapDumpDirectory = tempFolder.newFolder()
298298
val detector = HeapDiff.repeatingJvmInProcessScenario(
299299
objectGrowthDetector = ObjectGrowthDetector.forJvmHeapNoSyntheticRefs(),
300-
heapDumpDeletionStrategy = HeapDumpDeletionStrategy.DeleteOnObjectsNotGrowing {
300+
heapDumpDeletionStrategy = HeapDumpDeletionStrategy.KeepHeapDumpsOnObjectsGrowing {
301301
filesDeleted++
302302
it.delete()
303303
},
@@ -321,12 +321,12 @@ class JvmLiveObjectGrowthDetectorTest {
321321
}
322322

323323
@Test
324-
fun `ZipAndDeleteOnObjectsNotGrowing leaves zipped heap dumps if objects growing`() {
324+
fun `KeepZippedHeapDumpsOnObjectsGrowing leaves zipped heap dumps if objects growing`() {
325325
val maxHeapDump = 5
326326
val heapDumpDirectory = tempFolder.newFolder()
327327
val detector = HeapDiff.repeatingJvmInProcessScenario(
328328
objectGrowthDetector = ObjectGrowthDetector.forJvmHeapNoSyntheticRefs(),
329-
heapDumpDeletionStrategy = HeapDumpDeletionStrategy.ZipAndDeleteOnObjectsNotGrowing(),
329+
heapDumpDeletionStrategy = HeapDumpDeletionStrategy.KeepZippedHeapDumpsOnObjectsGrowing(),
330330
heapDumpDirectoryProvider = { heapDumpDirectory }
331331
)
332332

@@ -343,11 +343,11 @@ class JvmLiveObjectGrowthDetectorTest {
343343
}
344344

345345
@Test
346-
fun `ZipAndDeleteOnObjectsNotGrowing deletes all files if objects not growing`() {
346+
fun `KeepZippedHeapDumpsOnObjectsGrowing deletes all files if objects not growing`() {
347347
val heapDumpDirectory = tempFolder.newFolder()
348348
val detector = HeapDiff.repeatingJvmInProcessScenario(
349349
objectGrowthDetector = ObjectGrowthDetector.forJvmHeapNoSyntheticRefs(),
350-
heapDumpDeletionStrategy = HeapDumpDeletionStrategy.ZipAndDeleteOnObjectsNotGrowing(),
350+
heapDumpDeletionStrategy = HeapDumpDeletionStrategy.KeepZippedHeapDumpsOnObjectsGrowing(),
351351
heapDumpDirectoryProvider = { heapDumpDirectory }
352352
)
353353
val leakyScenarioRuns = 3
@@ -366,35 +366,6 @@ class JvmLiveObjectGrowthDetectorTest {
366366
assertThat(heapDumpDirectory.listFiles()).isEmpty()
367367
}
368368

369-
@Test
370-
fun `ZipAndDeleteOnObjectsNotGrowing zips heap dumps in between each scenario`() {
371-
val heapDumpDirectory = tempFolder.newFolder()
372-
val maxHeapDump = 5
373-
374-
val detector = HeapDiff.repeatingJvmInProcessScenario(
375-
objectGrowthDetector = ObjectGrowthDetector.forJvmHeapNoSyntheticRefs(),
376-
heapDumpDeletionStrategy = HeapDumpDeletionStrategy.ZipAndDeleteOnObjectsNotGrowing(),
377-
heapDumpDirectoryProvider = { heapDumpDirectory }
378-
)
379-
var i = 1
380-
detector.findRepeatedlyGrowingObjects(
381-
maxHeapDumps = maxHeapDump,
382-
scenarioLoopsPerDump = 1
383-
) {
384-
val heapDumpDirectoryFileExtensions = heapDumpDirectory.listFiles()!!.map { it.extension }
385-
assertThat(heapDumpDirectoryFileExtensions).hasSize(i - 1)
386-
if (i > 1) {
387-
assertThat(heapDumpDirectoryFileExtensions).containsOnly("zip")
388-
}
389-
leakies += Any()
390-
i++
391-
}
392-
393-
val heapDumpDirectoryFileExtensions = heapDumpDirectory.listFiles()!!.map { it.extension }
394-
assertThat(heapDumpDirectoryFileExtensions).hasSize(maxHeapDump)
395-
assertThat(heapDumpDirectoryFileExtensions).containsOnly("zip")
396-
}
397-
398369
private fun ObjectGrowthDetector.Companion.forJvmHeapNoSyntheticRefs(): ObjectGrowthDetector {
399370
val referenceMatchers = JvmObjectGrowthReferenceMatchers.defaults
400371
return ObjectGrowthDetector(

leakcanary/leakcanary-test-core/api/leakcanary-test-core.api

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ public final class leakcanary/HeapDumpDeletionStrategy$DeleteOnHeapDumpClose : l
1616
public fun onObjectGrowthDetectionComplete (Lshark/HeapDiff;)V
1717
}
1818

19-
public final class leakcanary/HeapDumpDeletionStrategy$DeleteOnObjectsNotGrowing : leakcanary/HeapDumpDeletionStrategy {
20-
public fun <init> ()V
21-
public fun <init> (Lkotlin/jvm/functions/Function1;)V
22-
public synthetic fun <init> (Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
19+
public final class leakcanary/HeapDumpDeletionStrategy$KeepHeapDumps : leakcanary/HeapDumpDeletionStrategy {
20+
public static final field INSTANCE Lleakcanary/HeapDumpDeletionStrategy$KeepHeapDumps;
2321
public fun onHeapDumpClosed (Ljava/io/File;)V
2422
public fun onObjectGrowthDetectionComplete (Lshark/HeapDiff;)V
2523
}
2624

27-
public final class leakcanary/HeapDumpDeletionStrategy$KeepHeapDumps : leakcanary/HeapDumpDeletionStrategy {
28-
public static final field INSTANCE Lleakcanary/HeapDumpDeletionStrategy$KeepHeapDumps;
25+
public final class leakcanary/HeapDumpDeletionStrategy$KeepHeapDumpsOnObjectsGrowing : leakcanary/HeapDumpDeletionStrategy {
26+
public fun <init> ()V
27+
public fun <init> (Lkotlin/jvm/functions/Function1;)V
28+
public synthetic fun <init> (Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
2929
public fun onHeapDumpClosed (Ljava/io/File;)V
3030
public fun onObjectGrowthDetectionComplete (Lshark/HeapDiff;)V
3131
}
3232

33-
public final class leakcanary/HeapDumpDeletionStrategy$ZipAndDeleteOnObjectsNotGrowing : leakcanary/HeapDumpDeletionStrategy {
33+
public final class leakcanary/HeapDumpDeletionStrategy$KeepZippedHeapDumpsOnObjectsGrowing : leakcanary/HeapDumpDeletionStrategy {
3434
public fun <init> ()V
3535
public fun <init> (Lkotlin/jvm/functions/Function1;)V
3636
public synthetic fun <init> (Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V

leakcanary/leakcanary-test-core/src/main/java/leakcanary/HeapDumpDeletionStrategy.kt

+11-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ interface HeapDumpDeletionStrategy : DumpingHeapGraphProvider.HeapDumpClosedList
3232
* objects. This is useful if you intend to open up the heap dumps directly or re run
3333
* the analysis on failure.
3434
*/
35-
class DeleteOnObjectsNotGrowing(
35+
class KeepHeapDumpsOnObjectsGrowing(
3636
private val deleteFile: (File) -> Unit = { it.delete() }
3737
) : HeapDumpDeletionStrategy {
3838
// This assumes the detector instance is always used from the same thread, which seems like a
@@ -54,31 +54,33 @@ interface HeapDumpDeletionStrategy : DumpingHeapGraphProvider.HeapDumpClosedList
5454
}
5555

5656
/**
57-
* Zips the heap dumps until we're done diffing, then delete them only if there are no growing
58-
* objects. This is useful if you intend to upload the heap dumps on failure in CI and you
57+
* Keeps the heap dumps until we're done diffing, then on completion creates a zip for each heap
58+
* dump if there are growing object, and delete all the source heap dumps.
59+
* This is useful if you intend to upload the heap dumps on failure in CI and you
5960
* want to keep disk space, network usage and cloud storage low. Zipped heap dumps are typically
6061
* 4x smaller so this is worth it, although the trade off is that zipping can add a few seconds
6162
* per heap dump to the runtime duration of a test.
6263
*/
63-
class ZipAndDeleteOnObjectsNotGrowing(
64+
class KeepZippedHeapDumpsOnObjectsGrowing(
6465
private val deleteFile: (File) -> Unit = { it.delete() }
6566
) : HeapDumpDeletionStrategy {
6667
// This assumes the detector instance is always used from the same thread, which seems like a
6768
// safe enough assumption for tests.
6869
private val closedHeapDumpFiles = mutableListOf<File>()
6970

7071
override fun onHeapDumpClosed(heapDumpFile: File) {
71-
val zippedHeapDumpFile = heapDumpFile.zipFile()
72-
deleteFile(heapDumpFile)
73-
closedHeapDumpFiles += zippedHeapDumpFile
72+
closedHeapDumpFiles += heapDumpFile
7473
}
7574

7675
override fun onObjectGrowthDetectionComplete(result: HeapDiff) {
77-
if (!result.isGrowing) {
76+
if (result.isGrowing) {
7877
closedHeapDumpFiles.forEach {
79-
deleteFile(it)
78+
it.zipFile()
8079
}
8180
}
81+
closedHeapDumpFiles.forEach {
82+
deleteFile(it)
83+
}
8284
closedHeapDumpFiles.clear()
8385
}
8486

0 commit comments

Comments
 (0)