-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* binder_leak: PR nits for #351 Add special case to ignore Binder leaks
- Loading branch information
Showing
8 changed files
with
208 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
leakcanary-analyzer/src/test/java/com/squareup/leakcanary/ServiceBinderLeakTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (C) 2016 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.squareup.leakcanary; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.lang.ref.WeakReference; | ||
|
||
import static com.squareup.leakcanary.LeakTraceElement.Holder.CLASS; | ||
import static com.squareup.leakcanary.LeakTraceElement.Holder.OBJECT; | ||
import static com.squareup.leakcanary.LeakTraceElement.Type.INSTANCE_FIELD; | ||
import static com.squareup.leakcanary.LeakTraceElement.Type.STATIC_FIELD; | ||
import static com.squareup.leakcanary.TestUtil.analyze; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* leak_service_binder_ignored.hprof contains a "normal" leak when binding to a service, where | ||
* leak_service_binder.hprof contains a leak where a binder is leaked by a static field. | ||
*/ | ||
public class ServiceBinderLeakTest { | ||
|
||
ExcludedRefs.Builder excludedRefs; | ||
|
||
@Before public void setUp() { | ||
excludedRefs = new ExcludedRefs.Builder() | ||
.clazz(WeakReference.class.getName(), true) | ||
.clazz("java.lang.ref.FinalizerReference", true); | ||
} | ||
|
||
@Test public void realBinderLeak() { | ||
excludedRefs.rootSuperClass("android.os.Binder", true); | ||
|
||
AnalysisResult result = analyze( | ||
TestUtil.fileFromName("leak_service_binder.hprof"), | ||
"b3abfae6-2c53-42e1-b8c1-96b0558dbeae", | ||
excludedRefs | ||
); | ||
|
||
assertTrue(result.leakFound); | ||
assertFalse(result.excludedLeak); | ||
LeakTraceElement gcRoot = result.leakTrace.elements.get(0); | ||
assertEquals(STATIC_FIELD, gcRoot.type); | ||
assertEquals("com.example.leakcanary.LeakyService", gcRoot.className); | ||
assertEquals(CLASS, gcRoot.holder); | ||
} | ||
|
||
@Test public void ignorableBinderLeak() { | ||
excludedRefs.rootSuperClass("android.os.Binder", false); | ||
|
||
AnalysisResult result = analyze( | ||
TestUtil.fileFromName("leak_service_binder_ignored.hprof"), | ||
"6e524414-9581-4ce7-8690-e8ddf8b82454", | ||
excludedRefs | ||
); | ||
|
||
assertTrue(result.leakFound); | ||
assertTrue(result.excludedLeak); | ||
LeakTraceElement gcRoot = result.leakTrace.elements.get(0); | ||
assertEquals(INSTANCE_FIELD, gcRoot.type); | ||
assertEquals("com.example.leakcanary.LeakyService$MyBinder", gcRoot.className); | ||
assertEquals(OBJECT, gcRoot.holder); | ||
} | ||
|
||
@Test public void alwaysIgnorableBinderLeak() { | ||
excludedRefs.rootSuperClass("android.os.Binder", true); | ||
|
||
AnalysisResult result = analyze( | ||
TestUtil.fileFromName("leak_service_binder_ignored.hprof"), | ||
"6e524414-9581-4ce7-8690-e8ddf8b82454", | ||
excludedRefs | ||
); | ||
|
||
assertFalse(result.leakFound); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
leakcanary-analyzer/src/test/java/com/squareup/leakcanary/TestUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2016 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.squareup.leakcanary; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
|
||
final class TestUtil { | ||
static File fileFromName(String filename) { | ||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | ||
URL url = classLoader.getResource(filename); | ||
return new File(url.getPath()); | ||
} | ||
|
||
static AnalysisResult analyze(File heapDumpFile, String referenceKey, ExcludedRefs.Builder excludedRefs) { | ||
HeapAnalyzer heapAnalyzer = new HeapAnalyzer(excludedRefs.build()); | ||
AnalysisResult result = heapAnalyzer.checkForLeak(heapDumpFile, referenceKey); | ||
if (result.failure != null) { | ||
result.failure.printStackTrace(); | ||
} | ||
if (result.leakTrace != null) { | ||
System.out.println(result.leakTrace); | ||
} | ||
return result; | ||
} | ||
|
||
private TestUtil() { | ||
throw new AssertionError(); | ||
} | ||
} |
Binary file not shown.
Binary file added
BIN
+3.52 MB
leakcanary-analyzer/src/test/resources/leak_service_binder_ignored.hprof
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters