aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/common/test/DelayTest.kt
blob: 1b7314628508090dbad46daaabb52648416543d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

/*
 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED", "DEPRECATION") // KT-21913

package kotlinx.coroutines

import kotlin.test.*

class DelayTest : TestBase() {

    @Test
    fun testCancellation() = runTest(expected = {it is CancellationException }) {
        runAndCancel(1000)
    }

    @Test
    fun testMaxLongValue()= runTest(expected = {it is CancellationException }) {
        runAndCancel(Long.MAX_VALUE)
    }

    @Test
    fun testMaxIntValue()= runTest(expected = {it is CancellationException }) {
        runAndCancel(Int.MAX_VALUE.toLong())
    }

    @Test
    fun testRegularDelay() = runTest {
        val deferred = async {
            expect(2)
            delay(1)
            expect(3)
        }

        expect(1)
        yield()
        deferred.await()
        finish(4)
    }

    private suspend fun runAndCancel(time: Long) = coroutineScope {
        expect(1)
        val deferred = async {
            expect(2)
            delay(time)
            expectUnreached()
        }

        yield()
        expect(3)
        require(deferred.isActive)
        deferred.cancel()
        finish(4)
        deferred.await()
    }
}