Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | */ |
| 4 | |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental.channels |
| 6 | |
| 7 | import kotlinx.coroutines.experimental.* |
| 8 | import org.junit.runner.* |
| 9 | import org.junit.runners.* |
| 10 | import kotlin.coroutines.experimental.* |
| 11 | import kotlin.test.* |
| 12 | |
| 13 | @RunWith(Parameterized::class) |
| 14 | class SendReceiveJvmStressTest(private val channel: Channel<Int>) : TestBase() { |
| 15 | |
| 16 | companion object { |
| 17 | @Parameterized.Parameters(name = "{0}") |
| 18 | @JvmStatic |
| 19 | fun params(): Collection<Array<Any>> = listOf( |
| 20 | ArrayChannel<Int>(1), |
| 21 | ArrayChannel<Int>(10), |
| 22 | ArrayChannel<Int>(1_000_000), |
| 23 | LinkedListChannel<Int>(), |
| 24 | RendezvousChannel<Int>() |
| 25 | ).map { arrayOf<Any>(it) } |
| 26 | } |
| 27 | |
| 28 | @Test |
| 29 | fun testStress() = runTest { |
| 30 | val n = 100_000 * stressTestMultiplier |
| 31 | val sender = launch(coroutineContext) { |
| 32 | for (i in 1..n) { |
| 33 | channel.send(i) |
| 34 | } |
| 35 | expect(2) |
| 36 | } |
| 37 | val receiver = launch(coroutineContext) { |
| 38 | for (i in 1..n) { |
| 39 | val next = channel.receive() |
| 40 | check(next == i) |
| 41 | } |
| 42 | expect(3) |
| 43 | } |
| 44 | expect(1) |
| 45 | sender.join() |
| 46 | receiver.join() |
| 47 | finish(4) |
| 48 | } |
| 49 | } |