• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 UnconfinedTest : TestBase() {
10 
11     @Test
<lambda>null12     fun testOrder() = runTest {
13         expect(1)
14         launch(Dispatchers.Unconfined) {
15             expect(2)
16             launch {
17                 expect(4)
18                 launch {
19                     expect(6)
20                 }
21 
22                 launch {
23                     expect(7)
24                 }
25                 expect(5)
26             }
27 
28             expect(3)
29         }
30 
31         finish(8)
32     }
33 
34     @Test
<lambda>null35     fun testBlockThrows() = runTest {
36         expect(1)
37         try {
38             withContext(Dispatchers.Unconfined) {
39                 expect(2)
40                 withContext(Dispatchers.Unconfined + CoroutineName("a")) {
41                     expect(3)
42                 }
43 
44                 expect(4)
45                 launch(start = CoroutineStart.ATOMIC) {
46                     expect(5)
47                 }
48 
49                 throw TestException()
50             }
51         } catch (e: TestException) {
52             finish(6)
53         }
54     }
55 
56     @Test
<lambda>null57     fun testEnterMultipleTimes() = runTest {
58         launch(Unconfined) {
59             expect(1)
60         }
61 
62         launch(Unconfined) {
63             expect(2)
64         }
65 
66         launch(Unconfined) {
67             expect(3)
68         }
69 
70         finish(4)
71     }
72 
73     @Test
<lambda>null74     fun testYield() = runTest {
75         expect(1)
76         launch(Dispatchers.Unconfined) {
77             expect(2)
78             yield()
79             launch {
80                 expect(4)
81             }
82             expect(3)
83             yield()
84             expect(5)
85         }.join()
86 
87         finish(6)
88     }
89 
90     @Test
<lambda>null91     fun testCancellationWihYields() = runTest {
92         expect(1)
93         GlobalScope.launch(Dispatchers.Unconfined) {
94             val job = coroutineContext[Job]!!
95             expect(2)
96             yield()
97             GlobalScope.launch(Dispatchers.Unconfined) {
98                 expect(4)
99                 job.cancel()
100                 expect(5)
101             }
102             expect(3)
103 
104             try {
105                 yield()
106             } finally {
107                 expect(6)
108             }
109         }
110 
111         finish(7)
112     }
113 }
114