Skip to content

Commit

Permalink
test optimised routes
Browse files Browse the repository at this point in the history
  • Loading branch information
VysotskiVadim committed Oct 20, 2023
1 parent 37690d1 commit 7748e01
Show file tree
Hide file tree
Showing 4 changed files with 43,993 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ class NavigationRoute internal constructor(
routeRequestUrl: String,
routerOrigin: RouterOrigin,
responseTimeElapsedSeconds: Long?,
routeParser: SDKRouteParser = SDKRouteParser.default
routeParser: SDKRouteParser = SDKRouteParser.default,
optimiseMemory: Boolean = false
): List<NavigationRoute> {
logI("NavigationRoute.createAsync is called", LOG_CATEGORY)

Expand Down Expand Up @@ -230,6 +231,7 @@ class NavigationRoute internal constructor(
deferredResponseParsing.await(),
deferredRouteOptionsParsing.await(),
responseTimeElapsedSeconds,
optimiseMemory
).also {
logD(
"NavigationRoute.createAsync finished " +
Expand Down Expand Up @@ -272,7 +274,13 @@ class NavigationRoute internal constructor(
routeOptionsUrlString,
routerOrigin
).run {
create(this, directionsResponse, routeOptions, responseTimeElapsedSeconds)
create(
this,
directionsResponse,
routeOptions,
responseTimeElapsedSeconds,
optimiseMemory = false
)
}
}

Expand All @@ -281,18 +289,21 @@ class NavigationRoute internal constructor(
directionsResponse: DirectionsResponse,
routeOptions: RouteOptions,
responseTimeElapsedSeconds: Long?,
optimiseMemory: Boolean
): List<NavigationRoute> {
return expected.fold({ error ->
logE("NavigationRoute", "Failed to parse a route. Reason: $error")
listOf()
}, { value ->
value
}).mapIndexed { index, routeInterface ->
val directionResponseWithoutOtherRoutes = directionsResponse.toBuilder().routes(
emptyList()
).build()
val responseToSaveInRoute = if (optimiseMemory) {
directionsResponse.toBuilder().routes(
emptyList()
).build()
} else directionsResponse
NavigationRoute(
directionResponseWithoutOtherRoutes,
responseToSaveInRoute,
index,
routeOptions,
getDirectionsRoute(directionsResponse, index, routeOptions),
Expand All @@ -304,7 +315,7 @@ class NavigationRoute internal constructor(
refreshTtl + responseTimeElapsedSeconds
}
)
}.cache()
}.cache(optimiseMemory)
}
}

Expand Down Expand Up @@ -607,8 +618,10 @@ internal fun RouteInterface.toNavigationRoute(
).cache()
}

private fun List<NavigationRoute>.cache(): List<NavigationRoute> {
//RouteCompatibilityCache.cacheCreationResult(this)
private fun List<NavigationRoute>.cache(optimiseMemory: Boolean = false): List<NavigationRoute> {
if (!optimiseMemory) {
RouteCompatibilityCache.cacheCreationResult(this)
}
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class NavigationRouteTest {
.coordinates("0.0,0.0;1.1,1.1")
.build(),
DirectionsRoute.fromJson(routeJson),
mockk(relaxed = true),
mockk(relaxed = true),
null
)

Expand Down Expand Up @@ -415,4 +415,42 @@ class NavigationRouteTest {
route.updateExpirationTime(45)
assertEquals(45L, route.expirationTimeElapsedSeconds)
}

@Test
fun `optimised navigation route`() = runBlocking<Unit> {
val responseString = FileUtils.loadJsonFixture("route_with_alternatives_response.json")
val responseDataRef = responseString.toDataRef()
val requestUrl = FileUtils.loadJsonFixture("route_with_alternatives_request.txt")

val navigationRoutes = NavigationRoute.createAsync(
responseDataRef,
requestUrl,
RouterOrigin.Offboard,
null,
optimiseMemory = true
)

assertEquals(3, navigationRoutes.size)
assertEquals(emptyList<DirectionsRoute>(), navigationRoutes[0].directionsResponse.routes())
assertEquals(emptyList<DirectionsRoute>(), navigationRoutes[1].directionsResponse.routes())
assertEquals(emptyList<DirectionsRoute>(), navigationRoutes[2].directionsResponse.routes())
}

@Test
fun `not optimised navigation route`() = runBlocking<Unit> {
val responseString = FileUtils.loadJsonFixture("route_with_alternatives_response.json")
val responseDataRef = responseString.toDataRef()
val requestUrl = FileUtils.loadJsonFixture("route_with_alternatives_request.txt")

val navigationRoutes = NavigationRoute.createAsync(
responseDataRef,
requestUrl,
RouterOrigin.Offboard,
null,
optimiseMemory = false
)

assertEquals(3, navigationRoutes.size)
assertEquals(listOf(3, 3, 3), navigationRoutes.map { it.directionsResponse.routes().size })
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://api.mapbox.com/directions/v5/mapbox/driving-traffic/13.323533327548603,52.494284300584894;13.459680057801876,52.52372648556391?steps=true&overview=full&geometries=polyline6&roundabout_exits=true&voice_units=imperial&voice_instructions=true&banner_instructions=true&alternatives=true
Loading

0 comments on commit 7748e01

Please sign in to comment.