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.rx1 |
| 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.* |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 11 | |
| 12 | class ObservableTest : TestBase() { |
| 13 | @Test |
| 14 | fun testBasicSuccess() = runBlocking<Unit> { |
| 15 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 16 | val observable = rxObservable(coroutineContext) { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 17 | expect(4) |
| 18 | send("OK") |
| 19 | } |
| 20 | expect(2) |
| 21 | observable.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<Unit> { |
| 32 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 33 | val observable = rxObservable<String>(coroutineContext) { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 34 | expect(4) |
| 35 | throw RuntimeException("OK") |
| 36 | } |
| 37 | expect(2) |
| 38 | observable.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<Unit> { |
| 52 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 53 | val observable = rxObservable<String>(coroutineContext) { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 54 | expect(4) |
| 55 | yield() // back to main, will get cancelled |
| 56 | expectUnreached() |
| 57 | } |
| 58 | expect(2) |
| 59 | val sub = observable.subscribe({ |
| 60 | expectUnreached() |
| 61 | }, { |
| 62 | expectUnreached() |
| 63 | }) |
| 64 | expect(3) |
| 65 | yield() // to started coroutine |
| 66 | expect(5) |
| 67 | sub.unsubscribe() // will cancel coroutine |
| 68 | yield() |
| 69 | finish(6) |
| 70 | } |
| 71 | } |