diff --git a/instrumentation-tests/src/androidTest/java/com/mapbox/navigation/instrumentation_tests/core/CoreRerouteTest.kt b/instrumentation-tests/src/androidTest/java/com/mapbox/navigation/instrumentation_tests/core/CoreRerouteTest.kt index cbcca80e0f3..9c5933ee21e 100644 --- a/instrumentation-tests/src/androidTest/java/com/mapbox/navigation/instrumentation_tests/core/CoreRerouteTest.kt +++ b/instrumentation-tests/src/androidTest/java/com/mapbox/navigation/instrumentation_tests/core/CoreRerouteTest.kt @@ -877,7 +877,9 @@ class CoreRerouteTest : BaseTest(EmptyTestActivity::class.jav assertEquals(routes[1], rerouteResult.navigationRoutes.first()) } - @Test + // Restarting mock web server takes up to 30 seconds. + // Let it finish and skip the test if it did not succeed. + @Test(timeout = 50_000) fun reroute_with_retryable_error() = sdkTest { val mockRoute = RoutesProvider.dc_very_short(context) val originLocation = mockRoute.routeWaypoints.first() diff --git a/libtesting-ui/src/main/java/com/mapbox/navigation/testing/ui/http/MockWebServerRule.kt b/libtesting-ui/src/main/java/com/mapbox/navigation/testing/ui/http/MockWebServerRule.kt index a0c9536af44..de12576fb6e 100644 --- a/libtesting-ui/src/main/java/com/mapbox/navigation/testing/ui/http/MockWebServerRule.kt +++ b/libtesting-ui/src/main/java/com/mapbox/navigation/testing/ui/http/MockWebServerRule.kt @@ -2,13 +2,17 @@ package com.mapbox.navigation.testing.ui.http import android.util.Log import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeout import kotlinx.coroutines.withTimeoutOrNull import okhttp3.mockwebserver.Dispatcher import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockWebServer import okhttp3.mockwebserver.RecordedRequest +import org.junit.Assume.assumeTrue import org.junit.rules.TestWatcher import org.junit.runner.Description import java.lang.StringBuilder @@ -85,24 +89,30 @@ class MockWebServerRule : TestWatcher() { block() } finally { withContext(Dispatchers.IO) { - retryStarting(previousPort) + val serverRestarted = retryStarting(previousPort) + assumeTrue("Mock web server could not be restarted", serverRestarted) initDispatcher() } } } - private suspend fun retryStarting(port: Int) { - withTimeoutOrNull(30_000) { + private suspend fun retryStarting(port: Int): Boolean { + withTimeout(30_000) { while (true) { + if (!isActive) { + Log.e("MockWebServerRule", "can't start mock server on port $port") + return@withTimeout false + } try { webServer = MockWebServer() webServer.start(port) - break + return@withTimeout true } catch (t: Throwable) { Log.e("MockWebServerRule", "error starting mock web server", t) } delay(500) } - } ?: error("can't start mock server on port $port") + } + return false } }