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
This commit is contained in:
Daichi Hirono
2016-04-12 11:29:07 +09:00
parent 76be46f4d9
commit 3edcde2953
2 changed files with 15 additions and 9 deletions

View File

@@ -196,9 +196,10 @@ class DocumentLoader implements AutoCloseable {
}
task.loadObjectInfoList(NUM_LOADING_ENTRIES);
final boolean shouldNotify =
task.mLastNotified.getTime() <
new Date().getTime() - NOTIFY_PERIOD_MS ||
task.getState() != LoaderTask.STATE_LOADING;
task.getState() != LoaderTask.STATE_CANCELLED &&
(task.mLastNotified.getTime() <
new Date().getTime() - NOTIFY_PERIOD_MS ||
task.getState() != LoaderTask.STATE_LOADING);
if (shouldNotify) {
task.notify(mResolver);
}

View File

@@ -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() {