Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava Completabe andThen testing

Tags:

rx-java

I have the following RxJava2 Kotlin code:

val tester = Completable.complete()
            .andThen(SingleSource<Int> { Single.just(42) })
            .test()        
tester.assertComplete()
tester.assertValue(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
    });
like image 831
Miguel Beltran Avatar asked Aug 14 '17 17:08

Miguel Beltran


1 Answers

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:

.andThen(Single.just(42))

or deferred use

.andThen(Single.defer { Single.just(42) })
like image 53
akarnokd Avatar answered Sep 21 '22 12:09

akarnokd