blob: 8336187452989f27e465833e558815f90d9b0ee9 [file] [log] [blame]
Konrad KamiƄski3ae898c2017-03-30 17:37:00 +02001/*
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
17package kotlinx.coroutines.experimental.reactor
18
19import kotlinx.coroutines.experimental.CommonPool
20import kotlinx.coroutines.experimental.TestBase
21import kotlinx.coroutines.experimental.reactive.consumeEach
22import kotlinx.coroutines.experimental.runBlocking
23import kotlinx.coroutines.experimental.withTimeout
24import org.junit.Test
25import java.util.Random
26import kotlin.coroutines.experimental.CoroutineContext
27
28class 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}