Created
January 11, 2019 22:20
-
-
Save Groostav/5870edde62a40235f6dc8e67c7830437 to your computer and use it in GitHub Desktop.
This file contains 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
class Tests{ | |
@Test | |
fun `Submits app selection if selections can be made`() = runBlocking(Dispatchers.Main) { //@Test... = runBlocking is a good convention | |
mainActivityViewModel.submitAppSelection(selectedApp) | |
verify(mockAppsStartupFsm).submitEvent(AppSelected(selectedApp)) | |
} | |
} | |
class MainActivityViewModel { | |
suspend fun submitAppSelection(app: App) { //make this suspend | |
if (!selectionsCanBeMade()) return | |
lastSelectedApp = app | |
//val coroutineScope = CoroutineScope(Dispatchers.Default) | |
//coroutineScope.launch { appsStartupFsm.submitEvent(AppSelected(app)) } | |
// different strategy: | |
// 1. explicitly move off Main thread because we **must have** parallelism (for some reason?) | |
// 2. synchronize on the result, only after `submitEvent` returns (not suspends) will `submitAppSelection` return. | |
withContext(Dispatchers.Default) { | |
appsStartupFsm.submitEvent(AppSelected(app)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also worth noting, with a statement like
lastSelectedApp = app
, you might want to require that you're on theMainThread
, surrounding the whole function withwithContext(Dispatchers.Main)
, because if you dont you've got funny thread safety issues