Skip to content

Modify Rx tests to work with real asynchronous observables #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions kotlinx-coroutines-rx/src/test/kotlin/AsyncRxTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kotlinx.coroutines

import org.junit.Test
import rx.Observable
import java.util.concurrent.TimeUnit
import kotlin.test.assertEquals
import kotlin.test.fail

Expand All @@ -17,6 +18,18 @@ class AsyncRxTest {
}
}

@Test
fun testSingleWithDelay() {
val observable = asyncRx<String> {
Observable.timer(50, TimeUnit.MILLISECONDS).map { "O" }.awaitSingle() + "K"
}

checkObservableWithSingleValue(observable) {
assertEquals("OK", it)
}
}


@Test
fun testSingleException() {
val observable = asyncRx<String> {
Expand Down Expand Up @@ -117,26 +130,15 @@ class AsyncRxTest {
observable: Observable<*>,
checker: (Throwable) -> Unit
) {
var onErrorCalled = false
observable.subscribe({ fail("Next item on erroneous observable") }) {
checker(it)
onErrorCalled = true
}

assert(onErrorCalled)
val singleNotification = observable.materialize().toBlocking().single()
checker(singleNotification.throwable)
}

private fun <T> checkObservableWithSingleValue(
observable: Observable<T>,
checker: (T) -> Unit
) {
var subscribeCalled = false

observable.single().subscribe {
checker(it)
subscribeCalled = true
}

assert(subscribeCalled)
val singleValue = observable.toBlocking().single()
checker(singleValue)
}
}