1 package kotlinx.coroutines 2 3 import kotlinx.coroutines.testing.* 4 import kotlin.test.* 5 6 class ExperimentalDispatchModeTest : TestBase() { 7 @Test <lambda>null8 fun testUnconfinedCancellation() = runTest { 9 val parent = Job() 10 launch(parent) { 11 expect(1) 12 parent.cancel() 13 launch(Dispatchers.Unconfined) { 14 expectUnreached() 15 } 16 17 }.join() 18 finish(2) 19 } 20 21 @Test <lambda>null22 fun testUnconfinedCancellationState() = runTest { 23 val parent = Job() 24 launch(parent) { 25 expect(1) 26 parent.cancel() 27 val job = launch(Dispatchers.Unconfined) { 28 expectUnreached() 29 } 30 31 assertTrue(job.isCancelled) 32 assertTrue(job.isCompleted) 33 assertFalse(job.isActive) 34 }.join() 35 finish(2) 36 } 37 38 @Test <lambda>null39 fun testUnconfinedCancellationLazy() = runTest { 40 val parent = Job() 41 launch(parent) { 42 expect(1) 43 val job = launch(Dispatchers.Unconfined, start = CoroutineStart.LAZY) { 44 expectUnreached() 45 } 46 job.invokeOnCompletion { expect(2) } 47 assertFalse(job.isCompleted) 48 49 parent.cancel() 50 job.join() 51 }.join() 52 finish(3) 53 } 54 55 @Test <lambda>null56 fun testUndispatchedCancellation() = runTest { 57 val parent = Job() 58 launch(parent) { 59 expect(1) 60 parent.cancel() 61 launch(start = CoroutineStart.UNDISPATCHED) { 62 expect(2) 63 yield() 64 expectUnreached() 65 } 66 67 }.join() 68 finish(3) 69 } 70 71 @Test <lambda>null72 fun testCancelledAtomicUnconfined() = runTest { 73 val parent = Job() 74 launch(parent) { 75 expect(1) 76 parent.cancel() 77 launch(Dispatchers.Unconfined, start = CoroutineStart.ATOMIC) { 78 expect(2) 79 yield() 80 expectUnreached() 81 } 82 }.join() 83 finish(3) 84 } 85 86 87 @Test <lambda>null88 fun testCancelledWithContextUnconfined() = runTest { 89 val parent = Job() 90 launch(parent) { 91 expect(1) 92 parent.cancel() 93 withContext(Dispatchers.Unconfined) { 94 expectUnreached() 95 } 96 }.join() 97 finish(2) 98 } 99 }