Skip to content

2.x Testing a Single unexpectedly results in an IllegalStateException with message: "onSubscribe not called in proper order" #5339

@autonomousapps

Description

@autonomousapps

RxJava 2.0.3.

Sample code with the problem:

@Test public void test() throws Exception {
        mockNoNetwork(); // will result in an exception with NO_NETWORK error code as shown below

        service.getSingle()
                .compose(ApiHelper.checkForNetwork())
                .test()
                .assertError(throwable -> {
                    if (throwable instanceof ApiException) {
                        int errorCode = ((ApiException) throwable).getErrorCode();
                        return errorCode == NO_NETWORK;
                    }
                    return false;
                });
}

// ApiHelper:
    public static <T> SingleTransformer<Result<T>, T> checkForNetwork() {
        return upstream ->
                upstream.delaySubscription(isNetworkAvailable())
                        .observeOn(ioScheduler) // in tests, changed to Trampoline
                        .map(checkForErrors());
    }

    private static CompletableSource isNetworkAvailable() {
        return subscriber -> {
            if (isNetworkUnavailable()) { // this is true in test
                subscriber.onError(ApiException.fromErrorCode(NO_NETWORK));
            } else {
                subscriber.onComplete();
            }
        };
    }

I'm getting the exception I want, but there is an additional exception thrown which seems to be complaining about a failure to subscribe to the Single (and this fails the test). What am I missing?

Here is the stack trace:

java.lang.AssertionError: Error present but other errors as well (latch = 0, values = 0, errors = 2, completions = 0)

	at io.reactivex.observers.BaseTestConsumer.fail(BaseTestConsumer.java:133)
	at io.reactivex.observers.BaseTestConsumer.assertError(BaseTestConsumer.java:278)
	at com.myapp.backend.retrofit.membership.ServiceTest.test(ServiceTest.java:61)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: io.reactivex.exceptions.CompositeException: 2 exceptions occurred. 
	at io.reactivex.observers.BaseTestConsumer.fail(BaseTestConsumer.java:138)
	... 31 more
Caused by: io.reactivex.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
	at io.reactivex.exceptions.CompositeException.getCause(CompositeException.java:105)
	at java.lang.Throwable.printEnclosedStackTrace(Throwable.java:707)
	at java.lang.Throwable.printStackTrace(Throwable.java:667)
	at java.lang.Throwable.printStackTrace(Throwable.java:721)
	at org.junit.runner.notification.Failure.getTrace(Failure.java:75)
	at com.intellij.junit4.JUnit4TestListener.getTrace(JUnit4TestListener.java:297)
	at com.intellij.junit4.JUnit4TestListener.testFailure(JUnit4TestListener.java:279)
	at com.intellij.junit4.JUnit4TestListener.testFailure(JUnit4TestListener.java:231)
	at com.intellij.junit4.JUnit4TestListener.testFailure(JUnit4TestListener.java:207)
	at org.junit.runner.notification.SynchronizedRunListener.testFailure(SynchronizedRunListener.java:63)
	at org.junit.runner.notification.RunNotifier$4.notifyListener(RunNotifier.java:142)
	at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:72)
	at org.junit.runner.notification.RunNotifier.fireTestFailures(RunNotifier.java:138)
	at org.junit.runner.notification.RunNotifier.fireTestFailure(RunNotifier.java:132)
	at org.junit.internal.runners.model.EachTestNotifier.addFailure(EachTestNotifier.java:23)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:329)
	... 18 more
Caused by: java.lang.IllegalStateException: onSubscribe not called in proper order
	at io.reactivex.observers.TestObserver.onError(TestObserver.java:163)
	at io.reactivex.internal.operators.single.SingleMap$1.onError(SingleMap.java:55)
	at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.run(SingleObserveOn.java:79)
	at io.reactivex.internal.schedulers.TrampolineScheduler.scheduleDirect(TrampolineScheduler.java:49)
	at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.onError(SingleObserveOn.java:71)
	at io.reactivex.internal.operators.single.SingleDelayWithCompletable$OtherObserver.onError(SingleDelayWithCompletable.java:65)
	at com.myapp.backend.retrofit.ApiHelper.lambda$checkForNetwork$2(ApiHelper.java:77)
	at io.reactivex.internal.operators.single.SingleDelayWithCompletable.subscribeActual(SingleDelayWithCompletable.java:36)
	at io.reactivex.Single.subscribe(Single.java:2656)
	at io.reactivex.internal.operators.single.SingleObserveOn.subscribeActual(SingleObserveOn.java:35)
	at io.reactivex.Single.subscribe(Single.java:2656)
	at io.reactivex.internal.operators.single.SingleMap.subscribeActual(SingleMap.java:33)
	at io.reactivex.Single.subscribe(Single.java:2656)
	at io.reactivex.Single.test(Single.java:3067)
	at com.myapp.backend.retrofit.membership.ServiceTest.test(ServiceTest.java:60)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	... 18 more
Caused by: com.myapp.backend.retrofit.ApiException: Code: -4
	at com.myapp.backend.retrofit.ApiException.fromErrorCode(ApiException.java:15)
	... 38 more

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions