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 | 2cdbfd7 | 2018-04-22 18:26:14 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental.selects |
| 6 | |
| 7 | import kotlinx.coroutines.experimental.* |
| 8 | import kotlinx.coroutines.experimental.channels.* |
| 9 | import kotlinx.coroutines.experimental.intrinsics.* |
| 10 | import kotlin.test.* |
| 11 | |
| 12 | class SelectChannelStressTest: TestBase() { |
| 13 | |
| 14 | @Test |
| 15 | fun testSelectSendResourceCleanupArrayChannel() = runTest { |
| 16 | val channel = ArrayChannel<Int>(1) |
| 17 | val n = 10_000_000 * stressTestMultiplier |
| 18 | expect(1) |
| 19 | channel.send(-1) // fill the buffer, so all subsequent sends cannot proceed |
| 20 | repeat(n) { i -> |
| 21 | select { |
| 22 | channel.onSend(i) { expectUnreached() } |
| 23 | default { expect(i + 2) } |
| 24 | } |
| 25 | } |
| 26 | finish(n + 2) |
| 27 | } |
| 28 | |
| 29 | @Test |
| 30 | fun testSelectReceiveResourceCleanupArrayChannel() = runTest { |
| 31 | val channel = ArrayChannel<Int>(1) |
| 32 | val n = 10_000_000 * stressTestMultiplier |
| 33 | expect(1) |
| 34 | repeat(n) { i -> |
| 35 | select { |
| 36 | channel.onReceive { expectUnreached() } |
| 37 | default { expect(i + 2) } |
| 38 | } |
| 39 | } |
| 40 | finish(n + 2) |
| 41 | } |
| 42 | |
| 43 | @Test |
| 44 | fun testSelectSendResourceCleanupRendezvousChannel() = runTest { |
| 45 | val channel = RendezvousChannel<Int>() |
| 46 | val n = 1_000_000 * stressTestMultiplier |
| 47 | expect(1) |
| 48 | repeat(n) { i -> |
| 49 | select { |
| 50 | channel.onSend(i) { expectUnreached() } |
| 51 | default { expect(i + 2) } |
| 52 | } |
| 53 | } |
| 54 | finish(n + 2) |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | fun testSelectReceiveResourceRendezvousChannel() = runTest { |
| 59 | val channel = RendezvousChannel<Int>() |
| 60 | val n = 1_000_000 * stressTestMultiplier |
| 61 | expect(1) |
| 62 | repeat(n) { i -> |
| 63 | select { |
| 64 | channel.onReceive { expectUnreached() } |
| 65 | default { expect(i + 2) } |
| 66 | } |
| 67 | } |
| 68 | finish(n + 2) |
| 69 | } |
| 70 | |
| 71 | internal fun <R> SelectBuilder<R>.default(block: suspend () -> R) { |
| 72 | this as SelectBuilderImpl // type assertion |
| 73 | if (!trySelect(null)) return |
| 74 | block.startCoroutineUndispatched(this) |
| 75 | } |
| 76 | } |