blob: 1ebc8e9a5a79467b87e8188b49d5f312edfdf50d [file] [log] [blame]
Roman Elizarov331750b2017-02-15 17:59:17 +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 Elizarov331750b2017-02-15 17:59:17 +03003 */
4
5package kotlinx.coroutines.experimental.rx1
6
Roman Elizarov9fe5f462018-02-21 19:05:52 +03007import kotlinx.coroutines.experimental.*
8import org.hamcrest.core.*
9import org.junit.*
10import kotlin.coroutines.experimental.*
Roman Elizarov331750b2017-02-15 17:59:17 +030011
12class ObservableTest : TestBase() {
13 @Test
14 fun testBasicSuccess() = runBlocking<Unit> {
15 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030016 val observable = rxObservable(coroutineContext) {
Roman Elizarov331750b2017-02-15 17:59:17 +030017 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 Elizarov43e3af72017-07-21 16:01:31 +030033 val observable = rxObservable<String>(coroutineContext) {
Roman Elizarov331750b2017-02-15 17:59:17 +030034 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 Elizarov43e3af72017-07-21 16:01:31 +030053 val observable = rxObservable<String>(coroutineContext) {
Roman Elizarov331750b2017-02-15 17:59:17 +030054 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}