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 AsyncJvmTest : TestBase() {
10     // This must be a common test but it fails on JS because of KT-21961
11     @Test
12     fun testAsyncWithFinally() = runTest {
13         expect(1)
14 
15         @Suppress("UNREACHABLE_CODE")
16         val d = async {
17             expect(3)
18             try {
19                 yield() // to main, will cancel
20             } finally {
21                 expect(6) // will go there on await
22                 return@async "Fail" // result will not override cancellation
23             }
24             expectUnreached()
25             "Fail2"
26         }
27         expect(2)
28         yield() // to async
29         expect(4)
30         check(d.isActive && !d.isCompleted && !d.isCancelled)
31         d.cancel()
32         check(!d.isActive && !d.isCompleted && d.isCancelled)
33         check(!d.isActive && !d.isCompleted && d.isCancelled)
34         expect(5)
35         try {
36             d.await() // awaits
37             expectUnreached() // does not complete normally
38         } catch (e: Throwable) {
39             expect(7)
40             check(e is CancellationException)
41         }
42         check(!d.isActive && d.isCompleted && d.isCancelled)
43         finish(8)
44     }
45 }
46