Reputation: 7586
For instrumentation testing purposes I want to know when an observable is subscribed to and when it completes so that when all the worker threads are done, automated ui actions and assertions can be made.
Given the following code with RxJava2:
RxJavaPlugins.setOnSingleSubscribe((observable, observer) -> {
observable.doFinally(() -> Timber.d("single finish"))
.doOnSubscribe(__ -> Timber.d("single subscribe2"))
.doOnSuccess(__ -> Timber.d("single success"))
.doOnEvent((__, ___) -> Timber.d("single event"))
.doOnError(__ -> Timber.e("single error"));
Timber.d("single subscription");
return observer;
});
It only logs single subscription
but never any of the other messages.
How is it supposed to work?
The code I used in RxJava1 was:
RxJavaHooks.setOnSingleStart { _, onSubscribe ->
Timber.d("single subscription")
return onSubscribe
}
RxJavaHooks.setOnSingleReturn { subscription ->
Timber.d("single finish")
return subscription
}
Upvotes: 0
Views: 283
Reputation: 7586
I found the solution:
RxJavaPlugins.setOnSingleSubscribe((observable, observer) -> new SingleObserver() {
@Override
public void onSubscribe(Disposable d) {
Timber.d("single subscription");
observer.onSubscribe(d);
}
@Override
public void onSuccess(Object o) {
Timber.d("single success");
observer.onSuccess(o);
}
@Override
public void onError(Throwable e) {
Timber.d("single error");
observer.onError(e);
}
});
Upvotes: 1