Search in sources :

Example 16 with ConditionVariable

use of android.os.ConditionVariable in project ExoPlayer by google.

the class CronetDataSourceTest method testRedirectIncreasesConnectionTimeout.

@Test
public void testRedirectIncreasesConnectionTimeout() throws InterruptedException {
    when(mockClock.elapsedRealtime()).thenReturn(0L);
    final ConditionVariable startCondition = buildUrlRequestStartedCondition();
    final ConditionVariable timedOutCondition = new ConditionVariable();
    final AtomicInteger openExceptions = new AtomicInteger(0);
    new Thread() {

        @Override
        public void run() {
            try {
                dataSourceUnderTest.open(testDataSpec);
                fail();
            } catch (HttpDataSourceException e) {
                // Expected.
                assertTrue(e instanceof CronetDataSource.OpenException);
                assertTrue(e.getCause() instanceof SocketTimeoutException);
                openExceptions.getAndIncrement();
                timedOutCondition.open();
            }
        }
    }.start();
    startCondition.block();
    // We should still be trying to open.
    assertFalse(timedOutCondition.block(50));
    // We should still be trying to open as we approach the timeout.
    when(mockClock.elapsedRealtime()).thenReturn((long) TEST_CONNECT_TIMEOUT_MS - 1);
    assertFalse(timedOutCondition.block(50));
    // A redirect arrives just in time.
    dataSourceUnderTest.onRedirectReceived(mockUrlRequest, testUrlResponseInfo, "RandomRedirectedUrl1");
    long newTimeoutMs = 2 * TEST_CONNECT_TIMEOUT_MS - 1;
    when(mockClock.elapsedRealtime()).thenReturn(newTimeoutMs - 1);
    // Give the thread some time to run.
    assertFalse(timedOutCondition.block(newTimeoutMs));
    // We should still be trying to open as we approach the new timeout.
    assertFalse(timedOutCondition.block(50));
    // A redirect arrives just in time.
    dataSourceUnderTest.onRedirectReceived(mockUrlRequest, testUrlResponseInfo, "RandomRedirectedUrl2");
    newTimeoutMs = 3 * TEST_CONNECT_TIMEOUT_MS - 2;
    when(mockClock.elapsedRealtime()).thenReturn(newTimeoutMs - 1);
    // Give the thread some time to run.
    assertFalse(timedOutCondition.block(newTimeoutMs));
    // We should still be trying to open as we approach the new timeout.
    assertFalse(timedOutCondition.block(50));
    // Now we timeout.
    when(mockClock.elapsedRealtime()).thenReturn(newTimeoutMs);
    timedOutCondition.block();
    verify(mockTransferListener, never()).onTransferStart(dataSourceUnderTest, testDataSpec);
    assertEquals(1, openExceptions.get());
}
Also used : ConditionVariable(android.os.ConditionVariable) SocketTimeoutException(java.net.SocketTimeoutException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpDataSourceException(com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException) Test(org.junit.Test)

Example 17 with ConditionVariable

use of android.os.ConditionVariable in project ExoPlayer by google.

the class CronetDataSourceTest method testConnectTimeout.

@Test
public void testConnectTimeout() {
    when(mockClock.elapsedRealtime()).thenReturn(0L);
    final ConditionVariable startCondition = buildUrlRequestStartedCondition();
    final ConditionVariable timedOutCondition = new ConditionVariable();
    new Thread() {

        @Override
        public void run() {
            try {
                dataSourceUnderTest.open(testDataSpec);
                fail();
            } catch (HttpDataSourceException e) {
                // Expected.
                assertTrue(e instanceof CronetDataSource.OpenException);
                assertTrue(e.getCause() instanceof SocketTimeoutException);
                assertEquals(TEST_CONNECTION_STATUS, ((CronetDataSource.OpenException) e).cronetConnectionStatus);
                timedOutCondition.open();
            }
        }
    }.start();
    startCondition.block();
    // We should still be trying to open.
    assertFalse(timedOutCondition.block(50));
    // We should still be trying to open as we approach the timeout.
    when(mockClock.elapsedRealtime()).thenReturn((long) TEST_CONNECT_TIMEOUT_MS - 1);
    assertFalse(timedOutCondition.block(50));
    // Now we timeout.
    when(mockClock.elapsedRealtime()).thenReturn((long) TEST_CONNECT_TIMEOUT_MS);
    timedOutCondition.block();
    verify(mockTransferListener, never()).onTransferStart(dataSourceUnderTest, testDataSpec);
}
Also used : ConditionVariable(android.os.ConditionVariable) SocketTimeoutException(java.net.SocketTimeoutException) HttpDataSourceException(com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException) Test(org.junit.Test)

Example 18 with ConditionVariable

use of android.os.ConditionVariable in project ExoPlayer by google.

the class CronetDataSourceTest method testConnectResponseBeforeTimeout.

@Test
public void testConnectResponseBeforeTimeout() {
    when(mockClock.elapsedRealtime()).thenReturn(0L);
    final ConditionVariable startCondition = buildUrlRequestStartedCondition();
    final ConditionVariable openCondition = new ConditionVariable();
    new Thread() {

        @Override
        public void run() {
            try {
                dataSourceUnderTest.open(testDataSpec);
                openCondition.open();
            } catch (HttpDataSourceException e) {
                fail();
            }
        }
    }.start();
    startCondition.block();
    // We should still be trying to open.
    assertFalse(openCondition.block(50));
    // We should still be trying to open as we approach the timeout.
    when(mockClock.elapsedRealtime()).thenReturn((long) TEST_CONNECT_TIMEOUT_MS - 1);
    assertFalse(openCondition.block(50));
    // The response arrives just in time.
    dataSourceUnderTest.onResponseStarted(mockUrlRequest, testUrlResponseInfo);
    openCondition.block();
}
Also used : ConditionVariable(android.os.ConditionVariable) HttpDataSourceException(com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException) Test(org.junit.Test)

Example 19 with ConditionVariable

use of android.os.ConditionVariable in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityServiceTest method testNetworkPinner.

@SmallTest
public void testNetworkPinner() {
    NetworkRequest wifiRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_WIFI).build();
    assertNull(mCm.getBoundNetworkForProcess());
    TestNetworkPinner.pin(mServiceContext, wifiRequest);
    assertNull(mCm.getBoundNetworkForProcess());
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.connect(true);
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(false);
    // When wi-fi connects, expect to be pinned.
    assertTrue(TestNetworkPinner.awaitPin(100));
    assertPinnedToWifiWithCellDefault();
    // Disconnect and expect the pin to drop.
    mWiFiNetworkAgent.disconnect();
    assertTrue(TestNetworkPinner.awaitUnpin(100));
    assertNotPinnedToWifi();
    // Reconnecting does not cause the pin to come back.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(false);
    assertFalse(TestNetworkPinner.awaitPin(100));
    assertNotPinnedToWifi();
    // Pinning while connected causes the pin to take effect immediately.
    TestNetworkPinner.pin(mServiceContext, wifiRequest);
    assertTrue(TestNetworkPinner.awaitPin(100));
    assertPinnedToWifiWithCellDefault();
    // Explicitly unpin and expect to use the default network again.
    TestNetworkPinner.unpin();
    assertNotPinnedToWifi();
    // Disconnect cell and wifi.
    // cell down, wifi up, wifi down.
    ConditionVariable cv = waitForConnectivityBroadcasts(3);
    mCellNetworkAgent.disconnect();
    mWiFiNetworkAgent.disconnect();
    waitFor(cv);
    // Pinning takes effect even if the pinned network is the default when the pin is set...
    TestNetworkPinner.pin(mServiceContext, wifiRequest);
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(false);
    assertTrue(TestNetworkPinner.awaitPin(100));
    assertPinnedToWifiWithWifiDefault();
    // ... and is maintained even when that network is no longer the default.
    cv = waitForConnectivityBroadcasts(1);
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mCellNetworkAgent.connect(true);
    waitFor(cv);
    assertPinnedToWifiWithCellDefault();
}
Also used : ConditionVariable(android.os.ConditionVariable) NetworkRequest(android.net.NetworkRequest) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 20 with ConditionVariable

