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 LaunchLazyTest : TestBase() { 10 @Test <lambda>null11 fun testLaunchAndYieldJoin() = runTest { 12 expect(1) 13 val job = launch(start = CoroutineStart.LAZY) { 14 expect(4) 15 yield() // does nothing -- main waits 16 expect(5) 17 } 18 expect(2) 19 yield() // does nothing, was not started yet 20 expect(3) 21 assertTrue(!job.isActive && !job.isCompleted) 22 job.join() 23 assertTrue(!job.isActive && job.isCompleted) 24 finish(6) 25 } 26 27 @Test <lambda>null28 fun testStart() = runTest { 29 expect(1) 30 val job = launch(start = CoroutineStart.LAZY) { 31 expect(5) 32 yield() // yields back to main 33 expect(7) 34 } 35 expect(2) 36 yield() // does nothing, was not started yet 37 expect(3) 38 assertTrue(!job.isActive && !job.isCompleted) 39 assertTrue(job.start()) 40 assertTrue(job.isActive && !job.isCompleted) 41 assertTrue(!job.start()) // start again -- does nothing 42 assertTrue(job.isActive && !job.isCompleted) 43 expect(4) 44 yield() // now yield to started coroutine 45 expect(6) 46 assertTrue(job.isActive && !job.isCompleted) 47 yield() // yield again 48 assertTrue(!job.isActive && job.isCompleted) // it completes this time 49 expect(8) 50 job.join() // immediately returns 51 finish(9) 52 } 53 54 @Test <lambda>null55 fun testInvokeOnCompletionAndStart() = runTest { 56 expect(1) 57 val job = launch(start = CoroutineStart.LAZY) { 58 expect(5) 59 } 60 yield() // no started yet! 61 expect(2) 62 job.invokeOnCompletion { 63 expect(6) 64 } 65 expect(3) 66 job.start() 67 expect(4) 68 yield() 69 finish(7) 70 } 71 } 72