• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package kotlinx.coroutines
2 
3 import kotlinx.coroutines.testing.*
4 import kotlin.test.*
5 
6 class LaunchLazyTest : TestBase() {
7     @Test
<lambda>null8     fun testLaunchAndYieldJoin() = runTest {
9         expect(1)
10         val job = launch(start = CoroutineStart.LAZY) {
11             expect(4)
12             yield() // does nothing -- main waits
13             expect(5)
14         }
15         expect(2)
16         yield() // does nothing, was not started yet
17         expect(3)
18         assertTrue(!job.isActive && !job.isCompleted)
19         job.join()
20         assertTrue(!job.isActive && job.isCompleted)
21         finish(6)
22     }
23 
24     @Test
<lambda>null25     fun testStart() = runTest {
26         expect(1)
27         val job = launch(start = CoroutineStart.LAZY) {
28             expect(5)
29             yield() // yields back to main
30             expect(7)
31         }
32         expect(2)
33         yield() // does nothing, was not started yet
34         expect(3)
35         assertTrue(!job.isActive && !job.isCompleted)
36         assertTrue(job.start())
37         assertTrue(job.isActive && !job.isCompleted)
38         assertTrue(!job.start()) // start again -- does nothing
39         assertTrue(job.isActive && !job.isCompleted)
40         expect(4)
41         yield() // now yield to started coroutine
42         expect(6)
43         assertTrue(job.isActive && !job.isCompleted)
44         yield() // yield again
45         assertTrue(!job.isActive && job.isCompleted) // it completes this time
46         expect(8)
47         job.join() // immediately returns
48         finish(9)
49     }
50 
51     @Test
<lambda>null52     fun testInvokeOnCompletionAndStart() = runTest {
53         expect(1)
54         val job = launch(start = CoroutineStart.LAZY) {
55             expect(5)
56         }
57         yield() // no started yet!
58         expect(2)
59         job.invokeOnCompletion {
60             expect(6)
61         }
62         expect(3)
63         job.start()
64         expect(4)
65         yield()
66         finish(7)
67     }
68 }
69