Skip to content

Commit

Permalink
Fix flaky DownloadManagerDashTest
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 244170179
  • Loading branch information
erdemguven authored and andrewlewis committed Apr 18, 2019
1 parent f6de1aa commit 50c9fe6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ private void releaseDownloadManager() throws Exception {
}
}

private void runOnMainThread(final TestRunnable r) {
private void runOnMainThread(TestRunnable r) {
dummyMainThread.runTestOnMainThread(r);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.scheduler.Requirements;
import com.google.android.exoplayer2.testutil.DummyMainThread;
import com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable;
import com.google.android.exoplayer2.testutil.FakeDataSet;
import com.google.android.exoplayer2.testutil.FakeDataSource;
import com.google.android.exoplayer2.testutil.TestDownloadManagerListener;
Expand Down Expand Up @@ -99,8 +100,8 @@ public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
downloadManager.release();
public void tearDown() {
runOnMainThread(() -> downloadManager.release());
Util.recursiveDelete(tempFolder);
dummyMainThread.release();
}
Expand Down Expand Up @@ -128,10 +129,11 @@ public void testSaveAndLoadActionFile() throws Throwable {

// Run DM accessing code on UI/main thread as it should be. Also not to block handling of loaded
// actions.
dummyMainThread.runOnMainThread(
runOnMainThread(
() -> {
// Setup an Action and immediately release the DM.
handleDownloadRequest(fakeStreamKey1, fakeStreamKey2);
DownloadRequest request = getDownloadRequest(fakeStreamKey1, fakeStreamKey2);
downloadManager.addDownload(request);
downloadManager.release();
});

Expand Down Expand Up @@ -228,25 +230,28 @@ private void blockUntilTasksCompleteAndThrowAnyDownloadError() throws Throwable
}

private void handleDownloadRequest(StreamKey... keys) {
DownloadRequest request = getDownloadRequest(keys);
runOnMainThread(() -> downloadManager.addDownload(request));
}

private DownloadRequest getDownloadRequest(StreamKey... keys) {
ArrayList<StreamKey> keysList = new ArrayList<>();
Collections.addAll(keysList, keys);
DownloadRequest action =
new DownloadRequest(
TEST_ID,
DownloadRequest.TYPE_DASH,
TEST_MPD_URI,
keysList,
/* customCacheKey= */ null,
null);
downloadManager.addDownload(action);
return new DownloadRequest(
TEST_ID,
DownloadRequest.TYPE_DASH,
TEST_MPD_URI,
keysList,
/* customCacheKey= */ null,
null);
}

private void handleRemoveAction() {
downloadManager.removeDownload(TEST_ID);
runOnMainThread(() -> downloadManager.removeDownload(TEST_ID));
}

private void createDownloadManager() {
dummyMainThread.runTestOnMainThread(
runOnMainThread(
() -> {
Factory fakeDataSourceFactory = new FakeDataSource.Factory().setFakeDataSet(fakeDataSet);
downloadManager =
Expand All @@ -260,9 +265,13 @@ private void createDownloadManager() {
new Requirements(0));

downloadManagerListener =
new TestDownloadManagerListener(downloadManager, dummyMainThread);
new TestDownloadManagerListener(
downloadManager, dummyMainThread, /* timeout= */ 3000);
downloadManager.startDownloads();
});
}

private void runOnMainThread(TestRunnable r) {
dummyMainThread.runTestOnMainThread(r);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,21 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
private final DummyMainThread dummyMainThread;
private final HashMap<String, ArrayBlockingQueue<Integer>> downloadStates;
private final ConditionVariable initializedCondition;
private final int timeout;

private CountDownLatch downloadFinishedCondition;
@Download.FailureReason private int failureReason;

public TestDownloadManagerListener(
DownloadManager downloadManager, DummyMainThread dummyMainThread) {
this(downloadManager, dummyMainThread, TIMEOUT);
}

public TestDownloadManagerListener(
DownloadManager downloadManager, DummyMainThread dummyMainThread, int timeout) {
this.downloadManager = downloadManager;
this.dummyMainThread = dummyMainThread;
this.timeout = timeout;
downloadStates = new HashMap<>();
initializedCondition = new ConditionVariable();
downloadManager.addListener(this);
Expand Down Expand Up @@ -110,7 +117,7 @@ public void blockUntilTasksComplete() throws InterruptedException {
downloadFinishedCondition.countDown();
}
});
assertThat(downloadFinishedCondition.await(TIMEOUT, TimeUnit.MILLISECONDS)).isTrue();
assertThat(downloadFinishedCondition.await(timeout, TimeUnit.MILLISECONDS)).isTrue();
}

private ArrayBlockingQueue<Integer> getStateQueue(String taskId) {
Expand Down

0 comments on commit 50c9fe6

Please sign in to comment.