Search in sources :

Example 11 with ConditionVariable

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();
}
Also used : ConditionVariable(android.os.ConditionVariable) HandlerThread(android.os.HandlerThread) ArrayList(java.util.ArrayList) HandlerWrapper(com.google.android.exoplayer2.util.HandlerWrapper) Test(org.junit.Test)

Example 12 with ConditionVariable

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;
}
Also used : ConditionVariable(android.os.ConditionVariable) MediaPeriod(com.google.android.exoplayer2.source.MediaPeriod) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 13 with ConditionVariable

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();
}
Also used : ConditionVariable(android.os.ConditionVariable) InterruptedIOException(java.io.InterruptedIOException) HttpDataSourceException(com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 14 with ConditionVariable

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();
}
Also used : ConditionVariable(android.os.ConditionVariable) HttpDataSourceException(com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) InvalidResponseCodeException(com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException) NetworkException(org.chromium.net.NetworkException) HttpDataSourceException(com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException) InterruptedIOException(java.io.InterruptedIOException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Test(org.junit.Test)

Example 15 with ConditionVariable

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();
}
Also used : ConditionVariable(com.google.android.exoplayer2.util.ConditionVariable) Callback(com.google.android.exoplayer2.source.MediaPeriod.Callback) TrackGroupArray(com.google.android.exoplayer2.source.TrackGroupArray) AtomicReference(java.util.concurrent.atomic.AtomicReference) MediaPeriod(com.google.android.exoplayer2.source.MediaPeriod)

Aggregations

Test (org.junit.Test)15 ConditionVariable (android.os.ConditionVariable)12 HttpDataSourceException (com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException)9 ConditionVariable (com.google.android.exoplayer2.util.ConditionVariable)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 InterruptedIOException (java.io.InterruptedIOException)4 SocketTimeoutException (java.net.SocketTimeoutException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 HandlerThread (android.os.HandlerThread)2 MediaPeriod (com.google.android.exoplayer2.source.MediaPeriod)2 HandlerWrapper (com.google.android.exoplayer2.util.HandlerWrapper)2 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Handler (android.os.Handler)1 Looper (android.os.Looper)1 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)1 MediaItem (com.google.android.exoplayer2.MediaItem)1 PlaybackException (com.google.android.exoplayer2.PlaybackException)1 Player (com.google.android.exoplayer2.Player)1 RobolectricUtil.createRobolectricConditionVariable (com.google.android.exoplayer2.robolectric.RobolectricUtil.createRobolectricConditionVariable)1