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

Commit b96969da authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Improve threading model for test stability.

There were cases where some async getSharedPreferences() calls would
race with test tear-down, so this ensures we've processed all async
events before executing a test case.

Bug: 190181745
Test: atest BluetoothInstrumentationTests --rerun-until-failure 100
Change-Id: I746afc33e925659cd15ba810337d23d7b359a310
parent 46a5630e
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -34,8 +34,10 @@ import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.content.res.Resources;
import android.media.AudioManager;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import android.os.Process;
@@ -137,6 +139,12 @@ public class AdapterServiceTest {
        }
        Assert.assertNotNull(Looper.myLooper());

        // Dispatch all async work through instrumentation so we can wait until
        // it's drained below
        AsyncTask.setDefaultExecutor((r) -> {
            InstrumentationRegistry.getInstrumentation().runOnMainSync(r);
        });

        InstrumentationRegistry.getInstrumentation().runOnMainSync(
                () -> mAdapterService = new AdapterService());
        mServiceBinder = new AdapterService.AdapterServiceBinder(mAdapterService);
@@ -192,8 +200,11 @@ public class AdapterServiceTest {

        // Attach a context to the service for permission checks.
        mAdapterService.attach(mMockContext, null, null, null, mApplication, null);

        mAdapterService.onCreate();

        // Wait for any async events to drain
        InstrumentationRegistry.getInstrumentation().waitForIdleSync();

        doReturn(Context.PERMISSION_CHECKER_SERVICE).when(mMockContext)
                .getSystemServiceName(PermissionCheckerManager.class);
        when(mMockContext.getSystemService(Context.PERMISSION_CHECKER_SERVICE))