Skip to content

Commit

Permalink
throw exception if coroutine is cancelled while server is restarting
Browse files Browse the repository at this point in the history
  • Loading branch information
dzinad committed Nov 13, 2023
1 parent 766c634 commit cb5bcc9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,9 @@ class CoreRerouteTest : BaseTest<EmptyTestActivity>(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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

0 comments on commit cb5bcc9

Please sign in to comment.