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 import kotlin.time.Duration.Companion.seconds 14 import kotlin.time.Duration.Companion.nanoseconds 15 16 class DelayDurationTest : TestBase() { 17 18 @Test testCancellationnull19 fun testCancellation() = runTest(expected = { it is CancellationException }) { 20 runAndCancel(1.seconds) 21 } 22 23 @Test testInfinitenull24 fun testInfinite() = runTest(expected = { it is CancellationException }) { 25 runAndCancel(Duration.INFINITE) 26 } 27 28 @Test <lambda>null29 fun testRegularDelay() = runTest { 30 val deferred = async { 31 expect(2) 32 delay(1.seconds) 33 expect(4) 34 } 35 36 expect(1) 37 yield() 38 expect(3) 39 deferred.await() 40 finish(5) 41 } 42 43 @Test <lambda>null44 fun testNanoDelay() = runTest { 45 val deferred = async { 46 expect(2) 47 delay(1.nanoseconds) 48 expect(4) 49 } 50 51 expect(1) 52 yield() 53 expect(3) 54 deferred.await() 55 finish(5) 56 } 57 <lambda>null58 private suspend fun runAndCancel(time: Duration) = coroutineScope { 59 expect(1) 60 val deferred = async { 61 expect(2) 62 delay(time) 63 expectUnreached() 64 } 65 66 yield() 67 expect(3) 68 require(deferred.isActive) 69 deferred.cancel() 70 finish(4) 71 deferred.await() 72 } 73 } 74