• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
4  */
5 
6 @file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED", "DEPRECATION") // KT-21913
7 
8 package kotlinx.coroutines
9 
10 import kotlin.test.*
11 
12 class DelayTest : TestBase() {
13 
14     @Test
testCancellationnull15     fun testCancellation() = runTest(expected = {it is CancellationException }) {
16         runAndCancel(1000)
17     }
18 
19     @Test
<lambda>null20     fun testMaxLongValue()= runTest(expected = {it is CancellationException }) {
21         runAndCancel(Long.MAX_VALUE)
22     }
23 
24     @Test
<lambda>null25     fun testMaxIntValue()= runTest(expected = {it is CancellationException }) {
26         runAndCancel(Int.MAX_VALUE.toLong())
27     }
28 
29     @Test
<lambda>null30     fun testRegularDelay() = runTest {
31         val deferred = async {
32             expect(2)
33             delay(1)
34             expect(3)
35         }
36 
37         expect(1)
38         yield()
39         deferred.await()
40         finish(4)
41     }
42 
<lambda>null43     private suspend fun runAndCancel(time: Long) = coroutineScope {
44         expect(1)
45         val deferred = async {
46             expect(2)
47             delay(time)
48             expectUnreached()
49         }
50 
51         yield()
52         expect(3)
53         require(deferred.isActive)
54         deferred.cancel()
55         finish(4)
56         deferred.await()
57     }
58 }
59