aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/jvm/test/channels/ActorTest.kt
blob: bdca5039d22938a8962fde977a22fa69ebb9eed9 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines.channels

import kotlinx.coroutines.*
import org.junit.Test
import org.junit.runner.*
import org.junit.runners.*
import java.io.*
import kotlin.test.*

@RunWith(Parameterized::class)
class ActorTest(private val capacity: Int) : TestBase() {

    companion object {
        @Parameterized.Parameters(name = "Capacity: {0}")
        @JvmStatic
        fun params(): Collection<Array<Any>> = listOf(0, 1, Channel.UNLIMITED, Channel.CONFLATED).map { arrayOf<Any>(it) }
    }

    @Test
    fun testEmpty() = runBlocking {
        expect(1)
        val actor = actor<String>(capacity = capacity) {
            expect(3)
        }
        actor as Job // type assertion
        assertTrue(actor.isActive)
        assertFalse(actor.isCompleted)
        assertFalse(actor.isClosedForSend)
        expect(2)
        yield() // to actor code
        assertFalse(actor.isActive)
        assertTrue(actor.isCompleted)
        assertTrue(actor.isClosedForSend)
        finish(4)
    }

    @Test
    fun testOne() = runBlocking {
        expect(1)
        val actor = actor<String>(capacity = capacity) {
            expect(3)
            assertEquals("OK", receive())
            expect(6)
        }
        actor as Job // type assertion
        assertTrue(actor.isActive)
        assertFalse(actor.isCompleted)
        assertFalse(actor.isClosedForSend)
        expect(2)
        yield() // to actor code
        assertTrue(actor.isActive)
        assertFalse(actor.isCompleted)
        assertFalse(actor.isClosedForSend)
        expect(4)
        // send message to actor
        actor.send("OK")
        expect(5)
        yield() // to actor code
        assertFalse(actor.isActive)
        assertTrue(actor.isCompleted)
        assertTrue(actor.isClosedForSend)
        finish(7)
    }

    @Test
    fun testCloseWithoutCause() = runTest {
        val actor = actor<Int>(capacity = capacity) {
            val element = channel.receiveOrNull()
            expect(2)
            assertEquals(42, element)
            val next = channel.receiveOrNull()
            assertNull(next)
            expect(3)
        }

        expect(1)
        actor.send(42)
        yield()
        actor.close()
        yield()
        finish(4)
    }

    @Test
    fun testCloseWithCause() = runTest {
        val actor = actor<Int>(capacity = capacity) {
            val element = channel.receiveOrNull()
            expect(2)
            require(element!! == 42)
            try {
                channel.receiveOrNull()
            } catch (e: IOException) {
                expect(3)
            }
        }

        expect(1)
        actor.send(42)
        yield()
        actor.close(IOException())
        yield()
        finish(4)
    }

    @Test
    fun testCancelEnclosingJob() = runTest {
        val job = async {
            actor<Int>(capacity = capacity) {
                expect(1)
                channel.receiveOrNull()
                expectUnreached()
            }
        }

        yield()
        yield()

        expect(2)
        yield()
        job.cancel()

        try {
            job.await()
            expectUnreached()
        } catch (e: CancellationException) {
            assertTrue(e.message?.contains("DeferredCoroutine was cancelled") ?: false)
        }

        finish(3)
    }

    @Test
    fun testThrowingActor() = runTest(unhandled = listOf({e -> e is IllegalArgumentException})) {
        val parent = Job()
        val actor = actor<Int>(parent) {
            channel.consumeEach {
                expect(1)
                throw IllegalArgumentException()
            }
        }

        actor.send(1)
        parent.cancel()
        parent.join()
        finish(2)
    }

    @Test
    fun testChildJob() = runTest {
        val parent = Job()
        actor<Int>(parent) {
            launch {
                try {
                    delay(Long.MAX_VALUE)
                } finally {
                    expect(1)
                }
            }
        }

        yield()
        yield()
        parent.cancel()
        parent.join()
        finish(2)
    }

    @Test
    fun testCloseFreshActor() = runTest {
        for (start in CoroutineStart.values()) {
            val job = launch {
                val actor = actor<Int>(start = start) { for (i in channel) {} }
                actor.close()
            }

            job.join()
        }
    }
}