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.* |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 20 | import kotlinx.coroutines.experimental.timeunit.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 21 | import org.junit.* |
| 22 | import org.junit.runner.* |
| 23 | import org.junit.runners.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 24 | import java.util.concurrent.atomic.* |
| 25 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Creates a broadcast channel and repeatedly opens new subscription, receives event, closes it, |
| 29 | * to stress test the logic of opening the subscription |
| 30 | * to broadcast channel while events are being concurrently sent to it. |
| 31 | */ |
| 32 | @RunWith(Parameterized::class) |
| 33 | class BroadcastChannelSubStressTest( |
| 34 | val kind: TestBroadcastChannelKind |
| 35 | ) : TestBase() { |
| 36 | companion object { |
| 37 | @Parameterized.Parameters(name = "{0}") |
| 38 | @JvmStatic |
| 39 | fun params(): Collection<Array<Any>> = |
| 40 | TestBroadcastChannelKind.values().map { arrayOf<Any>(it) } |
| 41 | } |
| 42 | |
| 43 | private val nSeconds = 5 * stressTestMultiplier |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 44 | private val broadcast = kind.create<Long>() |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 45 | |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 46 | private val sentTotal = AtomicLong() |
| 47 | private val receivedTotal = AtomicLong() |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 48 | |
| 49 | @Test |
| 50 | fun testStress() = runBlocking { |
Roman Elizarov | d3d335b | 2017-10-21 17:43:53 +0300 | [diff] [blame] | 51 | println("--- BroadcastChannelSubStressTest $kind") |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 52 | val ctx = coroutineContext + CommonPool |
| 53 | val sender = |
| 54 | launch(context = ctx + CoroutineName("Sender")) { |
| 55 | while (isActive) { |
| 56 | broadcast.send(sentTotal.incrementAndGet()) |
| 57 | } |
| 58 | } |
| 59 | val receiver = |
| 60 | launch(context = ctx + CoroutineName("Receiver")) { |
Roman Elizarov | 45c8565 | 2017-10-13 14:46:22 +0300 | [diff] [blame] | 61 | var last = -1L |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 62 | while (isActive) { |
Marko Devcic | 1d6230a | 2018-04-04 20:13:08 +0200 | [diff] [blame] | 63 | val channel = broadcast.openSubscription() |
| 64 | val i = channel.receive() |
| 65 | check(i >= last) { "Last was $last, got $i" } |
| 66 | if (!kind.isConflated) check(i != last) { "Last was $last, got it again" } |
| 67 | receivedTotal.incrementAndGet() |
| 68 | last = i |
| 69 | channel.cancel() |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 70 | } |
| 71 | } |
Roman Elizarov | 9525f3c | 2017-10-27 12:11:55 +0300 | [diff] [blame] | 72 | var prevSent = -1L |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 73 | repeat(nSeconds) { sec -> |
| 74 | delay(1000) |
Roman Elizarov | 9525f3c | 2017-10-27 12:11:55 +0300 | [diff] [blame] | 75 | val curSent = sentTotal.get() |
| 76 | println("${sec + 1}: Sent $curSent, received ${receivedTotal.get()}") |
| 77 | check(curSent > prevSent) { "Send stalled at $curSent events" } |
| 78 | prevSent = curSent |
Roman Elizarov | 4391897 | 2017-10-07 21:00:06 +0300 | [diff] [blame] | 79 | } |
| 80 | withTimeout(5, TimeUnit.SECONDS) { |
| 81 | sender.cancelAndJoin() |
| 82 | receiver.cancelAndJoin() |
| 83 | } |
| 84 | } |
| 85 | } |