Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.rx2 |
| 6 | |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 7 | import io.reactivex.Observable |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 8 | import kotlinx.coroutines.experimental.CommonPool |
| 9 | import kotlinx.coroutines.experimental.TestBase |
| 10 | import kotlinx.coroutines.experimental.Unconfined |
| 11 | import kotlinx.coroutines.experimental.launch |
| 12 | import org.junit.Assert.assertEquals |
| 13 | import org.junit.Test |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 14 | import java.io.IOException |
| 15 | |
| 16 | /** |
| 17 | * Test emitting multiple values with [rxObservable]. |
| 18 | */ |
| 19 | class ObservableMultiTest : TestBase() { |
| 20 | @Test |
| 21 | fun testNumbers() { |
| 22 | val n = 100 * stressTestMultiplier |
| 23 | val observable = rxObservable(CommonPool) { |
| 24 | repeat(n) { send(it) } |
| 25 | } |
| 26 | checkSingleValue(observable.toList()) { list -> |
| 27 | assertEquals((0..n - 1).toList(), list) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | @Test |
| 32 | fun testConcurrentStress() { |
| 33 | val n = 10_000 * stressTestMultiplier |
| 34 | val observable = rxObservable<Int>(CommonPool) { |
| 35 | // concurrent emitters (many coroutines) |
| 36 | val jobs = List(n) { |
| 37 | // launch |
| 38 | launch(CommonPool) { |
| 39 | send(it) |
| 40 | } |
| 41 | } |
| 42 | jobs.forEach { it.join() } |
| 43 | } |
| 44 | checkSingleValue(observable.toList()) { list -> |
| 45 | assertEquals(n, list.size) |
| 46 | assertEquals((0..n - 1).toList(), list.sorted()) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | @Test |
| 51 | fun testIteratorResendUnconfined() { |
| 52 | val n = 10_000 * stressTestMultiplier |
| 53 | val observable = rxObservable(Unconfined) { |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 54 | Observable.range(0, n).consumeEach { send(it) } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 55 | } |
| 56 | checkSingleValue(observable.toList()) { list -> |
| 57 | assertEquals((0..n - 1).toList(), list) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | fun testIteratorResendPool() { |
| 63 | val n = 10_000 * stressTestMultiplier |
| 64 | val observable = rxObservable(CommonPool) { |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 65 | Observable.range(0, n).consumeEach { send(it) } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 66 | } |
| 67 | checkSingleValue(observable.toList()) { list -> |
| 68 | assertEquals((0..n - 1).toList(), list) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | fun testSendAndCrash() { |
| 74 | val observable = rxObservable(CommonPool) { |
| 75 | send("O") |
| 76 | throw IOException("K") |
| 77 | } |
| 78 | val single = rxSingle(CommonPool) { |
| 79 | var result = "" |
| 80 | try { |
Roman Elizarov | 35af7c8 | 2017-03-17 18:08:29 +0300 | [diff] [blame] | 81 | observable.consumeEach { result += it } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 82 | } catch(e: IOException) { |
| 83 | result += e.message |
| 84 | } |
| 85 | result |
| 86 | } |
| 87 | checkSingleValue(single) { |
| 88 | assertEquals("OK", it) |
| 89 | } |
| 90 | } |
| 91 | } |