• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 @file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913
6 
7 package kotlinx.coroutines
8 
9 import kotlin.test.*
10 
11 /**
12  * Tests that the transitions to the state of the [Job] correspond to documentation in the
13  * table that is presented in the [Job] documentation.
14  */
15 class JobStatesTest : TestBase() {
16     @Test
17     public fun testNormalCompletion() = runTest {
18         expect(1)
19         val parent = coroutineContext.job
20         val job = launch(start = CoroutineStart.LAZY) {
21             expect(2)
22             // launches child
23             launch {
24                 expect(4)
25             }
26             // completes normally
27         }
28         // New job
29         assertFalse(job.isActive)
30         assertFalse(job.isCompleted)
31         assertFalse(job.isCancelled)
32         assertSame(parent, job.parent)
33         // New -> Active
34         job.start()
35         assertTrue(job.isActive)
36         assertFalse(job.isCompleted)
37         assertFalse(job.isCancelled)
38         assertSame(parent, job.parent)
39         // Active -> Completing
40         yield() // scheduled & starts child
41         expect(3)
42         assertTrue(job.isActive)
43         assertFalse(job.isCompleted)
44         assertFalse(job.isCancelled)
45         assertSame(parent, job.parent)
46         // Completing -> Completed
47         yield()
48         finish(5)
49         assertFalse(job.isActive)
50         assertTrue(job.isCompleted)
51         assertFalse(job.isCancelled)
52         assertNull(job.parent)
53     }
54 
55     @Test
56     public fun testCompletingFailed() = runTest(
57         unhandled = listOf({ it -> it is TestException })
58     ) {
59         expect(1)
60         val job = launch(NonCancellable, start = CoroutineStart.LAZY) {
61             expect(2)
62             // launches child
63             launch {
64                 expect(4)
65                 throw TestException()
66             }
67             // completes normally
68         }
69         // New job
70         assertFalse(job.isActive)
71         assertFalse(job.isCompleted)
72         assertFalse(job.isCancelled)
73         // New -> Active
74         job.start()
75         assertTrue(job.isActive)
76         assertFalse(job.isCompleted)
77         assertFalse(job.isCancelled)
78         // Active -> Completing
79         yield() // scheduled & starts child
80         expect(3)
81         assertTrue(job.isActive)
82         assertFalse(job.isCompleted)
83         assertFalse(job.isCancelled)
84         // Completing -> Cancelled
85         yield()
86         finish(5)
87         assertFalse(job.isActive)
88         assertTrue(job.isCompleted)
89         assertTrue(job.isCancelled)
90     }
91 
92     @Test
93     public fun testFailed() = runTest(
94         unhandled = listOf({ it -> it is TestException })
95     ) {
96         expect(1)
97         val job = launch(NonCancellable, start = CoroutineStart.LAZY) {
98             expect(2)
99             // launches child
100             launch(start = CoroutineStart.ATOMIC) {
101                 expect(4)
102             }
103             // failing
104             throw TestException()
105         }
106         // New job
107         assertFalse(job.isActive)
108         assertFalse(job.isCompleted)
109         assertFalse(job.isCancelled)
110         // New -> Active
111         job.start()
112         assertTrue(job.isActive)
113         assertFalse(job.isCompleted)
114         assertFalse(job.isCancelled)
115         // Active -> Cancelling
116         yield() // scheduled & starts child
117         expect(3)
118         assertFalse(job.isActive)
119         assertFalse(job.isCompleted)
120         assertTrue(job.isCancelled)
121         // Cancelling -> Cancelled
122         yield()
123         finish(5)
124         assertFalse(job.isActive)
125         assertTrue(job.isCompleted)
126         assertTrue(job.isCancelled)
127     }
128 
129     @Test
130     public fun testCancelling() = runTest {
131         expect(1)
132         val job = launch(NonCancellable, start = CoroutineStart.LAZY) {
133             expect(2)
134             // launches child
135             launch(start = CoroutineStart.ATOMIC) {
136                 expect(4)
137             }
138             // completes normally
139         }
140         // New job
141         assertFalse(job.isActive)
142         assertFalse(job.isCompleted)
143         assertFalse(job.isCancelled)
144         // New -> Active
145         job.start()
146         assertTrue(job.isActive)
147         assertFalse(job.isCompleted)
148         assertFalse(job.isCancelled)
149         // Active -> Completing
150         yield() // scheduled & starts child
151         expect(3)
152         assertTrue(job.isActive)
153         assertFalse(job.isCompleted)
154         assertFalse(job.isCancelled)
155         // Completing -> Cancelling
156         job.cancel()
157         assertFalse(job.isActive)
158         assertFalse(job.isCompleted)
159         assertTrue(job.isCancelled)
160         // Cancelling -> Cancelled
161         yield()
162         finish(5)
163         assertFalse(job.isActive)
164         assertTrue(job.isCompleted)
165         assertTrue(job.isCancelled)
166     }
167 }
168