Skip to content

Instantly share code, notes, and snippets.

@ZakTaccardi
Last active May 7, 2020 19:56
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 ZakTaccardi/e77d5983660ce4e8e0fa2f2ef0d582ea to your computer and use it in GitHub Desktop.
Save ZakTaccardi/e77d5983660ce4e8e0fa2f2ef0d582ea to your computer and use it in GitHub Desktop.
Don't break the chain
package com.capitalone.android.coroutines
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
import kotlinx.coroutines.runBlocking
import org.junit.Test
import kotlin.coroutines.ContinuationInterceptor
class DontBreakTheChainTest {
@Test
fun this_test_passes_when_an_exception_is_thrown() = runBlocking<Unit> {
val parentScope = this
val parentDispatcher = parentScope.coroutineContext[ContinuationInterceptor]!! as CoroutineDispatcher
// break structured concurrency
val newScope = CoroutineScope(parentDispatcher)
newScope.launch {
throw Exception("fail")
}
.join()
}
@Test
fun this_test_fails_when_an_exception_is_thrown() = runBlocking<Unit> {
val parentScope = this
// keep structured concurrency alive
val newScope = parentScope + Dispatchers.Unconfined
newScope.launch {
throw Exception("fail")
}
.join()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment