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

Commit 3edcde29 authored by Daichi Hirono's avatar Daichi Hirono
Browse files

Fix flakiness DocumentLoader#testCancelTask.

For testing, we use blockDocument and unblockDocument test mehtods to
control flow of DocumentLoader's background thread.
Previously testCancelTask may exit before the background thread goes
back from blocking, which causes InterruptedException.

Fixes: 28125289
Change-Id: Id03826733c5b6f1da66b9280838eb1d2897ed5fc
parent 76be46f4
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -196,9 +196,10 @@ class DocumentLoader implements AutoCloseable {
                }
                task.loadObjectInfoList(NUM_LOADING_ENTRIES);
                final boolean shouldNotify =
                        task.mLastNotified.getTime() <
                        task.getState() != LoaderTask.STATE_CANCELLED &&
                        (task.mLastNotified.getTime() <
                         new Date().getTime() - NOTIFY_PERIOD_MS ||
                        task.getState() != LoaderTask.STATE_LOADING;
                         task.getState() != LoaderTask.STATE_LOADING);
                if (shouldNotify) {
                    task.notify(mResolver);
                }
+11 −6
Original line number Diff line number Diff line
@@ -143,9 +143,9 @@ public class DocumentLoaderTest extends AndroidTestCase {
        }
    }

    public void testCancelTask() throws IOException, InterruptedException {
    public void testCancelTask() throws IOException, InterruptedException, TimeoutException {
        setUpDocument(mManager,
                DocumentLoader.NUM_INITIAL_ENTRIES + DocumentLoader.NUM_LOADING_ENTRIES + 1);
                DocumentLoader.NUM_INITIAL_ENTRIES + 1);

        // Block the first iteration in the background thread.
        mManager.blockDocument(
@@ -155,19 +155,24 @@ public class DocumentLoaderTest extends AndroidTestCase {
                MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION, mParentIdentifier)) {
            assertTrue(cursor.getExtras().getBoolean(DocumentsContract.EXTRA_LOADING));
        }
        Thread.sleep(DocumentLoader.NOTIFY_PERIOD_MS);

        final Uri uri = DocumentsContract.buildChildDocumentsUri(
                MtpDocumentsProvider.AUTHORITY, mParentIdentifier.mDocumentId);
        assertEquals(0, mResolver.getChangeCount(uri));

        // Clear task while the first iteration is being blocked.
        mLoader.cancelTask(mParentIdentifier);
        mManager.unblockDocument(
                0, DocumentLoader.NUM_INITIAL_ENTRIES + 1);
        mLoader.cancelTask(mParentIdentifier);

        Thread.sleep(DocumentLoader.NOTIFY_PERIOD_MS * 2);
        Thread.sleep(DocumentLoader.NOTIFY_PERIOD_MS);
        assertEquals(0, mResolver.getChangeCount(uri));

        // Check if it's OK to query invalidated task.
        try (final Cursor cursor = mLoader.queryChildDocuments(
                MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION, mParentIdentifier)) {
            assertTrue(cursor.getExtras().getBoolean(DocumentsContract.EXTRA_LOADING));
        }
        mResolver.waitForNotification(uri, 1);
    }

    private void setUpLoader() {