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

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

package kotlinx.coroutines.selects

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlin.test.*

class SelectLoopTest : TestBase() {
    // https://github.com/Kotlin/kotlinx.coroutines/issues/1130
    @Test
    fun testChannelSelectLoop() = runTest(
        expected = { it is TestException }
    ) {
        expect(1)
        val channel = Channel<Unit>()
        val job = launch {
            expect(2)
            channel.send(Unit)
            expect(3)
            throw TestException()
        }
        try {
            while (true) {
                select<Unit> {
                    channel.onReceiveOrNull {
                        expectUnreached()
                    }
                    job.onJoin {
                        expectUnreached()
                    }
                }
            }
        } catch (e: CancellationException) {
            // select will get cancelled because of the failure of job
            finish(4)
        }
    }
}