Konrad KamiĆski | 3ae898c | 2017-03-30 17:37:00 +0200 | [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.reactor |
| 18 | |
| 19 | import kotlinx.coroutines.experimental.CommonPool |
| 20 | import kotlinx.coroutines.experimental.TestBase |
| 21 | import kotlinx.coroutines.experimental.reactive.consumeEach |
| 22 | import kotlinx.coroutines.experimental.runBlocking |
| 23 | import kotlinx.coroutines.experimental.withTimeout |
| 24 | import org.junit.Test |
| 25 | import java.util.Random |
| 26 | import kotlin.coroutines.experimental.CoroutineContext |
| 27 | |
| 28 | class FluxCompletionStressTest : TestBase() { |
| 29 | val N_REPEATS = 10_000 * stressTestMultiplier |
| 30 | |
| 31 | fun range(context: CoroutineContext, start: Int, count: Int) = flux(context) { |
| 32 | for (x in start until start + count) send(x) |
| 33 | } |
| 34 | |
| 35 | @Test |
| 36 | fun testCompletion() { |
| 37 | val rnd = Random() |
| 38 | repeat(N_REPEATS) { |
| 39 | val count = rnd.nextInt(5) |
| 40 | runBlocking { |
| 41 | withTimeout(5000) { |
| 42 | var received = 0 |
| 43 | range(CommonPool, 1, count).consumeEach { x -> |
| 44 | received++ |
| 45 | if (x != received) error("$x != $received") |
| 46 | } |
| 47 | if (received != count) error("$received != $count") |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |