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

Commit 4b2e603c authored by David Saff's avatar David Saff Committed by Android (Google) Code Review
Browse files

Merge "Allow waiting for temp folder to become available" into main

parents be8251ab f6d028d5
Loading
Loading
Loading
Loading
+32 −11
Original line number Diff line number Diff line
@@ -192,9 +192,12 @@ public abstract class SysuiTestCase {
    private Instrumentation mRealInstrumentation;
    private SysuiTestDependency mSysuiDependency;

    static {
        assertTempFilesAreCreatable();
    }

    @Before
    public void SysuiSetup() throws Exception {
        assertTempFilesAreCreatable();
        ProtoLog.REQUIRE_PROTOLOGTOOL = false;
        mSysuiDependency = new SysuiTestDependency(mContext, shouldFailOnLeakedReceiver());
        mDependency = mSysuiDependency.install();
@@ -217,25 +220,43 @@ public abstract class SysuiTestCase {
    }

    private static Boolean sCanCreateTempFiles = null;

    private static void assertTempFilesAreCreatable() {
    private static void assertTempFilesAreCreatable() throws RuntimeException {
        // TODO(b/391948934): hopefully remove this hack
        if (sCanCreateTempFiles == null) {
            attemptToCreateTempFile();
        }
        if (!sCanCreateTempFiles) {
            Assert.fail(
                    "Cannot create temp files, so mockito will probably fail (b/391948934).  Temp"
                            + " folder should be: "
                            + System.getProperty("java.io.tmpdir"));
        }
    }

    private static void attemptToCreateTempFile() throws RuntimeException {
        // If I understand https://buganizer.corp.google.com/issues/123230176#comment11 correctly,
        // we may have to wait for the temp folder to become available.
        int retriesRemaining = 20;
        IOException latestFailure = null;
        while (sCanCreateTempFiles == null && retriesRemaining > 0) {
            retriesRemaining--;
            try {
                File tempFile = File.createTempFile("confirm_temp_file_createable", "txt");
                sCanCreateTempFiles = true;
                assertTrue(tempFile.delete());
                return;
            } catch (IOException e) {
                sCanCreateTempFiles = false;
                throw new RuntimeException(e);
                latestFailure = e;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                    // just keep waiting
                }
            }
        if (!sCanCreateTempFiles) {
            Assert.fail(
                    "Cannot create temp files, so mockito will probably fail (b/391948934).  Temp"
                            + " folder should be: "
                            + System.getProperty("java.io.tmpdir"));
        }

        sCanCreateTempFiles = false;
        throw new RuntimeException(latestFailure);
    }

    protected boolean shouldFailOnLeakedReceiver() {