-
Notifications
You must be signed in to change notification settings - Fork 3
[MS-1418] Replace WorkManager location collection with in-process coroutine #1772
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
feature/setup/src/main/java/com/simprints/feature/setup/location/CollectLocationUseCase.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package com.simprints.feature.setup.location | ||
|
|
||
| import com.simprints.infra.events.event.domain.models.scope.Location | ||
| import com.simprints.infra.logging.Simber | ||
| import kotlinx.coroutines.CancellationException | ||
| import kotlinx.coroutines.flow.filterNotNull | ||
| import javax.inject.Inject | ||
|
|
||
| internal class CollectLocationUseCase @Inject constructor( | ||
| private val locationManager: LocationManager, | ||
| private val updateSessionScopeLocationUseCase: UpdateSessionScopeLocationUseCase, | ||
| ) { | ||
| /** | ||
| * Runs directly in the caller's coroutine context/scope so that cancelling the caller | ||
| * (e.g. cancelling its scope's children) cancels this collection too, without needing to | ||
| * track and cancel a [kotlinx.coroutines.Job] manually. | ||
| */ | ||
| suspend operator fun invoke() { | ||
| val requestStartTimeMs = System.currentTimeMillis() | ||
| Simber.i("Started collecting location", tag = TAG) | ||
| try { | ||
| locationManager | ||
| .requestLocation() | ||
| .filterNotNull() | ||
| .collect { location -> | ||
| try { | ||
| saveUserLocation(location, requestStartTimeMs) | ||
| } catch (c: CancellationException) { | ||
| throw c | ||
| } catch (t: Throwable) { | ||
| Simber.e("Failed to save user's location", t, tag = TAG) | ||
| } | ||
| } | ||
| Simber.d("Finished collecting location (took ${elapsedMs(requestStartTimeMs)}ms)", tag = TAG) | ||
| } catch (c: CancellationException) { | ||
| Simber.d("Stopped collecting location (took ${elapsedMs(requestStartTimeMs)}ms)", tag = TAG) | ||
| throw c | ||
| } catch (t: Throwable) { | ||
| Simber.e("Failed to collect location", t, tag = TAG) | ||
| } | ||
| } | ||
|
|
||
| private suspend fun saveUserLocation( | ||
| location: Location, | ||
| requestStartTimeMs: Long, | ||
| ) { | ||
| updateSessionScopeLocationUseCase(location) | ||
| Simber.d("Saved user's location into the current session (took ${elapsedMs(requestStartTimeMs)}ms)", tag = TAG) | ||
| } | ||
|
|
||
| private fun elapsedMs(requestStartTimeMs: Long) = System.currentTimeMillis() - requestStartTimeMs | ||
|
|
||
| private companion object { | ||
| private const val TAG = "CollectLocationUseCase" | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
feature/setup/src/main/java/com/simprints/feature/setup/location/LocationStoreImpl.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.simprints.feature.setup.location | ||
|
|
||
| import com.simprints.core.AppScope | ||
| import com.simprints.feature.setup.LocationStore | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.SupervisorJob | ||
| import kotlinx.coroutines.cancelChildren | ||
| import kotlinx.coroutines.launch | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| internal class LocationStoreImpl @Inject constructor( | ||
| @AppScope appScope: CoroutineScope, | ||
| private val collectLocation: CollectLocationUseCase, | ||
| ) : LocationStore { | ||
| // A child scope of the app scope so that collection coroutines can be cancelled | ||
| private val scope = CoroutineScope(appScope.coroutineContext + SupervisorJob(appScope.coroutineContext[Job])) | ||
|
|
||
| override fun collectLocationInBackground() { | ||
| scope.coroutineContext.cancelChildren() | ||
| scope.launch { collectLocation() } | ||
| } | ||
|
|
||
| override fun cancelLocationCollection() { | ||
| scope.coroutineContext.cancelChildren() | ||
| } | ||
| } | ||
28 changes: 0 additions & 28 deletions
28
.../setup/src/main/java/com/simprints/feature/setup/location/LocationStoreWorkerScheduler.kt
This file was deleted.
Oops, something went wrong.
50 changes: 0 additions & 50 deletions
50
...in/java/com/simprints/feature/setup/location/StoreUserLocationIntoCurrentSessionWorker.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
feature/setup/src/test/java/com/simprints/feature/setup/location/LocationStoreImplTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package com.simprints.feature.setup.location | ||
|
|
||
| import com.google.common.truth.Truth.assertThat | ||
| import com.simprints.testtools.common.coroutines.TestCoroutineRule | ||
| import io.mockk.MockKAnnotations | ||
| import io.mockk.coEvery | ||
| import io.mockk.coVerify | ||
| import io.mockk.impl.annotations.MockK | ||
| import kotlinx.coroutines.CancellationException | ||
| import kotlinx.coroutines.CompletableDeferred | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.awaitCancellation | ||
| import org.junit.Before | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
|
|
||
| internal class LocationStoreImplTest { | ||
| @get:Rule | ||
| val testCoroutineRule = TestCoroutineRule() | ||
|
|
||
| @MockK | ||
| private lateinit var collectLocation: CollectLocationUseCase | ||
|
|
||
| private lateinit var appScope: CoroutineScope | ||
| private lateinit var locationStore: LocationStoreImpl | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| MockKAnnotations.init(this, relaxed = true) | ||
| appScope = CoroutineScope(testCoroutineRule.testCoroutineDispatcher + Job()) | ||
| locationStore = LocationStoreImpl(appScope, collectLocation) | ||
| } | ||
|
|
||
| @Test | ||
| fun `collectLocationInBackground starts a new collection`() { | ||
| coEvery { collectLocation() } returns Unit | ||
|
|
||
| locationStore.collectLocationInBackground() | ||
|
|
||
| coVerify(exactly = 1) { collectLocation() } | ||
| } | ||
|
|
||
| @Test | ||
| fun `collectLocationInBackground cancels a previous collection before starting a new one`() { | ||
| val firstCollectionCancelled = CompletableDeferred<Unit>() | ||
| coEvery { collectLocation() } coAnswers { | ||
| try { | ||
| awaitCancellation() | ||
| } catch (c: CancellationException) { | ||
| firstCollectionCancelled.complete(Unit) | ||
| throw c | ||
| } | ||
| } | ||
|
|
||
| locationStore.collectLocationInBackground() | ||
| locationStore.collectLocationInBackground() | ||
|
|
||
| assertThat(firstCollectionCancelled.isCompleted).isTrue() | ||
| } | ||
|
|
||
| @Test | ||
| fun `cancelLocationCollection cancels the current collection`() { | ||
| val collectionCancelled = CompletableDeferred<Unit>() | ||
| coEvery { collectLocation() } coAnswers { | ||
| try { | ||
| awaitCancellation() | ||
| } catch (c: CancellationException) { | ||
| collectionCancelled.complete(Unit) | ||
| throw c | ||
| } | ||
| } | ||
|
|
||
| locationStore.collectLocationInBackground() | ||
| locationStore.cancelLocationCollection() | ||
|
|
||
| assertThat(collectionCancelled.isCompleted).isTrue() | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the whole point was to do the collection coroutine in the context/scope of the caller to get the benefit of structured concurrency and avoid tracking this job manually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My Idea was that the collectLocationInBackground gets started in the setup fragment and gets canceled from the orchestrator. That is why I needed ahigher context than the caller so I used AppScope.
I implemented it in a nicer way now in LocationStoreImpl
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the expected result is for location collection to run as long as the session is active. If we switch it to cancel as soon as Setup is done, that's a behaviour change and likely a negative one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BurningAXE The cancellation is happening as before, at the end of the orchestration fragment rather than during the setup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BurningAXE technically, it would be until location is received or session interrupted. This implementation seems to be slightly simpler to understand, although I am surprised that the time benefits are negligible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was commenting on the suggestion to use the scope of the caller which is the Setup VM. Would have been easier if we had a dedicated session scope. (We do have SessionCoroutineScope but that one is strictly reserved for events and I don't think this single use case merits creating a new one)