From 0413179d77870950b548844baba357ed9b61c545 Mon Sep 17 00:00:00 2001 From: Hawkwood Glazier Date: Tue, 19 Sep 2023 19:08:00 +0000 Subject: Speed up PluginInstanceTest by skipping unnessecary gc runs Bail from the GC loop early if the allocated instances falls to match or be below the expected number of allocations. This should speed up most test runs when the test is passing. Bug: 299965591 Test: atest PluginInstanceTest Change-Id: Ic01305ad93f133001eee83ec7134523e650dc7e8 --- .../shared/plugins/PluginInstanceTest.java | 27 +++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/SystemUI/tests/src/com/android/systemui/shared/plugins/PluginInstanceTest.java b/packages/SystemUI/tests/src/com/android/systemui/shared/plugins/PluginInstanceTest.java index 88d853e244e0..8f06e636b479 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/shared/plugins/PluginInstanceTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/shared/plugins/PluginInstanceTest.java @@ -181,29 +181,30 @@ public class PluginInstanceTest extends SysuiTestCase { String ACTION = "testAction"; } - public void assertInstances(Integer allocated, Integer created) { - // Run the garbage collector to finalize and deallocate outstanding - // instances. Since the GC doesn't always appear to want to run - // completely when we ask, we ask it 10 times in a short loop. - for (int i = 0; i < 10; i++) { + private void assertInstances(int allocated, int created) { + // If there are more than the expected number of allocated instances, then we run the + // garbage collector to finalize and deallocate any outstanding non-referenced instances. + // Since the GC doesn't always appear to want to run completely when we ask, we do this up + // to 10 times before failing the test. + for (int i = 0; mCounter.getAllocatedInstances() > allocated && i < 10; i++) { System.runFinalization(); System.gc(); } - mCounter.assertInstances(allocated, created); + assertEquals(allocated, mCounter.getAllocatedInstances()); + assertEquals(created, mCounter.getCreatedInstances()); } public static class RefCounter { public final AtomicInteger mAllocatedInstances = new AtomicInteger(); public final AtomicInteger mCreatedInstances = new AtomicInteger(); - public void assertInstances(Integer allocated, Integer created) { - if (allocated != null) { - assertEquals(allocated.intValue(), mAllocatedInstances.get()); - } - if (created != null) { - assertEquals(created.intValue(), mCreatedInstances.get()); - } + public int getAllocatedInstances() { + return mAllocatedInstances.get(); + } + + public int getCreatedInstances() { + return mCreatedInstances.get(); } } -- cgit v1.2.3-59-g8ed1b