Last active
May 7, 2020 19:56
-
-
Save ZakTaccardi/e77d5983660ce4e8e0fa2f2ef0d582ea to your computer and use it in GitHub Desktop.
Don't break the chain
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
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