Java Code Examples for io.reactivex.rxjava3.observers.TestObserver#dispose()

The following examples show how to use io.reactivex.rxjava3.observers.TestObserver#dispose() . 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: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void valueMissedWhenNoSubscribers() {
  PublishSubject<String> subject = PublishSubject.create();
  Observable<String> observable = subject.compose(ReplayingShare.<String>instance());

  TestObserver<String> observer1 = new TestObserver<>();
  observable.subscribe(observer1);
  observer1.assertNoValues();
  observer1.dispose();

  subject.onNext("Foo");
  observer1.assertNoValues();

  TestObserver<String> observer2 = new TestObserver<>();
  observable.subscribe(observer2);
  observer2.assertNoValues();
}
 
Example 2
Source File: PublishRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void addRemoveRance() throws Exception {

    for (int i = 0; i < 100; i++) {
        final PublishRelay<Integer> pp = PublishRelay.create();

        final TestObserver<Integer> ts = pp.test();

        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                pp.subscribe();
            }
        };
        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                ts.dispose();
            }
        };

        TestHelper.race(r1, r2, Schedulers.io());
    }
}
 
Example 3
Source File: PublishRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void crossCancel() {
    final TestObserver<Integer> ts1 = new TestObserver<Integer>();
    TestObserver<Integer> ts2 = new TestObserver<Integer>() {
        @Override
        public void onNext(Integer t) {
            super.onNext(t);
            ts1.dispose();
        }
    };

    PublishRelay<Integer> pp = PublishRelay.create();

    pp.subscribe(ts2);
    pp.subscribe(ts1);

    pp.accept(1);

    ts2.assertValue(1);

    ts1.assertNoValues();
}
 
Example 4
Source File: BehaviorRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void addRemoveRace() {
    for (int i = 0; i < 500; i++) {
        final BehaviorRelay<Object> p = BehaviorRelay.create();

        final TestObserver<Object> ts = p.test();

        Runnable r1 = new Runnable() {
            @Override
            @SuppressWarnings("CheckReturnValue")
            public void run() {
                p.test();
            }
        };

        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                ts.dispose();
            }
        };

        TestHelper.race(r1, r2, Schedulers.single());
    }
}
 
Example 5
Source File: ReplayRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void subscribeCancelRace() {
    for (int i = 0; i < 500; i++) {
        final TestObserver<Integer> ts = new TestObserver<Integer>();

        final ReplayRelay<Integer> rp = ReplayRelay.create();

        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                rp.subscribe(ts);
            }
        };

        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                ts.dispose();
            }
        };

        TestHelper.race(r1, r2, Schedulers.single());
    }
}
 
Example 6
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void initialValueToNewSubscriberAfterUnsubscribe() {
  PublishSubject<String> subject = PublishSubject.create();
  Observable<String> observable = subject.compose(ReplayingShare.<String>instance());

  TestObserver<String> observer1 = new TestObserver<>();
  observable.subscribe(observer1);
  observer1.assertNoValues();

  subject.onNext("Foo");
  observer1.assertValues("Foo");
  observer1.dispose();

  TestObserver<String> observer2 = new TestObserver<>();
  observable.subscribe(observer2);
  observer2.assertValues("Foo");
}
 
Example 7
Source File: PublishRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void addRemoveRance() throws Exception {

    for (int i = 0; i < 100; i++) {
        final PublishRelay<Integer> pp = PublishRelay.create();

        final TestObserver<Integer> ts = pp.test();

        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                pp.subscribe();
            }
        };
        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                ts.dispose();
            }
        };

        TestHelper.race(r1, r2, Schedulers.io());
    }
}
 
Example 8
Source File: PublishRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void crossCancel() {
    final TestObserver<Integer> ts1 = new TestObserver<Integer>();
    TestObserver<Integer> ts2 = new TestObserver<Integer>() {
        @Override
        public void onNext(Integer t) {
            super.onNext(t);
            ts1.dispose();
        }
    };

    PublishRelay<Integer> pp = PublishRelay.create();

    pp.subscribe(ts2);
    pp.subscribe(ts1);

    pp.accept(1);

    ts2.assertValue(1);

    ts1.assertNoValues();
}
 
Example 9
Source File: BehaviorRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void addRemoveRace() {
    for (int i = 0; i < 500; i++) {
        final BehaviorRelay<Object> p = BehaviorRelay.create();

        final TestObserver<Object> ts = p.test();

        Runnable r1 = new Runnable() {
            @Override
            @SuppressWarnings("CheckReturnValue")
            public void run() {
                p.test();
            }
        };

        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                ts.dispose();
            }
        };

        TestHelper.race(r1, r2, Schedulers.single());
    }
}
 
Example 10
Source File: ReplayRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void subscribeCancelRace() {
    for (int i = 0; i < 500; i++) {
        final TestObserver<Integer> ts = new TestObserver<Integer>();

        final ReplayRelay<Integer> rp = ReplayRelay.create();

        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                rp.subscribe(ts);
            }
        };

        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                ts.dispose();
            }
        };

        TestHelper.race(r1, r2, Schedulers.single());
    }
}
 
