Java Code Examples for rx.observers.TestSubscriber#assertUnsubscribed()

The following examples show how to use rx.observers.TestSubscriber#assertUnsubscribed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RxStoresTest.java    From grox with Apache License 2.0 6 votes vote down vote up
@Test
public void states_should_stopObservingStateChanges() {
  //GIVEN
  Store<Integer> store = new Store<>(0);
  TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
  final Subscription subscription = states(store).subscribe(testSubscriber);

  //WHEN
  subscription.unsubscribe();
  store.dispatch(integer -> integer + 1);
  final Integer state = store.getState();

  //THEN
  testSubscriber.assertNoTerminalEvent();
  testSubscriber.assertValue(0);
  testSubscriber.assertUnsubscribed();
  assertThat(state, is(1));
}
 
Example 2
Source File: RxTiPresenterSubscriptionHandlerTest.java    From ThirtyInch with Apache License 2.0 6 votes vote down vote up
@Test
public void testManageSubscriptions_WithAlreadyUnsubscribedSubscription_ShouldDoNothing()
        throws Exception {
    TestSubscriber<Void> firstSubscriber = new TestSubscriber<>();
    TestSubscriber<Void> secondSubscriber = new TestSubscriber<>();
    TestSubscriber<Void> thirdSubscriber = new TestSubscriber<>();
    firstSubscriber.unsubscribe();
    secondSubscriber.unsubscribe();
    thirdSubscriber.unsubscribe();

    mSubscriptionHandler.manageSubscriptions(firstSubscriber, secondSubscriber, thirdSubscriber);

    firstSubscriber.assertUnsubscribed();
    secondSubscriber.assertUnsubscribed();
    thirdSubscriber.assertUnsubscribed();
}
 
Example 3
Source File: RxTiPresenterSubscriptionHandlerTest.java    From ThirtyInch with Apache License 2.0 6 votes vote down vote up
@Test
public void testManageSubscriptions_WithDestroy_ShouldUnsubscribe() throws Exception {
    mPresenter.create();
    TestSubscriber<Void> firstSubscriber = new TestSubscriber<>();
    TestSubscriber<Void> secondSubscriber = new TestSubscriber<>();
    TestSubscriber<Void> thirdSubscriber = new TestSubscriber<>();

    mSubscriptionHandler.manageSubscriptions(firstSubscriber, secondSubscriber, thirdSubscriber);
    assertThat(firstSubscriber.isUnsubscribed()).isFalse();
    assertThat(secondSubscriber.isUnsubscribed()).isFalse();
    assertThat(thirdSubscriber.isUnsubscribed()).isFalse();
    mPresenter.destroy();
    firstSubscriber.assertUnsubscribed();
    secondSubscriber.assertUnsubscribed();
    thirdSubscriber.assertUnsubscribed();
}
 
Example 4
Source File: RxTiPresenterSubscriptionHandlerTest.java    From ThirtyInch with Apache License 2.0 6 votes vote down vote up
@Test
public void testManageSubscriptions_WithDetach_ShouldUnsubcribe() throws Exception {
    mPresenter.create();
    mPresenter.attachView(mView);
    TestSubscriber<Void> firstSubscriber = new TestSubscriber<>();
    TestSubscriber<Void> secondSubscriber = new TestSubscriber<>();
    TestSubscriber<Void> thirdSubscriber = new TestSubscriber<>();

    mSubscriptionHandler
            .manageViewSubscriptions(firstSubscriber, secondSubscriber, thirdSubscriber);
    assertThat(firstSubscriber.isUnsubscribed()).isFalse();
    assertThat(secondSubscriber.isUnsubscribed()).isFalse();
    assertThat(thirdSubscriber.isUnsubscribed()).isFalse();

    mPresenter.detachView();
    firstSubscriber.assertUnsubscribed();
    secondSubscriber.assertUnsubscribed();
    thirdSubscriber.assertUnsubscribed();
}
 
