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

Commit ec1b1bb7 authored by Sergey Nikolaienkov's avatar Sergey Nikolaienkov
Browse files

Fix data race in RecentsLoaderTests

Fix data race in RecentsLoaderTests#testContentsUpdate_observable() that would occur when accessing `private boolean mContentChanged` variable from the Main (a.k.a. UI) thread and the thread the test runs on.
Instead of relying on a class member mContentChanged use the existing thread-safe CountDownLatch object.

Bug: 288342044
Test: atest --rerun-until-failure 10000 \
      DocumentsUIUnitTests:com.android.documentsui.RecentsLoaderTests
Change-Id: I336ecddf4c894d1ba383a4246e7eb4a58ac200e2
Merged-In: I336ecddf4c894d1ba383a4246e7eb4a58ac200e2
(cherry picked from commit f243c48d)
parent a2c5763f
Loading
Loading
Loading
Loading
+10 −10
Original line number Diff line number Diff line
@@ -56,7 +56,6 @@ public class RecentsLoaderTests {
    private TestEnv mEnv;
    private TestActivity mActivity;
    private RecentsLoader mLoader;
    private boolean mContentChanged;

    @Before
    public void setUp() {
@@ -148,12 +147,13 @@ public class RecentsLoaderTests {

    @Test
    public void testContentsUpdate_observable() throws Exception {
        CountDownLatch latch = new CountDownLatch(1);
        Runnable callback = () -> {
            latch.countDown();
            mContentChanged = true;
        };
        mLoader.setObserver(new LockingContentObserver(new ContentLock(), callback));
        final CountDownLatch latch = new CountDownLatch(1);

        // Please be mindful of the fact that the callback will be invoked on the Main (aka UI)
        // thread, while the test itself is running on another (dedicated) thread.
        final Runnable onContentChangedCallback = latch::countDown;
        mLoader.setObserver(new LockingContentObserver(
                new ContentLock(), onContentChangedCallback));

        final DocumentInfo doc = mEnv.model.createFile("freddy.jpg");
        doc.lastModified = System.currentTimeMillis();
@@ -162,12 +162,12 @@ public class RecentsLoaderTests {

        mLoader.loadInBackground();

        TestCursor c = (TestCursor) mEnv.mockProviders.get(TestProvidersAccess.HOME.authority)
        final TestCursor c = (TestCursor) mEnv.mockProviders.get(TestProvidersAccess.HOME.authority)
                .queryRecentDocuments(null, null);
        c.mockOnChange();

        latch.await(1, TimeUnit.SECONDS);
        assertTrue(mContentChanged);
        final boolean onContentChangedCallbackInvoked = latch.await(1, TimeUnit.SECONDS);
        assertTrue(onContentChangedCallbackInvoked);
    }

    @Test