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

Commit 92b9365f authored by Garfield Tan's avatar Garfield Tan Committed by Wale Ogunwale
Browse files

Reset some threads after test finishes.

Occassionally production code schedule delayed messages to other threads
that hold references to mocks of finished tests. Note we never recreate
a new thread for new tests, or clear messages of these queues, so it's
possible it still uses mocks after they're cleared and cause crashes.

Bug: 122846381
Test: tradefed.sh run commandAndExit WmTests --include-annotation
android.platform.test.annotations.Presubmit --exclude-annotation
androidx.test.filters.FlakyTest
Change-Id: I3ba8f94689ddf633f2d18c74917fa3891c689351
parent e3d37b50
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import static android.os.Process.THREAD_PRIORITY_DISPLAY;
import android.os.Handler;
import android.os.Trace;

import com.android.internal.annotations.VisibleForTesting;

/**
 * Thread for handling all legacy window animations, or anything that's directly impacting
 * animations like starting windows or traversals.
@@ -55,4 +57,20 @@ public final class AnimationThread extends ServiceThread {
            return sHandler;
        }
    }

    /**
     * Disposes current animation thread if it's initialized. Should only be used in tests to set up
     * a new environment.
     */
    @VisibleForTesting
    public static void dispose() {
        synchronized (DisplayThread.class) {
            if (sInstance == null) {
                return;
            }

            getHandler().runWithScissors(() -> sInstance.quit(), 0 /* timeout */);
            sInstance = null;
        }
    }
}
+18 −0
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ import android.os.Handler;
import android.os.Process;
import android.os.Trace;

import com.android.internal.annotations.VisibleForTesting;

/**
 * Shared singleton foreground thread for the system.  This is a thread for
 * operations that affect what's on the display, which needs to have a minimum
@@ -58,4 +60,20 @@ public final class DisplayThread extends ServiceThread {
            return sHandler;
        }
    }

    /**
     * Disposes current display thread if it's initialized. Should only be used in tests to set up a
     * new environment.
     */
    @VisibleForTesting
    public static void dispose() {
        synchronized (DisplayThread.class) {
            if (sInstance == null) {
                return;
            }

            getHandler().runWithScissors(() -> sInstance.quit(), 0 /* timeout */);
            sInstance = null;
        }
    }
}
+9 −0
Original line number Diff line number Diff line
@@ -63,6 +63,8 @@ import android.view.Surface;
import android.view.SurfaceControl;

import com.android.dx.mockito.inline.extended.StaticMockitoSession;
import com.android.server.AnimationThread;
import com.android.server.DisplayThread;
import com.android.server.LocalServices;
import com.android.server.LockGuard;
import com.android.server.ServiceThread;
@@ -300,6 +302,13 @@ public class SystemServicesTestRule implements TestRule {
        tearDownLocalServices();
        tearDownSystemCore();

        // Needs to explicitly dispose current static threads because there could be messages
        // scheduled at a later time, and all mocks are invalid when it's executed.
        DisplayThread.dispose();
        AnimationThread.dispose();
        // Reset priority booster because animation thread has been changed.
        WindowManagerService.sThreadPriorityBooster = new WindowManagerThreadPriorityBooster();

        Mockito.framework().clearInlineMocks();
    }