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

Commit f6d028d5 authored by David Saff's avatar David Saff
Browse files

Allow waiting for temp folder to become available

Bug: 404544974
Test: Local run, presubmit
Flag: TEST_ONLY
Change-Id: I41b4857088c19f0a42311f8d7a18ff62e71e1a8c
parent 1f1c7fe0
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() {