Example 5
Source File: RxTiPresenterSubscriptionHandlerTest.java    From ThirtyInch with Apache License 2.0 6 votes vote down vote up
@Test
public void testManagerViewSubscriptions_WithDetach_ShouldUnsubcribe() throws Exception {
    mPresenter.create();
    mPresenter.attachView(mView);
    TestSubscriber<Void> firstSubscriber = new TestSubscriber<>();
    TestSubscriber<Void> secondSubscriber = new TestSubscriber<>();
    TestSubscriber<Void> thirdSubscriber = new TestSubscriber<>();

    mSubscriptionHandler
            .manageViewSubscriptions(firstSubscriber, secondSubscriber, thirdSubscriber);
    assertThat(firstSubscriber.isUnsubscribed()).isFalse();
    assertThat(secondSubscriber.isUnsubscribed()).isFalse();
    assertThat(thirdSubscriber.isUnsubscribed()).isFalse();

    mPresenter.detachView();
    firstSubscriber.assertUnsubscribed();
    secondSubscriber.assertUnsubscribed();
    thirdSubscriber.assertUnsubscribed();
}
 
Example 6
Source File: TransformerStateMachineTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnsubscriptionFromTransition() {
    Func0<Integer> initialState = Functions.constant0(1);
    Func3<Integer, Integer, Subscriber<Integer>, Integer> transition = new Func3<Integer, Integer, Subscriber<Integer>, Integer>() {

        @Override
        public Integer call(Integer collection, Integer t, Subscriber<Integer> subscriber) {
            subscriber.onNext(123);
            subscriber.unsubscribe();
            return 1;
        }

    };
    Func2<Integer, Observer<Integer>, Boolean> completion = Functions.alwaysTrue2();
    Transformer<Integer, Integer> transformer = Transformers.stateMachine(initialState,
            transition, completion);
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    Observable.just(1, 1, 1).repeat().compose(transformer).subscribe(ts);
    ts.assertValue(123);
    ts.assertCompleted();
    ts.assertUnsubscribed();
}
 
Example 7
Source File: RxTiPresenterSubscriptionHandlerTest.java    From ThirtyInch with Apache License 2.0 5 votes vote down vote up
@Test
public void testManageSubscription_WithAlreadyUnsubscribedSubscription_ShouldDoNothing()
        throws Exception {
    TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
    testSubscriber.unsubscribe();

    mSubscriptionHandler.manageSubscription(testSubscriber);

    testSubscriber.assertUnsubscribed();
}
 
Example 8
Source File: RxTiPresenterSubscriptionHandlerTest.java    From ThirtyInch with Apache License 2.0 5 votes vote down vote up
@Test
public void testManageSubscription_WithDestroy_ShouldUnsubscribe() throws Exception {
    mPresenter.create();
    TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();

    mSubscriptionHandler.manageSubscription(testSubscriber);
    assertThat(testSubscriber.isUnsubscribed()).isFalse();

    mPresenter.destroy();
    testSubscriber.assertUnsubscribed();
}
 
Example 9
Source File: RxTiPresenterSubscriptionHandlerTest.java    From ThirtyInch with Apache License 2.0 5 votes vote down vote up
@Test
public void testManageViewSubscription_WithDetachSingleSub_ShouldUnsubscribe()
        throws Exception {
    mPresenter.create();
    mPresenter.attachView(mView);
    TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();

    mSubscriptionHandler.manageViewSubscription(testSubscriber);
    assertThat(testSubscriber.isUnsubscribed()).isFalse();

    mPresenter.detachView();
    testSubscriber.assertUnsubscribed();
}
 
Example 10
Source File: RxTiPresenterSubscriptionHandlerTest.java    From ThirtyInch with Apache License 2.0 5 votes vote down vote up
@Test
public void testManageViewSubscription_WithDetachView_ShouldUnsubscribe() throws Exception {
    mPresenter.create();
    mPresenter.attachView(mView);
    TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();

    mSubscriptionHandler.manageViewSubscription(testSubscriber);
    mPresenter.detachView();

    testSubscriber.assertUnsubscribed();
}
 
Example 11
Source File: RxTiPresenterSubscriptionHandlerTest.java    From ThirtyInch with Apache License 2.0 5 votes vote down vote up
@Test
public void testManageViewSubscriptions_WithOneAlreadyUnsubscribed_ShouldNotAddToSubscription()
        throws Exception {
    mPresenter.create();
    mPresenter.attachView(mView);
    TestSubscriber<Void> firstSubscriber = new TestSubscriber<>();
    TestSubscriber<Void> secondSubscriber = new TestSubscriber<>();
    secondSubscriber.unsubscribe();

    mSubscriptionHandler.manageViewSubscriptions(firstSubscriber, secondSubscriber);

    assertThat(firstSubscriber.isUnsubscribed()).isFalse();
    secondSubscriber.assertUnsubscribed();
}
 
