RxJava Completabe andThen testing

I have the following RxJava2 Kotlin code:

val tester = Completable.complete()
            .andThen(SingleSource<Int> { Single.just(42) })

This simulates having a Completable observable (imagine a simple update operation to an API) and then a Single observable (image a get operation on an API). I want to concatenate the two observables in a way that, when the Completable finishes, the Single runs and finally I get the onSuccess event on my observer (the Int 42).

However this test code is not working. The assertion fails with the following error:

java.lang.AssertionError: Not completed
(latch = 1, values = 0, errors = 0, completions = 0))

I am not able to understand what I am doing wrong, I expect that the Completable emits onComplete on subscription, and then the Single subscribes, and my observer (tester) gets an onSuccess event with the value 42, but seems that the subscription stays "suspended" not emitting anything.

The idea is similar to the one found in this blog post: https://android.jlelse.eu/making-your-rxjava-intentions-clearer-with-single-and-completable-f064d98d53a8

apiClient.updateMyData(myUpdatedData) // a Completable
    .andThen(performOtherOperation()) // a Single<OtherResult>
    .subscribe(otherResult -> {
        // handle otherResult
    }, throwable -> {
        // handle error

1 answer

  • answered 2017-08-14 17:55 akarnokd

    The problem is Kotlin with its ambiguous use of curly braces:

    .andThen(SingleSource<Int> { Single.just(42) })

    You create a SingleSource that does noting to its SingleObserver, but that is hidden by the Kotlin syntax. What you need is plain use:


    or deferred use

    .andThen(Single.defer { Single.just(42) })