Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package kotlinx.coroutines.experimental.channels |
| 18 | |
| 19 | import kotlinx.coroutines.experimental.* |
| 20 | import kotlinx.coroutines.experimental.selects.select |
| 21 | import org.junit.After |
| 22 | import org.junit.Test |
| 23 | import org.junit.runner.RunWith |
| 24 | import org.junit.runners.Parameterized |
| 25 | import java.util.concurrent.TimeUnit |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 26 | import java.util.concurrent.atomic.AtomicLong |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Tests delivery of events to multiple broadcast channel subscribers. |
| 30 | */ |
| 31 | @RunWith(Parameterized::class) |
| 32 | class BroadcastChannelMultiReceiveStressTest( |
| 33 | val kind: TestBroadcastChannelKind |
| 34 | ) : TestBase() { |
| 35 | companion object { |
| 36 | @Parameterized.Parameters(name = "{0}") |
| 37 | @JvmStatic |
| 38 | fun params(): Collection<Array<Any>> = |
| 39 | TestBroadcastChannelKind.values().map { arrayOf<Any>(it) } |
| 40 | } |
| 41 | |
| 42 | private val nReceivers = if (isStressTest) 10 else 5 |
| 43 | private val nSeconds = 5 * stressTestMultiplier |
| 44 | |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 45 | private val broadcast = kind.create<Long>() |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 46 | private val pool = newFixedThreadPoolContext(nReceivers + 1, "BroadcastChannelMultiReceiveStressTest") |
| 47 | |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 48 | private val sentTotal = AtomicLong() |
| 49 | private val receivedTotal = AtomicLong() |
| 50 | private val stopOnReceive = AtomicLong(-1) |
| 51 | private val lastReceived = Array(nReceivers) { AtomicLong(-1) } |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 52 | |
| 53 | @After |
| 54 | fun tearDown() { |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 55 | pool.close() |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Test |
| 59 | fun testStress() = runBlocking { |
| 60 | val ctx = pool + coroutineContext[Job]!! |
| 61 | val sender = |
| 62 | launch(context = ctx + CoroutineName("Sender")) { |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 63 | var i = 0L |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 64 | while (isActive) { |
Roman Elizarov | fdd6db5 | 2017-10-07 23:01:20 +0300 | [diff] [blame] | 65 | broadcast.send(++i) |
| 66 | sentTotal.set(i) // set sentTotal only if `send` was not cancelled |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | val receivers = mutableListOf<Job>() |
| 70 | repeat(nSeconds) { sec -> |
| 71 | // launch new receiver up to max |
| 72 | if (receivers.size < nReceivers) { |
| 73 | val receiverIndex = receivers.size |
| 74 | val name = "Receiver$receiverIndex" |
| 75 | println("$sec: Launching $name") |
| 76 | receivers += launch(ctx + CoroutineName(name)) { |
| 77 | broadcast.openSubscription().use { sub -> |
| 78 | when (receiverIndex % 5) { |
| 79 | 0 -> doReceive(sub, receiverIndex) |
| 80 | 1 -> doReceiveOrNull(sub, receiverIndex) |
| 81 | 2 -> doIterator(sub, receiverIndex) |
| 82 | 3 -> doReceiveSelect(sub, receiverIndex) |
| 83 | 4 -> doReceiveSelectOrNull(sub, receiverIndex) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | // wait a sec |
Roman Elizarov | fdd6db5 | 2017-10-07 23:01:20 +0300 | [diff] [blame] | 89 | delay(100) |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 90 | // print progress |
| 91 | println("${sec + 1}: Sent ${sentTotal.get()}, received ${receivedTotal.get()}, receivers=${receivers.size}") |
| 92 | } |
| 93 | sender.cancelAndJoin() |
| 94 | println("Tested with nReceivers=$nReceivers") |
| 95 | val total = sentTotal.get() |
| 96 | println(" Sent $total events, waiting for receivers") |
| 97 | stopOnReceive.set(total) |
Roman Elizarov | fdd6db5 | 2017-10-07 23:01:20 +0300 | [diff] [blame] | 98 | try { |
| 99 | withTimeout(5, TimeUnit.SECONDS) { |
| 100 | receivers.forEachIndexed { index, receiver -> |
| 101 | if (lastReceived[index].get() == total) |
| 102 | receiver.cancel() |
| 103 | else |
| 104 | receiver.join() |
| 105 | } |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 106 | } |
Roman Elizarov | fdd6db5 | 2017-10-07 23:01:20 +0300 | [diff] [blame] | 107 | } catch (e: Exception) { |
| 108 | println("Failed: $e") |
| 109 | receivers.indices.forEach { index -> |
| 110 | println("lastReceived[$index] = ${lastReceived[index].get()}") |
| 111 | } |
| 112 | throw e |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 113 | } |
| 114 | println(" Received ${receivedTotal.get()} events") |
| 115 | } |
| 116 | |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 117 | private fun doReceived(receiverIndex: Int, i: Long): Boolean { |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 118 | val last = lastReceived[receiverIndex].get() |
| 119 | check(i > last) { "Last was $last, got $i" } |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 120 | if (last != -1L && !kind.isConflated) |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 121 | check(i == last + 1) { "Last was $last, got $i" } |
| 122 | receivedTotal.incrementAndGet() |
| 123 | lastReceived[receiverIndex].set(i) |
| 124 | return i == stopOnReceive.get() |
| 125 | } |
| 126 | |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 127 | private suspend fun doReceive(channel: ReceiveChannel<Long>, receiverIndex: Int) { |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 128 | while (true) { |
| 129 | try { |
| 130 | val stop = doReceived(receiverIndex, channel.receive()) |
| 131 | if (stop) break |
| 132 | } |
| 133 | catch (ex: ClosedReceiveChannelException) { break } |
| 134 | } |
| 135 | } |
| 136 | |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 137 | private suspend fun doReceiveOrNull(channel: ReceiveChannel<Long>, receiverIndex: Int) { |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 138 | while (true) { |
| 139 | val stop = doReceived(receiverIndex, channel.receiveOrNull() ?: break) |
| 140 | if (stop) break |
| 141 | } |
| 142 | } |
| 143 | |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 144 | private suspend fun doIterator(channel: ReceiveChannel<Long>, receiverIndex: Int) { |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 145 | for (event in channel) { |
| 146 | val stop = doReceived(receiverIndex, event) |
| 147 | if (stop) break |
| 148 | } |
| 149 | } |
| 150 | |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 151 | private suspend fun doReceiveSelect(channel: ReceiveChannel<Long>, receiverIndex: Int) { |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 152 | while (true) { |
| 153 | try { |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 154 | val event = select<Long> { channel.onReceive { it } } |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 155 | val stop = doReceived(receiverIndex, event) |
| 156 | if (stop) break |
| 157 | } catch (ex: ClosedReceiveChannelException) { break } |
| 158 | } |
| 159 | } |
| 160 | |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 161 | private suspend fun doReceiveSelectOrNull(channel: ReceiveChannel<Long>, receiverIndex: Int) { |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 162 | while (true) { |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 163 | val event = select<Long?> { channel.onReceiveOrNull { it } } ?: break |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 164 | val stop = doReceived(receiverIndex, event) |
| 165 | if (stop) break |
| 166 | } |
| 167 | } |
| 168 | } |