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