Konrad Kamiński | 3ae898c | 2017-03-30 17:37:00 +0200 | [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. |
Konrad Kamiński | 3ae898c | 2017-03-30 17:37:00 +0200 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.reactor |
| 6 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.* |
| 8 | import org.hamcrest.core.* |
| 9 | import org.junit.* |
| 10 | import kotlin.coroutines.experimental.* |
Konrad Kamiński | 3ae898c | 2017-03-30 17:37:00 +0200 | [diff] [blame] | 11 | |
| 12 | class FluxTest : TestBase() { |
| 13 | @Test |
| 14 | fun testBasicSuccess() = runBlocking { |
| 15 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 16 | val flux = flux(coroutineContext) { |
Konrad Kamiński | 3ae898c | 2017-03-30 17:37:00 +0200 | [diff] [blame] | 17 | expect(4) |
| 18 | send("OK") |
| 19 | } |
| 20 | expect(2) |
| 21 | flux.subscribe { value -> |
| 22 | expect(5) |
| 23 | Assert.assertThat(value, IsEqual("OK")) |
| 24 | } |
| 25 | expect(3) |
| 26 | yield() // to started coroutine |
| 27 | finish(6) |
| 28 | } |
| 29 | |
| 30 | @Test |
| 31 | fun testBasicFailure() = runBlocking { |
| 32 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 33 | val flux = flux<String>(coroutineContext) { |
Konrad Kamiński | 3ae898c | 2017-03-30 17:37:00 +0200 | [diff] [blame] | 34 | expect(4) |
| 35 | throw RuntimeException("OK") |
| 36 | } |
| 37 | expect(2) |
| 38 | flux.subscribe({ |
| 39 | expectUnreached() |
| 40 | }, { error -> |
| 41 | expect(5) |
| 42 | Assert.assertThat(error, IsInstanceOf(RuntimeException::class.java)) |
| 43 | Assert.assertThat(error.message, IsEqual("OK")) |
| 44 | }) |
| 45 | expect(3) |
| 46 | yield() // to started coroutine |
| 47 | finish(6) |
| 48 | } |
| 49 | |
| 50 | @Test |
| 51 | fun testBasicUnsubscribe() = runBlocking { |
| 52 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 53 | val flux = flux<String>(coroutineContext) { |
Konrad Kamiński | 3ae898c | 2017-03-30 17:37:00 +0200 | [diff] [blame] | 54 | expect(4) |
| 55 | yield() // back to main, will get cancelled |
| 56 | expectUnreached() |
| 57 | } |
| 58 | expect(2) |
| 59 | val sub = flux.subscribe({ |
| 60 | expectUnreached() |
| 61 | }, { |
| 62 | expectUnreached() |
| 63 | }) |
| 64 | expect(3) |
| 65 | yield() // to started coroutine |
| 66 | expect(5) |
| 67 | sub.dispose() // will cancel coroutine |
| 68 | yield() |
| 69 | finish(6) |
| 70 | } |
| 71 | } |