RxJava: 2.1.2 Completable with andThen never completes

This test fails:

Completable.complete()
        .andThen{ Completable.complete() }
        .test()
        .assertComplete()

However this one succeeds:

Completable.complete()
        .test()
        .assertComplete()

This one also succeeds:

    Flowable.just("")
            .flatMap { Flowable.just("") }
            .test()
            .assertComplete()

My understanding is that concating (which is what andThen does?) two completables which complete immediately should complete. I tried:

  • using awaitForTerminalEvent but it just runs forever in the first case.
  • .andThen{} instead of .andThen{ Completable.complete() }
  • Completable.fromCallable instead of Completable.complete() or switching from a Flowable to Completable

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 4
  • Comments: 15 (5 by maintainers)

Commits related to this issue

Most upvoted comments

You want andThen(Completable.complete()). Note the use of parenthesis and not curly braces. The latter creates a lambda that doesn’t call its emitter.

On Sat, Aug 12, 2017, 11:30 AM Michał Klimczak notifications@github.com wrote:

This test fails:

Completable.complete() .andThen{ Completable.complete() } .test() .assertComplete()

However this one succeeds:

Completable.complete() .test() .assertComplete()

This one also succeeds:

Flowable.just("")
        .flatMap { Flowable.just("") }
        .test()
        .assertComplete()

My understanding is that concating (which is what andThen does?) two completables which complete immediately should complete. I tried:

  • using awaitForTerminalEvent but it just runs forever in the first case.
  • .andThen{} instead of .andThen{ Completable.complete() }
  • Completable.fromCallable instead of Completable.complete() or switching from a Flowable to Completable

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ReactiveX/RxJava/issues/5551, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEbqMY0_Tox59wudzvNRhY1aL1URWks5sXe8mgaJpZM4O1fDE .

Please be patient on StackOverflow (deleted) next time!

For one, you implemented Completable incorrectly and in a way most people don’t need to do it. Use Completable.create() instead. Second, you have to complete the CompletableObserver, otherwise andThen won’t know to switch to the other source.

Completable completable = Completable.create(emitter -> {
    emitter.onComplete();
});

completable
        .andThen(observable)
        .subscribe(integer -> System.out.println("emitted:"+integer));

This is a very silly api tbh, everyone I know was burned by this

Generally I am, but it honestly looked like an error to me, especially given the way flatMap and similar operators work for Flowable. Sorry!

@ericntd It’s not an issue with RxJava but how Kotlin trains you to use { } as the convenient lambda syntax. So RxJava 3 can’t help you solve this problem either.

@amanshuraikwar That is how it is supposed to be done. How is it not working. Could you write an unit test demonstrating your problem?

@micHar I agree with you, the .andThen{} is equivalent to .doOnComplete{}, isn’t it? I mean what would be the use case that only .andThen{} solve ?