From 05bf4f398f678867b83273ff14f4d82dda9035e5 Mon Sep 17 00:00:00 2001
From: Motomu Utsumi <motomuman@google.com>
Date: Thu, 25 Jan 2024 21:51:07 +0900
Subject: [PATCH] clearInlineMocks from DevSdkIgnoreRunner after running test

Memory leak was observed and inline mock is one of the reasons.
This CL updates DevSdkIgnoreRunner to clear mock state if there is no
thread leak.

Test: TH
Bug: 321603955
Change-Id: I50fd03f14c6703bd38b5729f577d7ea516cd5b44
---
 .../devicetests/com/android/testutils/DevSdkIgnoreRunner.kt   | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/staticlibs/testutils/devicetests/com/android/testutils/DevSdkIgnoreRunner.kt b/staticlibs/testutils/devicetests/com/android/testutils/DevSdkIgnoreRunner.kt
index 10accd4900..69fdbf8db0 100644
--- a/staticlibs/testutils/devicetests/com/android/testutils/DevSdkIgnoreRunner.kt
+++ b/staticlibs/testutils/devicetests/com/android/testutils/DevSdkIgnoreRunner.kt
@@ -31,6 +31,7 @@ import org.junit.runner.manipulation.Sorter
 import org.junit.runner.notification.Failure
 import org.junit.runner.notification.RunNotifier
 import org.junit.runners.Parameterized
+import org.mockito.Mockito
 
 /**
  * A runner that can skip tests based on the development SDK as defined in [DevSdkIgnoreRule].
@@ -124,6 +125,9 @@ class DevSdkIgnoreRunner(private val klass: Class<*>) : Runner(), Filterable, So
             notifier.fireTestFailure(Failure(leakMonitorDesc,
                     IllegalStateException("Unexpected thread changes: $threadsDiff")))
         }
+        // Clears up internal state of all inline mocks.
+        // TODO: Call clearInlineMocks() at the end of each test.
+        Mockito.framework().clearInlineMocks()
         notifier.fireTestFinished(leakMonitorDesc)
     }
 
-- 
GitLab