aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-debug/test/junit4/CoroutinesTimeoutTest.kt
blob: fb170c071c1828e148ab04cdb9166512d29c1fcd (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
/*
 * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines.debug.junit4

import kotlinx.coroutines.*
import org.junit.*
import org.junit.runners.model.*

class CoroutinesTimeoutTest : TestBase() {

    @Rule
    @JvmField
    public val validation = TestFailureValidation(
        1000, false,
        TestResultSpec("throwingTest", error = RuntimeException::class.java),
        TestResultSpec("successfulTest"),
        TestResultSpec(
            "hangingTest", expectedOutParts = listOf(
                "Coroutines dump",
                "Test hangingTest timed out after 1 seconds",
                "BlockingCoroutine{Active}",
                "runBlocking",
                "at kotlinx.coroutines.debug.junit4.CoroutinesTimeoutTest.suspendForever",
                "at kotlinx.coroutines.debug.junit4.CoroutinesTimeoutTest\$hangingTest\$1.invokeSuspend"),
            notExpectedOutParts = listOf("delay", "throwingTest"),
            error = TestTimedOutException::class.java)
    )

    @Test
    fun hangingTest() = runBlocking<Unit> {
        suspendForever()
        expectUnreached()
    }

    private suspend fun suspendForever() {
        delay(Long.MAX_VALUE)
        expectUnreached()
    }

    @Test
    fun throwingTest() = runBlocking<Unit> {
        throw RuntimeException()
    }

    @Test
    fun successfulTest() = runBlocking {
        val job = launch {
            yield()
        }

        job.join()
    }
}