blob: a969cef76a02dfc068de3028db0a815adfc4ef71 [file] [log] [blame]
Roman Elizarovf16fd272017-02-07 11:26:00 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarovf16fd272017-02-07 11:26:00 +03003 */
4
Roman Elizarov7b2d8b02017-02-02 20:09:14 +03005package kotlinx.coroutines.experimental.channels
6
Roman Elizarov9fe5f462018-02-21 19:05:52 +03007import kotlinx.coroutines.experimental.*
8import org.hamcrest.core.*
9import org.junit.*
10import org.junit.Assert.*
11import org.junit.runner.*
12import org.junit.runners.*
13import kotlin.coroutines.experimental.*
Roman Elizarov7b2d8b02017-02-02 20:09:14 +030014
15@RunWith(Parameterized::class)
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030016class SimpleSendReceiveJvmTest(
Roman Elizarov7b2d8b02017-02-02 20:09:14 +030017 val kind: TestChannelKind,
Roman Elizarove3aa8ff2017-04-27 19:16:40 +030018 val n: Int,
19 val concurrent: Boolean
Roman Elizarov7b2d8b02017-02-02 20:09:14 +030020) {
21 companion object {
Roman Elizarove3aa8ff2017-04-27 19:16:40 +030022 @Parameterized.Parameters(name = "{0}, n={1}, concurrent={2}")
Roman Elizarov7b2d8b02017-02-02 20:09:14 +030023 @JvmStatic
24 fun params(): Collection<Array<Any>> = TestChannelKind.values().flatMap { kind ->
Roman Elizarove3aa8ff2017-04-27 19:16:40 +030025 listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1000).flatMap { n ->
26 listOf(false, true).map { concurrent ->
27 arrayOf<Any>(kind, n, concurrent)
28 }
Roman Elizarov7b2d8b02017-02-02 20:09:14 +030029 }
30 }
31 }
32
33 val channel = kind.create()
34
35 @Test
36 fun testSimpleSendReceive() = runBlocking {
Roman Elizarov43e3af72017-07-21 16:01:31 +030037 val ctx = if (concurrent) CommonPool else coroutineContext
Roman Elizarove3aa8ff2017-04-27 19:16:40 +030038 launch(ctx) {
Roman Elizarov7b2d8b02017-02-02 20:09:14 +030039 repeat(n) { channel.send(it) }
40 channel.close()
41 }
Roman Elizarov3aed4ee2017-03-06 12:21:05 +030042 var expected = 0
Roman Elizarov7b2d8b02017-02-02 20:09:14 +030043 for (x in channel) {
Roman Elizarove3aa8ff2017-04-27 19:16:40 +030044 if (!kind.isConflated) {
Roman Elizarov3aed4ee2017-03-06 12:21:05 +030045 assertThat(x, IsEqual(expected++))
46 } else {
47 assertTrue(x >= expected)
48 expected = x + 1
49 }
Roman Elizarov7b2d8b02017-02-02 20:09:14 +030050 }
Roman Elizarov9ff32642017-06-06 14:36:38 +030051 if (!kind.isConflated) {
Roman Elizarov3aed4ee2017-03-06 12:21:05 +030052 assertThat(expected, IsEqual(n))
53 }
Roman Elizarov7b2d8b02017-02-02 20:09:14 +030054 }
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030055}