Commit 37d9e2f4 by olly Committed by Oliver Woodman

DownloadManagerTest: Improve thread interactions

- Remove assertReleased and replace it with a proper condition variable
  that's opened when Downloader.download or Download.remove finish. As
  far as I can tell assertReleased was basically implementing "sleep for
  10 seconds after the Downloader starts". Note fixing this properly
  also makes the tests run much faster!
- Use ConditionVariable instead of CountDownLatch(1).
- Use AtomicInteger instead of volatile int because it's clearer and
  allows removal of explanatory comments.

PiperOrigin-RevId: 308819204
parent 5c216977
...@@ -264,8 +264,7 @@ public class DownloadManagerDashTest { ...@@ -264,8 +264,7 @@ public class DownloadManagerDashTest {
downloadManager.setRequirements(new Requirements(0)); downloadManager.setRequirements(new Requirements(0));
downloadManagerListener = downloadManagerListener =
new TestDownloadManagerListener( new TestDownloadManagerListener(downloadManager, dummyMainThread);
downloadManager, dummyMainThread, /* timeoutMs= */ 3000);
downloadManager.resumeDownloads(); downloadManager.resumeDownloads();
}); });
} }
......
...@@ -22,41 +22,32 @@ import androidx.annotation.Nullable; ...@@ -22,41 +22,32 @@ import androidx.annotation.Nullable;
import com.google.android.exoplayer2.offline.Download; import com.google.android.exoplayer2.offline.Download;
import com.google.android.exoplayer2.offline.Download.State; import com.google.android.exoplayer2.offline.Download.State;
import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.ConditionVariable;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/** A {@link DownloadManager.Listener} for testing. */ /** A {@link DownloadManager.Listener} for testing. */
public final class TestDownloadManagerListener implements DownloadManager.Listener { public final class TestDownloadManagerListener implements DownloadManager.Listener {
private static final int TIMEOUT_MS = 1000; private static final int TIMEOUT_MS = 10_000;
private static final int INITIALIZATION_TIMEOUT_MS = 10_000;
private static final int STATE_REMOVED = -1; private static final int STATE_REMOVED = -1;
private final DownloadManager downloadManager; private final DownloadManager downloadManager;
private final DummyMainThread dummyMainThread; private final DummyMainThread dummyMainThread;
private final HashMap<String, ArrayBlockingQueue<Integer>> downloadStates; private final HashMap<String, ArrayBlockingQueue<Integer>> downloadStates;
private final CountDownLatch initializedCondition; private final ConditionVariable initializedCondition;
private final int timeoutMs; private final ConditionVariable idleCondition;
private @MonotonicNonNull 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_MS);
}
public TestDownloadManagerListener(
DownloadManager downloadManager, DummyMainThread dummyMainThread, int timeoutMs) {
this.downloadManager = downloadManager; this.downloadManager = downloadManager;
this.dummyMainThread = dummyMainThread; this.dummyMainThread = dummyMainThread;
this.timeoutMs = timeoutMs;
downloadStates = new HashMap<>(); downloadStates = new HashMap<>();
initializedCondition = new CountDownLatch(1); initializedCondition = TestUtil.createRobolectricConditionVariable();
idleCondition = TestUtil.createRobolectricConditionVariable();
downloadManager.addListener(this); downloadManager.addListener(this);
} }
...@@ -67,13 +58,12 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -67,13 +58,12 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
@Override @Override
public void onInitialized(DownloadManager downloadManager) { public void onInitialized(DownloadManager downloadManager) {
initializedCondition.countDown(); initializedCondition.open();
} }
public void waitUntilInitialized() throws InterruptedException { public void blockUntilInitialized() throws InterruptedException {
if (!downloadManager.isInitialized()) { if (!downloadManager.isInitialized()) {
assertThat(initializedCondition.await(INITIALIZATION_TIMEOUT_MS, TimeUnit.MILLISECONDS)) assertThat(initializedCondition.block(TIMEOUT_MS)).isTrue();
.isTrue();
} }
} }
...@@ -92,9 +82,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -92,9 +82,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
@Override @Override
public synchronized void onIdle(DownloadManager downloadManager) { public synchronized void onIdle(DownloadManager downloadManager) {
if (downloadFinishedCondition != null) { idleCondition.open();
downloadFinishedCondition.countDown();
}
} }
/** /**
...@@ -110,16 +98,14 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -110,16 +98,14 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
/** Blocks until all remove and download tasks are complete. Task errors are ignored. */ /** Blocks until all remove and download tasks are complete. Task errors are ignored. */
public void blockUntilTasksComplete() throws InterruptedException { public void blockUntilTasksComplete() throws InterruptedException {
synchronized (this) { idleCondition.close();
downloadFinishedCondition = new CountDownLatch(1);
}
dummyMainThread.runOnMainThread( dummyMainThread.runOnMainThread(
() -> { () -> {
if (downloadManager.isIdle()) { if (downloadManager.isIdle()) {
Util.castNonNull(downloadFinishedCondition).countDown(); idleCondition.open();
} }
}); });
assertThat(downloadFinishedCondition.await(timeoutMs, TimeUnit.MILLISECONDS)).isTrue(); assertThat(idleCondition.block(TIMEOUT_MS)).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