blob: f085919342a1b59f1106602dd933754b5b6a1749 [file] [log] [blame]
Roman Elizarovd14a3902017-03-13 12:03:35 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarovd14a3902017-03-13 12:03:35 +03003 */
4
5package kotlinx.coroutines.experimental.reactive
6
7import kotlinx.coroutines.experimental.CommonPool
8import kotlinx.coroutines.experimental.TestBase
9import kotlinx.coroutines.experimental.runBlocking
10import kotlinx.coroutines.experimental.withTimeout
11import org.junit.Test
12import java.util.*
13import kotlin.coroutines.experimental.CoroutineContext
14
15class PublisherCompletionStressTest : TestBase() {
16 val N_REPEATS = 10_000 * stressTestMultiplier
17
18 fun range(context: CoroutineContext, start: Int, count: Int) = publish<Int>(context) {
19 for (x in start until start + count) send(x)
20 }
21
22 @Test
23 fun testCompletion() {
24 val rnd = Random()
25 repeat(N_REPEATS) {
26 val count = rnd.nextInt(5)
27 runBlocking {
28 withTimeout(5000) {
29 var received = 0
Roman Elizarov86349be2017-03-17 16:47:37 +030030 range(CommonPool, 1, count).consumeEach { x ->
Roman Elizarovd14a3902017-03-13 12:03:35 +030031 received++
32 if (x != received) error("$x != $received")
33 }
34 if (received != count) error("$received != $count")
35 }
36 }
37 }
38 }
39}