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

Commit 4e1de31d authored by Hawkwood Glazier's avatar Hawkwood Glazier Committed by Android (Google) Code Review
Browse files

Merge "Speed up PluginInstanceTest by skipping unnessecary gc runs" into main

parents 24f37066 0413179d
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();
        }
    }