1 /* 2 * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 @file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED", "DEPRECATION") 6 7 // KT-21913 8 9 package kotlinx.coroutines 10 11 import kotlin.test.* 12 import kotlin.time.* 13 14 @ExperimentalTime 15 class DelayDurationTest : TestBase() { 16 17 @Test testCancellationnull18 fun testCancellation() = runTest(expected = { it is CancellationException }) { 19 runAndCancel(1.seconds) 20 } 21 22 @Test testInfinitenull23 fun testInfinite() = runTest(expected = { it is CancellationException }) { 24 runAndCancel(Duration.INFINITE) 25 } 26 27 @Test <lambda>null28 fun testRegularDelay() = runTest { 29 val deferred = async { 30 expect(2) 31 delay(1.seconds) 32 expect(4) 33 } 34 35 expect(1) 36 yield() 37 expect(3) 38 deferred.await() 39 finish(5) 40 } 41 42 @Test <lambda>null43 fun testNanoDelay() = runTest { 44 val deferred = async { 45 expect(2) 46 delay(1.nanoseconds) 47 expect(4) 48 } 49 50 expect(1) 51 yield() 52 expect(3) 53 deferred.await() 54 finish(5) 55 } 56 <lambda>null57 private suspend fun runAndCancel(time: Duration) = coroutineScope { 58 expect(1) 59 val deferred = async { 60 expect(2) 61 delay(time) 62 expectUnreached() 63 } 64 65 yield() 66 expect(3) 67 require(deferred.isActive) 68 deferred.cancel() 69 finish(4) 70 deferred.await() 71 } 72 } 73