1 package kotlinx.coroutines 2 3 import kotlinx.coroutines.testing.* 4 import kotlin.test.* 5 6 class UnconfinedTest : TestBase() { 7 8 @Test <lambda>null9 fun testOrder() = runTest { 10 expect(1) 11 launch(Dispatchers.Unconfined) { 12 expect(2) 13 launch { 14 expect(4) 15 launch { 16 expect(6) 17 } 18 19 launch { 20 expect(7) 21 } 22 expect(5) 23 } 24 25 expect(3) 26 } 27 28 finish(8) 29 } 30 31 @Test <lambda>null32 fun testBlockThrows() = runTest { 33 expect(1) 34 try { 35 withContext(Dispatchers.Unconfined) { 36 expect(2) 37 withContext(Dispatchers.Unconfined + CoroutineName("a")) { 38 expect(3) 39 } 40 41 expect(4) 42 launch(start = CoroutineStart.ATOMIC) { 43 expect(5) 44 } 45 46 throw TestException() 47 } 48 } catch (e: TestException) { 49 finish(6) 50 } 51 } 52 53 @Test <lambda>null54 fun testEnterMultipleTimes() = runTest { 55 launch(Unconfined) { 56 expect(1) 57 } 58 59 launch(Unconfined) { 60 expect(2) 61 } 62 63 launch(Unconfined) { 64 expect(3) 65 } 66 67 finish(4) 68 } 69 70 @Test <lambda>null71 fun testYield() = runTest { 72 expect(1) 73 launch(Dispatchers.Unconfined) { 74 expect(2) 75 yield() 76 launch { 77 expect(4) 78 } 79 expect(3) 80 yield() 81 expect(5) 82 }.join() 83 84 finish(6) 85 } 86 87 @Test <lambda>null88 fun testCancellationWihYields() = runTest { 89 expect(1) 90 GlobalScope.launch(Dispatchers.Unconfined) { 91 val job = coroutineContext[Job]!! 92 expect(2) 93 yield() 94 GlobalScope.launch(Dispatchers.Unconfined) { 95 expect(4) 96 job.cancel() 97 expect(5) 98 } 99 expect(3) 100 101 try { 102 yield() 103 } finally { 104 expect(6) 105 } 106 } 107 108 finish(7) 109 } 110 } 111