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 10 class AsyncJvmTest : TestBase() { 11 // We have the same test in common module, but the maintainer uses this particular file 12 // and semi-automatically types cmd+N + AsyncJvm in order to duck-tape any JVM samples/repros, 13 // please do not remove this test 14 15 @Test <lambda>null16 fun testAsyncWithFinally() = runTest { 17 expect(1) 18 19 @Suppress("UNREACHABLE_CODE") 20 val d = async { 21 expect(3) 22 try { 23 yield() // to main, will cancel 24 } finally { 25 expect(6) // will go there on await 26 return@async "Fail" // result will not override cancellation 27 } 28 expectUnreached() 29 "Fail2" 30 } 31 expect(2) 32 yield() // to async 33 expect(4) 34 check(d.isActive && !d.isCompleted && !d.isCancelled) 35 d.cancel() 36 check(!d.isActive && !d.isCompleted && d.isCancelled) 37 check(!d.isActive && !d.isCompleted && d.isCancelled) 38 expect(5) 39 try { 40 d.await() // awaits 41 expectUnreached() // does not complete normally 42 } catch (e: Throwable) { 43 expect(7) 44 check(e is CancellationException) 45 } 46 check(!d.isActive && d.isCompleted && d.isCancelled) 47 finish(8) 48 } 49 } 50