1 /* 2 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.coroutines 6 7 import kotlin.test.* 8 9 class ExperimentalDispatchModeTest : TestBase() { 10 @Test <lambda>null11 fun testUnconfinedCancellation() = runTest { 12 val parent = Job() 13 launch(parent) { 14 expect(1) 15 parent.cancel() 16 launch(Dispatchers.Unconfined) { 17 expectUnreached() 18 } 19 20 }.join() 21 finish(2) 22 } 23 24 @Test <lambda>null25 fun testUnconfinedCancellationState() = runTest { 26 val parent = Job() 27 launch(parent) { 28 expect(1) 29 parent.cancel() 30 val job = launch(Dispatchers.Unconfined) { 31 expectUnreached() 32 } 33 34 assertTrue(job.isCancelled) 35 assertTrue(job.isCompleted) 36 assertFalse(job.isActive) 37 }.join() 38 finish(2) 39 } 40 41 @Test <lambda>null42 fun testUnconfinedCancellationLazy() = runTest { 43 val parent = Job() 44 launch(parent) { 45 expect(1) 46 val job = launch(Dispatchers.Unconfined, start = CoroutineStart.LAZY) { 47 expectUnreached() 48 } 49 job.invokeOnCompletion { expect(2) } 50 assertFalse(job.isCompleted) 51 52 parent.cancel() 53 job.join() 54 }.join() 55 finish(3) 56 } 57 58 @Test <lambda>null59 fun testUndispatchedCancellation() = runTest { 60 val parent = Job() 61 launch(parent) { 62 expect(1) 63 parent.cancel() 64 launch(start = CoroutineStart.UNDISPATCHED) { 65 expect(2) 66 yield() 67 expectUnreached() 68 } 69 70 }.join() 71 finish(3) 72 } 73 74 @Test <lambda>null75 fun testCancelledAtomicUnconfined() = runTest { 76 val parent = Job() 77 launch(parent) { 78 expect(1) 79 parent.cancel() 80 launch(Dispatchers.Unconfined, start = CoroutineStart.ATOMIC) { 81 expect(2) 82 yield() 83 expectUnreached() 84 } 85 }.join() 86 finish(3) 87 } 88 89 90 @Test <lambda>null91 fun testCancelledWithContextUnconfined() = runTest { 92 val parent = Job() 93 launch(parent) { 94 expect(1) 95 parent.cancel() 96 withContext(Dispatchers.Unconfined) { 97 expectUnreached() 98 } 99 }.join() 100 finish(2) 101 } 102 }