Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0413179d authored by Hawkwood Glazier's avatar Hawkwood Glazier
Browse files

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
parent c7498aed
Loading
Loading
Loading
Loading
+14 −13
Original line number Diff line number Diff line
@@ -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();
        }
    }