Example 12
Source File: Example16Test.java    From AnDevCon-RxPatterns with Apache License 2.0 5 votes vote down vote up
@Test
public void test_anomalous_network_event() {

    // TestScheduler lets you advance time by hand
    TestScheduler scheduler = Schedulers.test();
    TestSubscriber<NetworkResponse> subscriber = new TestSubscriber<>();

    // Scheduler.Worker lets you schedule events in time
    Scheduler.Worker worker = scheduler.createWorker();

    // Subjects allow both input and output, so they can be swapped in for
    // Retrofit calls to unit test your code.
    final PublishSubject<NetworkResponse> networkSubject = PublishSubject.create();

    // schedule a first observable event to occur at 1000 ms
    worker.schedule(new Action0() {
        @Override
        public void call() {
            networkSubject.onError(new TimeoutException());
        }
    }, 10000, TimeUnit.MILLISECONDS);

    // subscribing so events appear
    networkSubject
            .subscribeOn(scheduler)
            .subscribe(subscriber);

    scheduler.advanceTimeBy(20000, TimeUnit.MILLISECONDS);

    subscriber.awaitTerminalEvent();

    // we use the class-based assertError method, since it's easier to match
    subscriber.assertError(TimeoutException.class);
    subscriber.assertValueCount(0);
    subscriber.assertUnsubscribed();
}
 
Example 13
Source File: TransformerStateMachineTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void testToListUntilChangedWithError() {
    Exception ex = new Exception("boo");
    Observable<Integer> o = Observable.error(ex);
    TestSubscriber<List<Integer>> ts = new TestSubscriber<List<Integer>>();
    o.compose(Transformers.<Integer> toListUntilChanged()).subscribe(ts);
    ts.awaitTerminalEvent();
    ts.assertError(ex);
    ts.assertUnsubscribed();
}
 
Example 14
Source File: Example16Test.java    From AnDevCon-RxPatterns with Apache License 2.0 4 votes vote down vote up
@Test
public void using_testscheduler_to_simulate_network_events() {

    // TestScheduler lets you advance time by hand
    TestScheduler scheduler = Schedulers.test();
    TestSubscriber<NetworkResponse> subscriber = new TestSubscriber<>();

    // Scheduler.Worker lets you schedule events in time
    Scheduler.Worker worker = scheduler.createWorker();

    // Subjects allow both input and output, so they can be swapped in for
    // Retrofit calls to unit test your code.
    final PublishSubject<NetworkResponse> networkSubject = PublishSubject.create();

    // schedule a first observable event to occur at 1000 ms
    worker.schedule(new Action0() {
        @Override
        public void call() {
            // explicitly calling onNext in a worker allows one to
            // create a very specific test of timed events
            networkSubject.onNext(new NetworkResponse(401));
        }
    }, 1000, TimeUnit.MILLISECONDS);

    // schedule a second observable event to occur at 2000 ms
    worker.schedule(new Action0() {
        @Override
        public void call() {
            networkSubject.onNext(new NetworkResponse(200));
            networkSubject.onCompleted();
        }
    }, 2000, TimeUnit.MILLISECONDS);

    // we must subscribe before anticipating results
    networkSubject
            .subscribeOn(scheduler)
            .subscribe(subscriber);

    // we can manually advance time using the scheduler and check assertions
    scheduler.advanceTimeBy(1500, TimeUnit.MILLISECONDS);
    subscriber.assertReceivedOnNext(Arrays.asList(
            new NetworkResponse(401)));

    // awaitTerminalEvent will wait forever if we don't advance time enough
    scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS);
    subscriber.awaitTerminalEvent();
    subscriber.assertReceivedOnNext(Arrays.asList(
            new NetworkResponse(401),
            new NetworkResponse(200)));

    // TestSubscriber provides many useful methods
    subscriber.assertNoErrors();
    subscriber.assertValueCount(2);
    subscriber.assertUnsubscribed();
}