Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAVAND-1564: do not resume route refreshes after routes are changed #7581

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/unreleased/bugfixes/7581.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed an issue where route refreshes might not have been paused after invoking `RouteRefreshController#pauseRouteRefreshes`.
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ internal class PlannedRouteRefreshController @VisibleForTesting constructor(
RetryRouteRefreshStrategy(maxAttemptsCount = MAX_RETRY_COUNT)
)

private var plannedRefreshScope = CoroutineUtils.createChildScope(parentScope)
private var paused = false
// null if refreshes are paused
private var plannedRefreshScope: CoroutineScope? = CoroutineUtils.createChildScope(parentScope)
var routesToRefresh: List<NavigationRoute>? = null
private set

fun startRoutesRefreshing(routes: List<NavigationRoute>) {
recreateScope()
if (plannedRefreshScope != null) {
plannedRefreshScope?.cancel()
plannedRefreshScope = CoroutineUtils.createChildScope(parentScope)
}
if (routes.isEmpty()) {
routesToRefresh = null
logI("Routes are empty, nothing to refresh", RouteRefreshLog.LOG_CATEGORY)
Expand Down Expand Up @@ -82,15 +85,15 @@ internal class PlannedRouteRefreshController @VisibleForTesting constructor(
}

fun pause() {
if (!paused) {
paused = true
recreateScope()
}
logI("Pausing refreshes", RouteRefreshLog.LOG_CATEGORY)
plannedRefreshScope?.cancel()
plannedRefreshScope = null
}

fun resume() {
if (paused) {
paused = false
logI("Resuming refreshes", RouteRefreshLog.LOG_CATEGORY)
if (plannedRefreshScope == null) {
plannedRefreshScope = CoroutineUtils.createChildScope(parentScope)
routesToRefresh?.let {
if (retryStrategy.shouldRetry()) {
scheduleUpdateRetry(it, shouldNotifyOnStart = true)
Expand All @@ -114,7 +117,7 @@ internal class PlannedRouteRefreshController @VisibleForTesting constructor(
}

private fun postAttempt(shouldResume: Boolean, attemptBlock: suspend () -> Unit) {
plannedRefreshScope.launch {
plannedRefreshScope?.launch {
try {
if (shouldResume) {
delayer.resumeDelay()
Expand Down Expand Up @@ -177,11 +180,6 @@ internal class PlannedRouteRefreshController @VisibleForTesting constructor(
}
}

private fun recreateScope() {
plannedRefreshScope.cancel()
plannedRefreshScope = CoroutineUtils.createChildScope(parentScope)
}

companion object {

const val MAX_RETRY_COUNT = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ internal class RouteRefreshPauseResumeIntegrationTest : RouteRefreshIntegrationT
routeRefreshController.destroy()
}

@Test
fun pause_refresh_while_request_is_in_progress() = runBlocking {
val routes = setUpRoutes("route_response_single_route_refresh.json", responseDelay = 2000)
routeRefreshController = createRefreshController(refreshInterval = 60_000)
routeRefreshController.registerRouteRefreshObserver(refreshObserver)
routeRefreshController.onRoutesChanged(
RoutesUpdatedResult(routes, emptyList(), RoutesExtra.ROUTES_UPDATE_REASON_NEW)
)
testDispatcher.advanceTimeBy(61_000)

routeRefreshController.pauseRouteRefreshes()

testDispatcher.advanceTimeBy(1_000)

assertEquals(0, refreshObserver.refreshes.size)

testDispatcher.advanceTimeBy(62_000)

assertEquals(0, refreshObserver.refreshes.size)
}

@Test
fun request_new_routes_planned_refresh_when_refresh_is_paused() = runBlocking {
val routes = setUpRoutes("route_response_single_route_refresh.json")
Expand All @@ -62,6 +83,6 @@ internal class RouteRefreshPauseResumeIntegrationTest : RouteRefreshIntegrationT
assertEquals(1, refreshObserver.refreshes.size)

testDispatcher.advanceTimeBy(30_000)
assertEquals(2, refreshObserver.refreshes.size)
assertEquals(1, refreshObserver.refreshes.size)
}
}