Ensure no thread leak after NetworkStatsServiceTest

This change includes:
 1. A mechanism in DevSdkIngoreRunnrer to dump thread counts
    before/after tests, and compare thread counts to detect leaks.
 2. Add an annotation @MonitorThreadLeak for test classes to
    annotate the classes which should monitor thread leaks.
 3. Annotated NetworkStatsServiceTest to apply the enforcement.

Sample output:
[1/2] android.net.connectivity.com.android.server.net.NetworkStatsServiceTest#testDumpStatsMap: PASSED (1.187s)
[2/2] android.net.connectivity.com.android.server.net.NetworkStatsServiceTest#ThreadLeakMonitor: FAILED (7ms)

STACKTRACE:
java.lang.IllegalStateException: Expected threads: {binder:26055_3=1, Instr: androidx.test.runner.AndroidJUnitRunner=1, main=1, InstrumentationConnectionThread=1, binder:26055_2=1, binder:26055_1=1} but got: {binder:26055_3=1, NetworkStatsObservers=1, Instr: androidx.test.runner.AndroidJUnitRunner=1, main=1, InstrumentationConnectionThread=1, binder:26055_2=1, binder:26055_1=1}

Test: atest ConnectivityCoverageTests:android.net.connectivity.com.android.server.net.NetworkStatsServiceTest \
      --rerun-until-failure 100
Bug: 308544001
Bug: 307693729
Change-Id: Ia0bccb82c5985df608b8402009b32626b6b17c5a
This commit is contained in:
Junyu Lai
2023-11-06 16:21:16 +08:00
parent 8b61783b78
commit 17f589cf47
2 changed files with 52 additions and 8 deletions

View File

@@ -193,6 +193,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
* TODO: This test used to be really brittle because it used Easymock - it uses Mockito now, but
* still uses the Easymock structure, which could be simplified.
*/
@DevSdkIgnoreRunner.MonitorThreadLeak
@RunWith(DevSdkIgnoreRunner.class)
@SmallTest
// NetworkStatsService is not updatable before T, so tests do not need to be backwards compatible