Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental.channels |
| 6 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.* |
| 8 | import org.hamcrest.core.* |
| 9 | import org.junit.* |
| 10 | import org.junit.Assert.* |
| 11 | import org.junit.runner.* |
| 12 | import org.junit.runners.* |
| 13 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 14 | |
| 15 | @RunWith(Parameterized::class) |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 16 | class SimpleSendReceiveJvmTest( |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 17 | val kind: TestChannelKind, |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 18 | val n: Int, |
| 19 | val concurrent: Boolean |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 20 | ) { |
| 21 | companion object { |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 22 | @Parameterized.Parameters(name = "{0}, n={1}, concurrent={2}") |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 23 | @JvmStatic |
| 24 | fun params(): Collection<Array<Any>> = TestChannelKind.values().flatMap { kind -> |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 25 | 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 Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | val channel = kind.create() |
| 34 | |
| 35 | @Test |
| 36 | fun testSimpleSendReceive() = runBlocking { |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 37 | val ctx = if (concurrent) CommonPool else coroutineContext |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 38 | launch(ctx) { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 39 | repeat(n) { channel.send(it) } |
| 40 | channel.close() |
| 41 | } |
Roman Elizarov | 3aed4ee | 2017-03-06 12:21:05 +0300 | [diff] [blame] | 42 | var expected = 0 |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 43 | for (x in channel) { |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 44 | if (!kind.isConflated) { |
Roman Elizarov | 3aed4ee | 2017-03-06 12:21:05 +0300 | [diff] [blame] | 45 | assertThat(x, IsEqual(expected++)) |
| 46 | } else { |
| 47 | assertTrue(x >= expected) |
| 48 | expected = x + 1 |
| 49 | } |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 50 | } |
Roman Elizarov | 9ff3264 | 2017-06-06 14:36:38 +0300 | [diff] [blame] | 51 | if (!kind.isConflated) { |
Roman Elizarov | 3aed4ee | 2017-03-06 12:21:05 +0300 | [diff] [blame] | 52 | assertThat(expected, IsEqual(n)) |
| 53 | } |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 54 | } |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 55 | } |