Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestSubscriber / TestSingleSubscriber assertUnsubscribed() fails #4193

Closed
jbialkowski13 opened this issue Jul 12, 2016 · 5 comments
Closed
Labels

Comments

@jbialkowski13
Copy link

Hi,

I know that Github issues are not the best place for posting issues, but I faced something strange (at least for me) with using Single together with TestSingleSubscriber.

To not copy here exactly the same content, I prefer just to leave a link to Stackoverflow issue:
http://stackoverflow.com/questions/38303194/rxjava-testsubscriber-testsinglesubscriber

Version of RxJava I am using is: 1.1.0

Thanks for your time.

@akarnokd
Copy link
Member

Hi. Could you try with 1.1.7? Could you post a self-contained unit test?

@akarnokd
Copy link
Member

Okay, what happens is that the subscribe method returns a wrapper and not the passed-in TestSingleSubscriber and the subscription to build()'s result immediately completes and unsubscribes the wrapper - leaving the TestSingleSubscriber intact. Change your OneShotUseCase.execute to this:

public void execute(SingleSubscriber<T> subscriber) {
    build().subscribe(subscriber);

    subscription.add(subscriber);
}

and the tests pass.

@jbialkowski13
Copy link
Author

I tried with 1.1.7 and same result.

Junit4 test:

@RunWith(JUnit4.class)
public class OneShotUseCaseTest {

    private TestUseCase sut;

    @Before
    public void setUp() {
        sut = new TestUseCase();
    }

    @Test
    public void shouldBuildUseCase() {
        final TestSingleSubscriber<String> stringTestSingleSubscriber = new TestSingleSubscriber<>();

        sut.executeSingle(stringTestSingleSubscriber);

        stringTestSingleSubscriber.assertValue("Test value");
    }

    @Test
    public void shouldCancelUseCase() {
        final TestSingleSubscriber<String> stringTestSingleSubscriber = new TestSingleSubscriber<>();

        sut.executeSingle(stringTestSingleSubscriber);
        sut.cancel();

        stringTestSingleSubscriber.assertUnsubscribed();
    }

    @Test
    public void shouldCancelObservable() {
        final TestSubscriber<String> testSubscriber = new TestSubscriber<>();

        sut.executeObservable(testSubscriber);
        sut.cancel();

        testSubscriber.assertUnsubscribed();
    }


    private static class TestUseCase {

        private CompositeSubscription subscription = new CompositeSubscription();

        public void cancel() {
            subscription.clear();
        }

        public void executeSingle(SingleSubscriber<String> subscriber) {
            subscription.add(
                    buildSingle().subscribe(subscriber)
            );
        }

        public void executeObservable(Subscriber<String> subscriber) {
            subscription.add(
                    buildObservable().subscribe(subscriber)
            );
        }

        protected Single<String> buildSingle() {
            return Single.just("Test value");
        }

        protected Observable<String> buildObservable() {
            return Observable.just("Test value observable");
        }
    }
}

shouldCancelObservable works fine, but shouldCancelUseCase fails.

UPDATE:
Your solution works perfect now. However I am confused that the same code works for regular Observable. Should I change code for Observable as well? Just not have any problem with future updates of RxJava (implementation change).

@akarnokd
Copy link
Member

The regular Observable wraps with a SafeSubscriber that (somewhat arguably) unsubscribes its wrapped Subscriber. Single simply doesn't do this.

@jbialkowski13
Copy link
Author

Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants