aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/jvm/test/guide/test/DispatcherGuideTest.kt
blob: 1a84fb942736bdebcc0c7a8e883dc0dea8e3a979 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

// This file was automatically generated from coroutine-context-and-dispatchers.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.test

import kotlinx.coroutines.knit.*
import org.junit.Test

class DispatcherGuideTest {
    @Test
    fun testExampleContext01() {
        test("ExampleContext01") { kotlinx.coroutines.guide.exampleContext01.main() }.verifyLinesStartUnordered(
            "Unconfined            : I'm working in thread main",
            "Default               : I'm working in thread DefaultDispatcher-worker-1",
            "newSingleThreadContext: I'm working in thread MyOwnThread",
            "main runBlocking      : I'm working in thread main"
        )
    }

    @Test
    fun testExampleContext02() {
        test("ExampleContext02") { kotlinx.coroutines.guide.exampleContext02.main() }.verifyLinesStart(
            "Unconfined      : I'm working in thread main",
            "main runBlocking: I'm working in thread main",
            "Unconfined      : After delay in thread kotlinx.coroutines.DefaultExecutor",
            "main runBlocking: After delay in thread main"
        )
    }

    @Test
    fun testExampleContext03() {
        test("ExampleContext03") { kotlinx.coroutines.guide.exampleContext03.main() }.verifyLinesFlexibleThread(
            "[main @coroutine#2] I'm computing a piece of the answer",
            "[main @coroutine#3] I'm computing another piece of the answer",
            "[main @coroutine#1] The answer is 42"
        )
    }

    @Test
    fun testExampleContext04() {
        test("ExampleContext04") { kotlinx.coroutines.guide.exampleContext04.main() }.verifyLines(
            "[Ctx1 @coroutine#1] Started in ctx1",
            "[Ctx2 @coroutine#1] Working in ctx2",
            "[Ctx1 @coroutine#1] Back to ctx1"
        )
    }

    @Test
    fun testExampleContext05() {
        test("ExampleContext05") { kotlinx.coroutines.guide.exampleContext05.main() }.also { lines ->
            check(lines.size == 1 && lines[0].startsWith("My job is \"coroutine#1\":BlockingCoroutine{Active}@"))
        }
    }

    @Test
    fun testExampleContext06() {
        test("ExampleContext06") { kotlinx.coroutines.guide.exampleContext06.main() }.verifyLines(
            "job1: I run in my own Job and execute independently!",
            "job2: I am a child of the request coroutine",
            "job1: I am not affected by cancellation of the request",
            "main: Who has survived request cancellation?"
        )
    }

    @Test
    fun testExampleContext07() {
        test("ExampleContext07") { kotlinx.coroutines.guide.exampleContext07.main() }.verifyLines(
            "request: I'm done and I don't explicitly join my children that are still active",
            "Coroutine 0 is done",
            "Coroutine 1 is done",
            "Coroutine 2 is done",
            "Now processing of the request is complete"
        )
    }

    @Test
    fun testExampleContext08() {
        test("ExampleContext08") { kotlinx.coroutines.guide.exampleContext08.main() }.verifyLinesFlexibleThread(
            "[main @main#1] Started main coroutine",
            "[main @v1coroutine#2] Computing v1",
            "[main @v2coroutine#3] Computing v2",
            "[main @main#1] The answer for v1 / v2 = 42"
        )
    }

    @Test
    fun testExampleContext09() {
        test("ExampleContext09") { kotlinx.coroutines.guide.exampleContext09.main() }.verifyLinesFlexibleThread(
            "I'm working in thread DefaultDispatcher-worker-1 @test#2"
        )
    }

    @Test
    fun testExampleContext10() {
        test("ExampleContext10") { kotlinx.coroutines.guide.exampleContext10.main() }.verifyLines(
            "Launched coroutines",
            "Coroutine 0 is done",
            "Coroutine 1 is done",
            "Destroying activity!"
        )
    }

    @Test
    fun testExampleContext11() {
        test("ExampleContext11") { kotlinx.coroutines.guide.exampleContext11.main() }.verifyLinesFlexibleThread(
            "Pre-main, current thread: Thread[main @coroutine#1,5,main], thread local value: 'main'",
            "Launch start, current thread: Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main], thread local value: 'launch'",
            "After yield, current thread: Thread[DefaultDispatcher-worker-2 @coroutine#2,5,main], thread local value: 'launch'",
            "Post-main, current thread: Thread[main @coroutine#1,5,main], thread local value: 'main'"
        )
    }
}