Commit 93f2dd8d by ibaker Committed by Oliver Woodman

Switch test usages of ConditionVariable#block(timeout) to CountDownLatch

ConditionVariable.block(timeout) doesn't work in Robolectric, because it
relies on the system clock which doesn't advance.

PiperOrigin-RevId: 275798281
parent 4e667b89
......@@ -40,13 +40,14 @@ import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.MappingTrackSelector.MappedTrackInfo;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.upstream.Allocator;
import com.google.android.exoplayer2.util.ConditionVariable;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Before;
import org.junit.Test;
......@@ -412,21 +413,21 @@ public class DownloadHelperTest {
private static void prepareDownloadHelper(DownloadHelper downloadHelper) throws Exception {
AtomicReference<Exception> prepareException = new AtomicReference<>(null);
ConditionVariable preparedCondition = new ConditionVariable();
CountDownLatch preparedLatch = new CountDownLatch(1);
downloadHelper.prepare(
new Callback() {
@Override
public void onPrepared(DownloadHelper helper) {
preparedCondition.open();
preparedLatch.countDown();
}
@Override
public void onPrepareError(DownloadHelper helper, IOException e) {
prepareException.set(e);
preparedCondition.open();
preparedLatch.countDown();
}
});
while (!preparedCondition.block(0)) {
while (!preparedLatch.await(0, TimeUnit.MILLISECONDS)) {
shadowMainLooper().idleFor(shadowMainLooper().getNextScheduledTaskTime());
}
if (prepareException.get() != null) {
......
......@@ -23,7 +23,6 @@ import static com.google.android.exoplayer2.testutil.CacheAsserts.assertCachedDa
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.os.ConditionVariable;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.offline.DefaultDownloadIndex;
......@@ -47,6 +46,8 @@ import com.google.android.exoplayer2.util.Util;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
......@@ -61,7 +62,7 @@ import org.robolectric.shadows.ShadowLog;
@LooperMode(LooperMode.Mode.PAUSED)
public class DownloadManagerDashTest {
private static final int ASSERT_TRUE_TIMEOUT = 1000;
private static final int ASSERT_TRUE_TIMEOUT_MS = 1000;
private SimpleCache cache;
private File tempFolder;
......@@ -204,16 +205,17 @@ public class DownloadManagerDashTest {
@Test
public void testHandleInterferingRemoveAction() throws Throwable {
final ConditionVariable downloadInProgressCondition = new ConditionVariable();
CountDownLatch downloadInProgressLatch = new CountDownLatch(1);
fakeDataSet
.newData("audio_segment_2")
.appendReadAction(downloadInProgressCondition::open)
.appendReadAction(downloadInProgressLatch::countDown)
.appendReadData(TestUtil.buildTestData(5))
.endData();
handleDownloadRequest(fakeStreamKey1);
assertThat(downloadInProgressCondition.block(ASSERT_TRUE_TIMEOUT)).isTrue();
assertThat(downloadInProgressLatch.await(ASSERT_TRUE_TIMEOUT_MS, TimeUnit.MILLISECONDS))
.isTrue();
handleRemoveAction();
......@@ -261,7 +263,7 @@ public class DownloadManagerDashTest {
downloadManagerListener =
new TestDownloadManagerListener(
downloadManager, dummyMainThread, /* timeout= */ 3000);
downloadManager, dummyMainThread, /* timeoutMs= */ 3000);
downloadManager.resumeDownloads();
});
}
......
......@@ -17,11 +17,12 @@ package com.google.android.exoplayer2.testutil;
import static com.google.common.truth.Truth.assertThat;
import android.os.ConditionVariable;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import com.google.android.exoplayer2.util.Util;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
/** Helper class to simulate main/UI thread in tests. */
......@@ -90,7 +91,7 @@ public final class DummyMainThread {
Util.sneakyThrow(e);
}
} else {
ConditionVariable finishedCondition = new ConditionVariable();
CountDownLatch finishedLatch = new CountDownLatch(1);
AtomicReference<Throwable> thrown = new AtomicReference<>();
handler.post(
() -> {
......@@ -99,9 +100,13 @@ public final class DummyMainThread {
} catch (Throwable t) {
thrown.set(t);
}
finishedCondition.open();
finishedLatch.countDown();
});
assertThat(finishedCondition.block(timeoutMs)).isTrue();
try {
assertThat(finishedLatch.await(timeoutMs, TimeUnit.MILLISECONDS)).isTrue();
} catch (InterruptedException e) {
Util.sneakyThrow(e);
}
if (thrown.get() != null) {
Util.sneakyThrow(thrown.get());
}
......
......@@ -89,8 +89,8 @@ public class MediaSourceTestRunner {
* @param runnable The {@link Runnable} to run.
*/
public void runOnPlaybackThread(final Runnable runnable) {
final Throwable[] throwable = new Throwable[1];
final ConditionVariable finishedCondition = new ConditionVariable();
Throwable[] throwable = new Throwable[1];
CountDownLatch finishedLatch = new CountDownLatch(1);
playbackHandler.post(
() -> {
try {
......@@ -98,10 +98,14 @@ public class MediaSourceTestRunner {
} catch (Throwable e) {
throwable[0] = e;
} finally {
finishedCondition.open();
finishedLatch.countDown();
}
});
assertThat(finishedCondition.block(TIMEOUT_MS)).isTrue();
try {
assertThat(finishedLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
} catch (InterruptedException e) {
Util.sneakyThrow(e);
}
if (throwable[0] != null) {
Util.sneakyThrow(throwable[0]);
}
......@@ -169,14 +173,14 @@ public class MediaSourceTestRunner {
*/
public CountDownLatch preparePeriod(final MediaPeriod mediaPeriod, final long positionUs) {
final ConditionVariable prepareCalled = new ConditionVariable();
final CountDownLatch preparedCountDown = new CountDownLatch(1);
final CountDownLatch preparedLatch = new CountDownLatch(1);
runOnPlaybackThread(
() -> {
mediaPeriod.prepare(
new MediaPeriod.Callback() {
@Override
public void onPrepared(MediaPeriod mediaPeriod1) {
preparedCountDown.countDown();
preparedLatch.countDown();
}
@Override
......@@ -188,7 +192,7 @@ public class MediaSourceTestRunner {
prepareCalled.open();
});
prepareCalled.block();
return preparedCountDown;
return preparedLatch;
}
/**
......@@ -267,8 +271,8 @@ public class MediaSourceTestRunner {
throws InterruptedException {
MediaPeriod mediaPeriod = createPeriod(mediaPeriodId);
assertThat(lastCreatedMediaPeriod.getAndSet(/* newValue= */ null)).isEqualTo(mediaPeriodId);
CountDownLatch preparedCondition = preparePeriod(mediaPeriod, 0);
assertThat(preparedCondition.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
CountDownLatch preparedLatch = preparePeriod(mediaPeriod, 0);
assertThat(preparedLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
// MediaSource is supposed to support multiple calls to createPeriod without an intervening call
// to releasePeriod.
MediaPeriodId secondMediaPeriodId =
......@@ -280,8 +284,8 @@ public class MediaSourceTestRunner {
MediaPeriod secondMediaPeriod = createPeriod(secondMediaPeriodId);
assertThat(lastCreatedMediaPeriod.getAndSet(/* newValue= */ null))
.isEqualTo(secondMediaPeriodId);
CountDownLatch secondPreparedCondition = preparePeriod(secondMediaPeriod, 0);
assertThat(secondPreparedCondition.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
CountDownLatch secondPreparedLatch = preparePeriod(secondMediaPeriod, 0);
assertThat(secondPreparedLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
// Release the periods.
releasePeriod(mediaPeriod);
assertThat(lastReleasedMediaPeriod.getAndSet(/* newValue= */ null)).isEqualTo(mediaPeriodId);
......
......@@ -18,7 +18,6 @@ package com.google.android.exoplayer2.testutil;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import android.os.ConditionVariable;
import com.google.android.exoplayer2.offline.Download;
import com.google.android.exoplayer2.offline.Download.State;
import com.google.android.exoplayer2.offline.DownloadManager;
......@@ -30,31 +29,31 @@ import java.util.concurrent.TimeUnit;
/** A {@link DownloadManager.Listener} for testing. */
public final class TestDownloadManagerListener implements DownloadManager.Listener {
private static final int TIMEOUT = 1000;
private static final int INITIALIZATION_TIMEOUT = 10000;
private static final int TIMEOUT_MS = 1000;
private static final int INITIALIZATION_TIMEOUT_MS = 10_000;
private static final int STATE_REMOVED = -1;
private final DownloadManager downloadManager;
private final DummyMainThread dummyMainThread;
private final HashMap<String, ArrayBlockingQueue<Integer>> downloadStates;
private final ConditionVariable initializedCondition;
private final int timeout;
private final CountDownLatch initializedCondition;
private final int timeoutMs;
private CountDownLatch downloadFinishedCondition;
@Download.FailureReason private int failureReason;
public TestDownloadManagerListener(
DownloadManager downloadManager, DummyMainThread dummyMainThread) {
this(downloadManager, dummyMainThread, TIMEOUT);
this(downloadManager, dummyMainThread, TIMEOUT_MS);
}
public TestDownloadManagerListener(
DownloadManager downloadManager, DummyMainThread dummyMainThread, int timeout) {
DownloadManager downloadManager, DummyMainThread dummyMainThread, int timeoutMs) {
this.downloadManager = downloadManager;
this.dummyMainThread = dummyMainThread;
this.timeout = timeout;
this.timeoutMs = timeoutMs;
downloadStates = new HashMap<>();
initializedCondition = new ConditionVariable();
initializedCondition = new CountDownLatch(1);
downloadManager.addListener(this);
}
......@@ -64,12 +63,13 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
@Override
public void onInitialized(DownloadManager downloadManager) {
initializedCondition.open();
initializedCondition.countDown();
}
public void waitUntilInitialized() {
public void waitUntilInitialized() throws InterruptedException {
if (!downloadManager.isInitialized()) {
assertThat(initializedCondition.block(INITIALIZATION_TIMEOUT)).isTrue();
assertThat(initializedCondition.await(INITIALIZATION_TIMEOUT_MS, TimeUnit.MILLISECONDS))
.isTrue();
}
}
......@@ -115,7 +115,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
downloadFinishedCondition.countDown();
}
});
assertThat(downloadFinishedCondition.await(timeout, TimeUnit.MILLISECONDS)).isTrue();
assertThat(downloadFinishedCondition.await(timeoutMs, TimeUnit.MILLISECONDS)).isTrue();
}
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