use of android.os.ConditionVariable in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityServiceTest method testIdleableHandlerThread.

// Tests that IdleableHandlerThread works as expected.
public void testIdleableHandlerThread() {
    // Causes the test to take about 200ms on bullhead-eng.
    final int attempts = 50;
    // Tests that waitForIdle returns immediately if the service is already idle.
    for (int i = 0; i < attempts; i++) {
        mService.waitForIdle();
    }
    // Bring up a network that we can use to send messages to ConnectivityService.
    ConditionVariable cv = waitForConnectivityBroadcasts(1);
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(false);
    waitFor(cv);
    Network n = mWiFiNetworkAgent.getNetwork();
    assertNotNull(n);
    // Tests that calling waitForIdle waits for messages to be processed.
    for (int i = 0; i < attempts; i++) {
        mWiFiNetworkAgent.setSignalStrength(i);
        mService.waitForIdle();
        assertEquals(i, mCm.getNetworkCapabilities(n).getSignalStrength());
    }
    // Ensure that not calling waitForIdle causes a race condition.
    for (int i = 0; i < attempts; i++) {
        mWiFiNetworkAgent.setSignalStrength(i);
        if (i != mCm.getNetworkCapabilities(n).getSignalStrength()) {
            // We hit a race condition, as expected. Pass the test.
            return;
        }
    }
    // No race? There is a bug in this test.
    fail("expected race condition at least once in " + attempts + " attempts");
}
Also used : ConditionVariable(android.os.ConditionVariable) Network(android.net.Network)

Aggregations

ConditionVariable (android.os.ConditionVariable)122 LargeTest (android.test.suitebuilder.annotation.LargeTest)36 NetworkRequest (android.net.NetworkRequest)24 SmallTest (android.test.suitebuilder.annotation.SmallTest)17 Handler (android.os.Handler)16 Messenger (android.os.Messenger)8 Test (org.junit.Test)6 Network (android.net.Network)5 Message (android.os.Message)5 PendingIntent (android.app.PendingIntent)4 BroadcastReceiver (android.content.BroadcastReceiver)4 Context (android.content.Context)4 Intent (android.content.Intent)4 IntentFilter (android.content.IntentFilter)4 ConnectivityManager (android.net.ConnectivityManager)4 NetworkCallback (android.net.ConnectivityManager.NetworkCallback)4 DataUsageRequest (android.net.DataUsageRequest)4 NetworkCapabilities (android.net.NetworkCapabilities)4 NetworkStats (android.net.NetworkStats)4 HandlerThread (android.os.HandlerThread)4