Commit a501f8c2 by eguven Committed by Oliver Woodman

Fix flaky DownloadManagerDashTest

PiperOrigin-RevId: 244170179
parent be0acc36
...@@ -552,7 +552,7 @@ public class DownloadManagerTest { ...@@ -552,7 +552,7 @@ public class DownloadManagerTest {
} }
} }
private void runOnMainThread(final TestRunnable r) { private void runOnMainThread(TestRunnable r) {
dummyMainThread.runTestOnMainThread(r); dummyMainThread.runTestOnMainThread(r);
} }
......
...@@ -34,6 +34,7 @@ import com.google.android.exoplayer2.offline.DownloaderConstructorHelper; ...@@ -34,6 +34,7 @@ import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.offline.StreamKey; import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.scheduler.Requirements; import com.google.android.exoplayer2.scheduler.Requirements;
import com.google.android.exoplayer2.testutil.DummyMainThread; 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.FakeDataSet;
import com.google.android.exoplayer2.testutil.FakeDataSource; import com.google.android.exoplayer2.testutil.FakeDataSource;
import com.google.android.exoplayer2.testutil.RobolectricUtil; import com.google.android.exoplayer2.testutil.RobolectricUtil;
...@@ -100,8 +101,8 @@ public class DownloadManagerDashTest { ...@@ -100,8 +101,8 @@ public class DownloadManagerDashTest {
} }
@After @After
public void tearDown() throws Exception { public void tearDown() {
downloadManager.release(); runOnMainThread(() -> downloadManager.release());
Util.recursiveDelete(tempFolder); Util.recursiveDelete(tempFolder);
dummyMainThread.release(); dummyMainThread.release();
} }
...@@ -129,10 +130,11 @@ public class DownloadManagerDashTest { ...@@ -129,10 +130,11 @@ public class DownloadManagerDashTest {
// Run DM accessing code on UI/main thread as it should be. Also not to block handling of loaded // Run DM accessing code on UI/main thread as it should be. Also not to block handling of loaded
// actions. // actions.
dummyMainThread.runOnMainThread( runOnMainThread(
() -> { () -> {
// Setup an Action and immediately release the DM. // Setup an Action and immediately release the DM.
handleDownloadRequest(fakeStreamKey1, fakeStreamKey2); DownloadRequest request = getDownloadRequest(fakeStreamKey1, fakeStreamKey2);
downloadManager.addDownload(request);
downloadManager.release(); downloadManager.release();
}); });
...@@ -229,25 +231,28 @@ public class DownloadManagerDashTest { ...@@ -229,25 +231,28 @@ public class DownloadManagerDashTest {
} }
private void handleDownloadRequest(StreamKey... keys) { private void handleDownloadRequest(StreamKey... keys) {
DownloadRequest request = getDownloadRequest(keys);
runOnMainThread(() -> downloadManager.addDownload(request));
}
private DownloadRequest getDownloadRequest(StreamKey... keys) {
ArrayList<StreamKey> keysList = new ArrayList<>(); ArrayList<StreamKey> keysList = new ArrayList<>();
Collections.addAll(keysList, keys); Collections.addAll(keysList, keys);
DownloadRequest action = return new DownloadRequest(
new DownloadRequest( TEST_ID,
TEST_ID, DownloadRequest.TYPE_DASH,
DownloadRequest.TYPE_DASH, TEST_MPD_URI,
TEST_MPD_URI, keysList,
keysList, /* customCacheKey= */ null,
/* customCacheKey= */ null, null);
null);
downloadManager.addDownload(action);
} }
private void handleRemoveAction() { private void handleRemoveAction() {
downloadManager.removeDownload(TEST_ID); runOnMainThread(() -> downloadManager.removeDownload(TEST_ID));
} }
private void createDownloadManager() { private void createDownloadManager() {
dummyMainThread.runTestOnMainThread( runOnMainThread(
() -> { () -> {
Factory fakeDataSourceFactory = new FakeDataSource.Factory().setFakeDataSet(fakeDataSet); Factory fakeDataSourceFactory = new FakeDataSource.Factory().setFakeDataSet(fakeDataSet);
downloadManager = downloadManager =
...@@ -261,9 +266,13 @@ public class DownloadManagerDashTest { ...@@ -261,9 +266,13 @@ public class DownloadManagerDashTest {
new Requirements(0)); new Requirements(0));
downloadManagerListener = downloadManagerListener =
new TestDownloadManagerListener(downloadManager, dummyMainThread); new TestDownloadManagerListener(
downloadManager, dummyMainThread, /* timeout= */ 3000);
downloadManager.startDownloads(); downloadManager.startDownloads();
}); });
} }
private void runOnMainThread(TestRunnable r) {
dummyMainThread.runTestOnMainThread(r);
}
} }
...@@ -40,14 +40,21 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -40,14 +40,21 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
private final DummyMainThread dummyMainThread; private final DummyMainThread dummyMainThread;
private final HashMap<String, ArrayBlockingQueue<Integer>> downloadStates; private final HashMap<String, ArrayBlockingQueue<Integer>> downloadStates;
private final ConditionVariable initializedCondition; private final ConditionVariable initializedCondition;
private final int timeout;
private CountDownLatch downloadFinishedCondition; private CountDownLatch downloadFinishedCondition;
@Download.FailureReason private int failureReason; @Download.FailureReason private int failureReason;
public TestDownloadManagerListener( public TestDownloadManagerListener(
DownloadManager downloadManager, DummyMainThread dummyMainThread) { DownloadManager downloadManager, DummyMainThread dummyMainThread) {
this(downloadManager, dummyMainThread, TIMEOUT);
}
public TestDownloadManagerListener(
DownloadManager downloadManager, DummyMainThread dummyMainThread, int timeout) {
this.downloadManager = downloadManager; this.downloadManager = downloadManager;
this.dummyMainThread = dummyMainThread; this.dummyMainThread = dummyMainThread;
this.timeout = timeout;
downloadStates = new HashMap<>(); downloadStates = new HashMap<>();
initializedCondition = new ConditionVariable(); initializedCondition = new ConditionVariable();
downloadManager.addListener(this); downloadManager.addListener(this);
...@@ -110,7 +117,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -110,7 +117,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
downloadFinishedCondition.countDown(); downloadFinishedCondition.countDown();
} }
}); });
assertThat(downloadFinishedCondition.await(TIMEOUT, TimeUnit.MILLISECONDS)).isTrue(); assertThat(downloadFinishedCondition.await(timeout, TimeUnit.MILLISECONDS)).isTrue();
} }
private ArrayBlockingQueue<Integer> getStateQueue(String taskId) { private ArrayBlockingQueue<Integer> getStateQueue(String taskId) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment