aboutsummaryrefslogtreecommitdiff
path: root/reactive/kotlinx-coroutines-rx2/test/Check.kt
blob: beb2c43a3df418c8c285af4910161813df0cf747 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines.rx2

import io.reactivex.*
import io.reactivex.functions.Consumer
import io.reactivex.plugins.*

fun <T> checkSingleValue(
    observable: Observable<T>,
    checker: (T) -> Unit
) {
    val singleValue = observable.blockingSingle()
    checker(singleValue)
}

fun checkErroneous(
        observable: Observable<*>,
        checker: (Throwable) -> Unit
) {
    val singleNotification = observable.materialize().blockingSingle()
    val error = singleNotification.error ?: error("Excepted error")
    checker(error)
}

fun <T> checkSingleValue(
    single: Single<T>,
    checker: (T) -> Unit
) {
    val singleValue = single.blockingGet()
    checker(singleValue)
}

fun checkErroneous(
    single: Single<*>,
    checker: (Throwable) -> Unit
) {
    try {
        single.blockingGet()
        error("Should have failed")
    } catch (e: Throwable) {
        checker(e)
    }
}

fun <T> checkMaybeValue(
        maybe: Maybe<T>,
        checker: (T?) -> Unit
) {
    val maybeValue = maybe.toFlowable().blockingIterable().firstOrNull()
    checker(maybeValue)
}

@Suppress("UNCHECKED_CAST")
fun checkErroneous(
    maybe: Maybe<*>,
    checker: (Throwable) -> Unit
) {
    try {
        (maybe as Maybe<Any>).blockingGet()
        error("Should have failed")
    } catch (e: Throwable) {
        checker(e)
    }
}

inline fun withExceptionHandler(noinline handler: (Throwable) -> Unit, block: () -> Unit) {
    val original = RxJavaPlugins.getErrorHandler()
    RxJavaPlugins.setErrorHandler { handler(it) }
    try {
        block()
    } finally {
        RxJavaPlugins.setErrorHandler(original)
    }
}