• 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 package kotlinx.coroutines
6 
7 import kotlin.coroutines.*
8 import kotlin.test.*
9 
10 class UndispatchedResultTest : TestBase() {
11 
12     @Test
13     fun testWithContext() = runTest {
14         invokeTest { block -> withContext(wrapperDispatcher(coroutineContext), block) }
15     }
16 
17     @Test
18     fun testWithContextFastPath() = runTest {
19         invokeTest { block -> withContext(coroutineContext, block) }
20     }
21 
22     @Test
23     fun testWithTimeout() = runTest {
24         invokeTest { block -> withTimeout(Long.MAX_VALUE, block) }
25     }
26 
27     @Test
28     fun testAsync() = runTest {
29         invokeTest { block -> async(NonCancellable, block = block).await() }
30     }
31 
32     @Test
33     fun testCoroutineScope() = runTest {
34         invokeTest { block -> coroutineScope(block) }
35     }
36 
37     private suspend fun invokeTest(scopeProvider: suspend (suspend CoroutineScope.() -> Unit) -> Unit) {
38         invokeTest(EmptyCoroutineContext, scopeProvider)
39         invokeTest(Unconfined, scopeProvider)
40     }
41 
42     private suspend fun invokeTest(
43         context: CoroutineContext,
44         scopeProvider: suspend (suspend CoroutineScope.() -> Unit) -> Unit
45     ) {
46         try {
47             scopeProvider { block(context) }
48         } catch (e: TestException) {
49             finish(5)
50             reset()
51         }
52     }
53 
54     private suspend fun CoroutineScope.block(context: CoroutineContext) {
55         try {
56             expect(1)
57             // Will cancel its parent
58             async<Unit>(context) {
59                 expect(2)
60                 throw TestException()
61             }.await()
62         } catch (e: TestException) {
63             expect(3)
64         }
65         expect(4)
66     }
67 }
68