blob: b99d151a6ac42951bbe1a823aab1bee6cab4eb87 [file] [log] [blame]
Konrad Kamiński3ae898c2017-03-30 17:37:00 +02001/*
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.
Konrad Kamiński3ae898c2017-03-30 17:37:00 +02003 */
4
5package kotlinx.coroutines.experimental.reactor
6
Roman Elizarov9fe5f462018-02-21 19:05:52 +03007import kotlinx.coroutines.experimental.*
8import org.hamcrest.core.*
9import org.junit.*
10import kotlin.coroutines.experimental.*
Konrad Kamiński3ae898c2017-03-30 17:37:00 +020011
12class FluxTest : TestBase() {
13 @Test
14 fun testBasicSuccess() = runBlocking {
15 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030016 val flux = flux(coroutineContext) {
Konrad Kamiński3ae898c2017-03-30 17:37:00 +020017 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 Elizarov43e3af72017-07-21 16:01:31 +030033 val flux = flux<String>(coroutineContext) {
Konrad Kamiński3ae898c2017-03-30 17:37:00 +020034 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 Elizarov43e3af72017-07-21 16:01:31 +030053 val flux = flux<String>(coroutineContext) {
Konrad Kamiński3ae898c2017-03-30 17:37:00 +020054 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}