• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 @file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED", "DEPRECATION")
2 
3 // KT-21913
4 
5 package kotlinx.coroutines
6 
7 import kotlinx.coroutines.testing.*
8 import kotlin.test.*
9 import kotlin.time.*
10 import kotlin.time.Duration.Companion.seconds
11 import kotlin.time.Duration.Companion.nanoseconds
12 
13 class DelayDurationTest : TestBase() {
14 
15     @Test
testCancellationnull16     fun testCancellation() = runTest(expected = { it is CancellationException }) {
17         runAndCancel(1.seconds)
18     }
19 
20     @Test
testInfinitenull21     fun testInfinite() = runTest(expected = { it is CancellationException }) {
22         runAndCancel(Duration.INFINITE)
23     }
24 
25     @Test
<lambda>null26     fun testRegularDelay() = runTest {
27         val deferred = async {
28             expect(2)
29             delay(1.seconds)
30             expect(4)
31         }
32 
33         expect(1)
34         yield()
35         expect(3)
36         deferred.await()
37         finish(5)
38     }
39 
40     @Test
<lambda>null41     fun testNanoDelay() = runTest {
42         val deferred = async {
43             expect(2)
44             delay(1.nanoseconds)
45             expect(4)
46         }
47 
48         expect(1)
49         yield()
50         expect(3)
51         deferred.await()
52         finish(5)
53     }
54 
<lambda>null55     private suspend fun runAndCancel(time: Duration) = coroutineScope {
56         expect(1)
57         val deferred = async {
58             expect(2)
59             delay(time)
60             expectUnreached()
61         }
62 
63         yield()
64         expect(3)
65         require(deferred.isActive)
66         deferred.cancel()
67         finish(4)
68         deferred.await()
69     }
70 }
71