Skip to content

Instantly share code, notes, and snippets.

@tiwiz
Last active October 10, 2018 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiwiz/b85c72fb77148e6cc12aa378b762e94b to your computer and use it in GitHub Desktop.
Save tiwiz/b85c72fb77148e6cc12aa378b762e94b to your computer and use it in GitHub Desktop.
class Selector(private dao: Dao,
dispatcher: CoroutineContext = Dispatchers.IO) : CoroutineScope, ViewModel() {
private val job = Job()
override val coroutineContext = dispatcher + job
fun selectById(id: Int) {
launch {
delay(5000)
dao.syncOperationWith(id)
}
}
override fun onCleared() {
super.onCleared()
job.cancel()
}
}
runBlocking {
val testedObject = Selector(mockDao, coroutineContext)
testedObject.selectById(1337)
verify(mockDao).syncOperationWith(1337)
}
@tiwiz
Copy link
Author

tiwiz commented Oct 10, 2018

Like this, it goes in error

Exception in thread "main @coroutine#2" java.lang.StackOverflowError
	at kotlinx.coroutines.EventLoopBase$DelayedTask.schedule(EventLoop.kt:322)
	at kotlinx.coroutines.EventLoopBase.scheduleImpl(EventLoop.kt:264)
	at kotlinx.coroutines.EventLoopBase.schedule$kotlinx_coroutines_core(EventLoop.kt:248)
        ...
        at kotlinx.coroutines.EventLoopBase.schedule$kotlinx_coroutines_core(EventLoop.kt:248)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment