run function is cancellable by default and accepts optional CoroutineStart
diff --git a/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/RunTest.kt b/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/RunTest.kt
index 31a388d..5a6faff 100644
--- a/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/RunTest.kt
+++ b/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/RunTest.kt
@@ -19,6 +19,8 @@
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.core.IsEqual
import org.junit.Test
+import kotlin.coroutines.experimental.ContinuationInterceptor
+import kotlin.coroutines.experimental.CoroutineContext
class RunTest : TestBase() {
@Test
@@ -124,4 +126,46 @@
assertThat(result, IsEqual("OK"))
finish(4)
}
+
+ @Test(expected = CancellationException::class)
+ fun testRunCancellableDefault() = runBlocking<Unit> {
+ val job = Job()
+ job.cancel() // cancel before it has a chance to run
+ run(job + wrapperDispatcher(context)) {
+ expectUnreached() // will get cancelled
+ }
+ }
+
+ @Test(expected = CancellationException::class)
+ fun testRunAtomicTryCancel() = runBlocking<Unit> {
+ expect(1)
+ val job = Job()
+ job.cancel() // try to cancel before it has a chance to run
+ run(job + wrapperDispatcher(context), CoroutineStart.ATOMIC) { // but start atomically
+ finish(2)
+ yield() // but will cancel here
+ expectUnreached()
+ }
+ }
+
+ @Test(expected = CancellationException::class)
+ fun testRunUndispatchedTryCancel() = runBlocking<Unit> {
+ expect(1)
+ val job = Job()
+ job.cancel() // try to cancel before it has a chance to run
+ run(job + wrapperDispatcher(context), CoroutineStart.UNDISPATCHED) { // but start atomically
+ finish(2)
+ yield() // but will cancel here
+ expectUnreached()
+ }
+ }
+
+ private fun wrapperDispatcher(context: CoroutineContext): CoroutineContext {
+ val dispatcher = context[ContinuationInterceptor] as CoroutineDispatcher
+ return object : CoroutineDispatcher() {
+ override fun dispatch(context: CoroutineContext, block: Runnable) {
+ dispatcher.dispatch(context, block)
+ }
+ }
+ }
}
\ No newline at end of file