use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.
the class FakeClockTest method createHandler_multiThreadCommunication_deliversMessagesDeterministicallyInOrder.
@Test
public void createHandler_multiThreadCommunication_deliversMessagesDeterministicallyInOrder() {
HandlerThread handlerThread1 = new HandlerThread("FakeClockTest");
handlerThread1.start();
HandlerThread handlerThread2 = new HandlerThread("FakeClockTest");
handlerThread2.start();
FakeClock fakeClock = new FakeClock(/* initialTimeMs= */
0);
HandlerWrapper handler1 = fakeClock.createHandler(handlerThread1.getLooper(), /* callback= */
null);
HandlerWrapper handler2 = fakeClock.createHandler(handlerThread2.getLooper(), /* callback= */
null);
ConditionVariable messagesFinished = new ConditionVariable();
ArrayList<Integer> executionOrder = new ArrayList<>();
handler1.post(() -> {
executionOrder.add(1);
handler2.post(() -> executionOrder.add(2));
handler1.post(() -> executionOrder.add(3));
handler2.post(() -> {
executionOrder.add(4);
handler2.post(() -> executionOrder.add(7));
handler1.post(() -> {
executionOrder.add(8);
messagesFinished.open();
});
});
handler2.post(() -> executionOrder.add(5));
handler1.post(() -> executionOrder.add(6));
});
ShadowLooper.idleMainLooper();
messagesFinished.block();
assertThat(executionOrder).containsExactly(1, 2, 3, 4, 5, 6, 7, 8).inOrder();
}
use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.
the class MediaSourceTestRunner method preparePeriod.
/**
* Calls {@link MediaPeriod#prepare(MediaPeriod.Callback, long)} on the playback thread and blocks
* until the method has been called.
*
* @param mediaPeriod The {@link MediaPeriod} to prepare.
* @param positionUs The position at which to prepare.
* @return A {@link CountDownLatch} that will be counted down when preparation completes.
*/
public CountDownLatch preparePeriod(final MediaPeriod mediaPeriod, final long positionUs) {
final ConditionVariable prepareCalled = new ConditionVariable();
final CountDownLatch preparedLatch = new CountDownLatch(1);
runOnPlaybackThread(() -> {
mediaPeriod.prepare(new MediaPeriod.Callback() {
@Override
public void onPrepared(MediaPeriod mediaPeriod1) {
preparedLatch.countDown();
}
@Override
public void onContinueLoadingRequested(MediaPeriod source) {
// Do nothing.
}
}, positionUs);
prepareCalled.open();
});
prepareCalled.block();
return preparedLatch;
}
use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.
the class CronetDataSourceTest method readByteBufferInterrupted.
@Test
public void readByteBufferInterrupted() throws HttpDataSourceException, InterruptedException {
mockResponseStartSuccess();
dataSourceUnderTest.open(testDataSpec);
final ConditionVariable startCondition = buildReadStartedCondition();
final CountDownLatch timedOutLatch = new CountDownLatch(1);
ByteBuffer returnedBuffer = ByteBuffer.allocateDirect(8);
Thread thread = new Thread() {
@Override
public void run() {
try {
dataSourceUnderTest.read(returnedBuffer);
fail();
} catch (HttpDataSourceException e) {
// Expected.
assertThat(e).hasCauseThat().isInstanceOf(InterruptedIOException.class);
timedOutLatch.countDown();
}
}
};
thread.start();
startCondition.block();
assertNotCountedDown(timedOutLatch);
// Now we interrupt.
thread.interrupt();
timedOutLatch.await();
}
use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.
the class CronetDataSourceTest method connectResponseBeforeTimeout.
@Test
public void connectResponseBeforeTimeout() throws Exception {
long startTimeMs = SystemClock.elapsedRealtime();
final ConditionVariable startCondition = buildUrlRequestStartedCondition();
final CountDownLatch openLatch = new CountDownLatch(1);
AtomicReference<Exception> exceptionOnTestThread = new AtomicReference<>();
new Thread() {
@Override
public void run() {
try {
dataSourceUnderTest.open(testDataSpec);
} catch (HttpDataSourceException e) {
exceptionOnTestThread.set(e);
} finally {
openLatch.countDown();
}
}
}.start();
startCondition.block();
// We should still be trying to open.
assertNotCountedDown(openLatch);
// We should still be trying to open as we approach the timeout.
setSystemClockInMsAndTriggerPendingMessages(/* nowMs= */
startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1);
assertNotCountedDown(openLatch);
// The response arrives just in time.
dataSourceUnderTest.urlRequestCallback.onResponseStarted(mockUrlRequest, testUrlResponseInfo);
openLatch.await();
assertThat(exceptionOnTestThread.get()).isNull();
}
use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.
the class MediaPeriodAsserts method prepareAndGetTrackGroups.
private static TrackGroupArray prepareAndGetTrackGroups(MediaPeriod mediaPeriod) {
AtomicReference<TrackGroupArray> trackGroupArray = new AtomicReference<>();
DummyMainThread testThread = new DummyMainThread();
ConditionVariable preparedCondition = new ConditionVariable();
testThread.runOnMainThread(() -> mediaPeriod.prepare(new Callback() {
@Override
public void onPrepared(MediaPeriod mediaPeriod) {
trackGroupArray.set(mediaPeriod.getTrackGroups());
preparedCondition.open();
}
@Override
public void onContinueLoadingRequested(MediaPeriod source) {
// Ignore.
}
}, /* positionUs= */
0));
try {
preparedCondition.block();
} catch (InterruptedException e) {
// Ignore.
}
testThread.release();
return trackGroupArray.get();
}
Aggregations