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