Example 11
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 5 votes vote down vote up
@Test public void unsubscribeBeforeSubscribePreventsCacheEmission() {
  PublishSubject<String> upstream = PublishSubject.create();
  Observable<String> replayed = upstream.compose(ReplayingShare.<String>instance());
  replayed.subscribe();
  upstream.onNext("something to cache");

  TestObserver<String> testObserver = new TestObserver<>();
  testObserver.dispose();
  replayed.subscribe(testObserver);
  testObserver.assertNoValues();
}
 
Example 12
Source File: PublishRelayTest.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
/**
 * Should be able to unsubscribe all Subscribers, have it stop emitting, then subscribe new ones and it start emitting again.
 */
@Test
public void testReSubscribe() {
    final PublishRelay<Integer> ps = PublishRelay.create();

    Observer<Integer> o1 = TestHelper.mockObserver();
    TestObserver<Integer> ts = new TestObserver<Integer>(o1);
    ps.subscribe(ts);

    // emit
    ps.accept(1);

    // validate we got it
    InOrder inOrder1 = inOrder(o1);
    inOrder1.verify(o1, times(1)).onNext(1);
    inOrder1.verifyNoMoreInteractions();

    // unsubscribe
    ts.dispose();

    // emit again but nothing will be there to receive it
    ps.accept(2);

    Observer<Integer> o2 = TestHelper.mockObserver();
    TestObserver<Integer> ts2 = new TestObserver<Integer>(o2);
    ps.subscribe(ts2);

    // emit
    ps.accept(3);

    // validate we got it
    InOrder inOrder2 = inOrder(o2);
    inOrder2.verify(o2, times(1)).onNext(3);
    inOrder2.verifyNoMoreInteractions();

    ts2.dispose();
}
 
Example 13
Source File: ReplayRelayTest.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Test
public void cancelRace() {
    for (int i = 0; i < 500; i++) {

        final ReplayRelay<Integer> rp = ReplayRelay.create();
        final TestObserver<Integer> ts1 = rp.test();
        final TestObserver<Integer> ts2 = rp.test();

        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                ts1.dispose();
            }
        };

        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                ts2.dispose();
            }
        };

        TestHelper.race(r1, r2, Schedulers.single());

        assertFalse(rp.hasObservers());
    }
}
 
Example 14
Source File: PublishRelayTest.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
/**
 * Should be able to unsubscribe all Subscribers, have it stop emitting, then subscribe new ones and it start emitting again.
 */
@Test
public void testReSubscribe() {
    final PublishRelay<Integer> ps = PublishRelay.create();

    Observer<Integer> o1 = TestHelper.mockObserver();
    TestObserver<Integer> ts = new TestObserver<Integer>(o1);
    ps.subscribe(ts);

    // emit
    ps.accept(1);

    // validate we got it
    InOrder inOrder1 = inOrder(o1);
    inOrder1.verify(o1, times(1)).onNext(1);
    inOrder1.verifyNoMoreInteractions();

    // unsubscribe
    ts.dispose();

    // emit again but nothing will be there to receive it
    ps.accept(2);

    Observer<Integer> o2 = TestHelper.mockObserver();
    TestObserver<Integer> ts2 = new TestObserver<Integer>(o2);
    ps.subscribe(ts2);

    // emit
    ps.accept(3);

    // validate we got it
    InOrder inOrder2 = inOrder(o2);
    inOrder2.verify(o2, times(1)).onNext(3);
    inOrder2.verifyNoMoreInteractions();

    ts2.dispose();
}
 
Example 15
Source File: ReplayRelayTest.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Test
public void cancelRace() {
    for (int i = 0; i < 500; i++) {

        final ReplayRelay<Integer> rp = ReplayRelay.create();
        final TestObserver<Integer> ts1 = rp.test();
        final TestObserver<Integer> ts2 = rp.test();

        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                ts1.dispose();
            }
        };

        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                ts2.dispose();
            }
        };

        TestHelper.race(r1, r2, Schedulers.single());

        assertFalse(rp.hasObservers());
    }
}
 
Example 16
Source File: ReplayRelayTest.java    From RxRelay with Apache License 2.0 3 votes vote down vote up
@Test
public void hasSubscribers() {
    ReplayRelay<Integer> rp = ReplayRelay.create();

    assertFalse(rp.hasObservers());

    TestObserver<Integer> ts = rp.test();

    assertTrue(rp.hasObservers());

    ts.dispose();

    assertFalse(rp.hasObservers());
}
 
Example 17
Source File: ReplayRelayTest.java    From RxRelay with Apache License 2.0 3 votes vote down vote up
@Test
public void hasSubscribers() {
    ReplayRelay<Integer> rp = ReplayRelay.create();

    assertFalse(rp.hasObservers());

    TestObserver<Integer> ts = rp.test();

    assertTrue(rp.hasObservers());

    ts.dispose();

    assertFalse(rp.hasObservers());
}