io.reactivex.rxjava3.observers.TestObserver Java Examples

The following examples show how to use io.reactivex.rxjava3.observers.TestObserver. 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: AndroidLifecycleFragmentTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testBindUntilEvent(LifecycleOwner owner) {
    Fragment fragment = (Fragment) owner;
    ActivityController<?> controller = startFragment(fragment);

    TestObserver<Object> testObserver = observable.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(Lifecycle.Event.ON_STOP)).test();

    testObserver.assertNotComplete();
    controller.start();
    testObserver.assertNotComplete();
    controller.resume();
    testObserver.assertNotComplete();
    controller.pause();
    testObserver.assertNotComplete();
    controller.stop();
    testObserver.assertComplete();
}
 
Example #2
Source File: MobiusEffectRouterTest.java    From mobius with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  cConsumer = new TestConsumer<>();
  dAction = new TestAction();

  ObservableTransformer<TestEffect, TestEvent> router =
      RxMobius.<TestEffect, TestEvent>subtypeEffectHandler()
          .addTransformer(A.class, (Observable<A> as) -> as.map(a -> AEvent.create(a.id())))
          .addTransformer(B.class, (Observable<B> bs) -> bs.map(b -> BEvent.create(b.id())))
          .addConsumer(C.class, cConsumer)
          .addAction(D.class, dAction)
          .addFunction(E.class, e -> AEvent.create(e.id()))
          .build();

  publishSubject = PublishSubject.create();
  testSubscriber = TestObserver.create();

  publishSubject.compose(router).subscribe(testSubscriber);
}
 
Example #3
Source File: AndroidLifecycleActivityTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testBindUntilEvent(ActivityController<? extends LifecycleOwner> controller) {
    LifecycleProvider<Lifecycle.Event> activity = AndroidLifecycle.createLifecycleProvider(controller.get());

    TestObserver<Object> testObserver = observable.compose(activity.bindUntilEvent(Lifecycle.Event.ON_STOP)).test();

    controller.create();
    testObserver.assertNotComplete();
    controller.start();
    testObserver.assertNotComplete();
    controller.resume();
    testObserver.assertNotComplete();
    controller.pause();
    testObserver.assertNotComplete();
    controller.stop();
    testObserver.assertComplete();
}
 
Example #4
Source File: RxMobiusLoopTest.java    From mobius with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSupportStartingALoopWithAnInit() throws Exception {
  MobiusLoop.Builder<String, Integer, Boolean> withInit =
      builder.init(
          new Init<String, Boolean>() {
            @Nonnull
            @Override
            public First<String, Boolean> init(String model) {
              return First.first(model + "-init");
            }
          });

  ObservableTransformer<Integer, String> transformer = RxMobius.loopFrom(withInit, "hi");

  final TestObserver<String> observer = Observable.just(10).compose(transformer).test();

  observer.awaitCount(2);
  observer.assertValues("hi-init", "hi-init10");
}
 
Example #5
Source File: RxSupportFragmentLifecycleTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testBindUntilEvent(LifecycleProvider<FragmentEvent> provider) {
    Fragment fragment = (Fragment) provider;
    startFragment(fragment);

    TestObserver<Object> testObserver = observable.compose(provider.bindUntilEvent(STOP)).test();

    fragment.onAttach(null);
    testObserver.assertNotComplete();
    fragment.onCreate(null);
    testObserver.assertNotComplete();
    fragment.onViewCreated(null, null);
    testObserver.assertNotComplete();
    fragment.onStart();
    testObserver.assertNotComplete();
    fragment.onResume();
    testObserver.assertNotComplete();
    fragment.onPause();
    testObserver.assertNotComplete();
    fragment.onStop();
    testObserver.assertComplete();
}
 
Example #6
Source File: TimeLimiterTransformerObservableTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotTimeout() {
    given(timeLimiter.getTimeLimiterConfig())
        .willReturn(toConfig(Duration.ofMinutes(1)));
    TestObserver<?> observer = Observable.interval(1, TimeUnit.SECONDS)
        .take(2)
        .compose(TimeLimiterTransformer.of(timeLimiter))
        .test();

    testScheduler.advanceTimeBy(1, TimeUnit.MINUTES);

    observer.assertValueCount(2)
        .assertComplete();
    then(timeLimiter).should(times(3))
        .onSuccess();
}
 
Example #7
Source File: UntilLifecycleTransformerSingleTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
@Test
public void oneEvent() {
    TestObserver<String> testObserver = Single.just("1")
        .delay(1, TimeUnit.MILLISECONDS, testScheduler)
        .compose(RxLifecycle.<String, String>bind(lifecycle))
        .test();

    testObserver.assertNoValues();
    testObserver.assertNoErrors();

    lifecycle.onNext("stop");
    testScheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);

    testObserver.assertNoValues();
    testObserver.assertError(CancellationException.class);
}
 
Example #8
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 #9
Source File: TimeLimiterTransformerCompletableTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotTimeout() {
    given(helloWorldService.returnHelloWorld())
        .willReturn("hello");
    given(timeLimiter.getTimeLimiterConfig())
        .willReturn(toConfig(Duration.ofMinutes(1)));
    TestObserver<?> observer = Completable.fromRunnable(helloWorldService::returnHelloWorld)
        .compose(TimeLimiterTransformer.of(timeLimiter))
        .test();

    testScheduler.advanceTimeBy(1, TimeUnit.MINUTES);

    observer.assertComplete();
    then(timeLimiter).should()
        .onSuccess();
}
 
Example #10
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 #11
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 #12
Source File: RxLifecycleTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
@Test
public void testBindUntilLifecycleEvent() {
    BehaviorSubject<Lifecycle.Event> lifecycle = BehaviorSubject.create();

    TestObserver<Object> testObserver =
            observable.compose(RxLifecycle.bindUntilEvent(lifecycle, Lifecycle.Event.ON_STOP)).test();

    lifecycle.onNext(Lifecycle.Event.ON_CREATE);
    testObserver.assertNotComplete();
    lifecycle.onNext(Lifecycle.Event.ON_START);
    testObserver.assertNotComplete();
    lifecycle.onNext(Lifecycle.Event.ON_RESUME);
    testObserver.assertNotComplete();
    lifecycle.onNext(Lifecycle.Event.ON_PAUSE);
    testObserver.assertNotComplete();
    lifecycle.onNext(Lifecycle.Event.ON_STOP);
    testObserver.assertComplete();
}
 
Example #13
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 #14
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 #15
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void errorClearsCacheAndResubscribes() {
  List<String> start = new ArrayList<>();
  start.add("initA");

  PublishSubject<String> upstream = PublishSubject.create();
  Observable<String> replayed = upstream.startWithIterable(start).compose(ReplayingShare.<String>instance());

  TestObserver<String> observer1 = new TestObserver<>();
  replayed.subscribe(observer1);
  observer1.assertValues("initA");

  TestObserver<String> observer2 = new TestObserver<>();
  replayed.subscribe(observer2);
  observer1.assertValues("initA");

  RuntimeException r = new RuntimeException();
  upstream.onError(r);
  observer1.assertError(r);
  observer2.assertError(r);

  start.set(0, "initB");

  TestObserver<String> observer3 = new TestObserver<>();
  replayed.subscribe(observer3);
  observer3.assertValues("initB");
}
 
Example #16
Source File: OutsideLifecycleExceptionTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
@Test
public void eventOutOfLifecycle() {
    PublishSubject<String> stream = PublishSubject.create();
    PublishSubject<String> lifecycle = PublishSubject.create();

    TestObserver<String> testObserver = stream
        .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
        .test();

    // Event is out of lifecycle, but this just results in completing the stream
    lifecycle.onNext("destroy");
    stream.onNext("1");

    testObserver.assertNoValues();
    testObserver.assertComplete();
}
 
Example #17
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 #18
Source File: RxFragmentLifecycleTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testBindUntilEvent(LifecycleProvider<FragmentEvent> provider) {
    Fragment fragment = (Fragment) provider;
    startFragment(fragment);

    TestObserver<Object> testObserver = observable.compose(provider.bindUntilEvent(STOP)).test();

    fragment.onAttach(null);
    testObserver.assertNotComplete();
    fragment.onCreate(null);
    testObserver.assertNotComplete();
    fragment.onViewCreated(null, null);
    testObserver.assertNotComplete();
    fragment.onStart();
    testObserver.assertNotComplete();
    fragment.onResume();
    testObserver.assertNotComplete();
    fragment.onPause();
    testObserver.assertNotComplete();
    fragment.onStop();
    testObserver.assertComplete();
}
 
Example #19
Source File: RxMobiusLoopTest.java    From mobius with Apache License 2.0 5 votes vote down vote up
@Test
public void startModelAndEffects() {
  RxMobiusLoop<Integer, String, Boolean> loop =
      new RxMobiusLoop<>(builder, "StartModel", ImmutableSet.of(true, false));
  final TestObserver<String> testObserver = Observable.just(1).compose(loop).test();

  testObserver.awaitCount(2);
  testObserver.assertValues("StartModel", "StartModel1");
  testObserver.assertNoErrors();
  assertEquals(2, connection.valueCount());
  connection.assertValuesInAnyOrder(true, false);
}
 
Example #20
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 5 votes vote down vote up
@Test public void initialValueToNewSubscriber() {
  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");

  TestObserver<String> observer2 = new TestObserver<>();
  observable.subscribe(observer2);
  observer2.assertValues("Foo");
}
 
Example #21
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 5 votes vote down vote up
@Test public void noInitialValue() {
  PublishSubject<String> subject = PublishSubject.create();
  Observable<String> observable = subject.compose(ReplayingShare.<String>instance());

  TestObserver<String> observer = new TestObserver<>();
  observable.subscribe(observer);
  observer.assertNoValues();
}
 
Example #22
Source File: CountriesServiceSolvedTest.java    From RxBasicsKata with MIT License 5 votes vote down vote up
@Test
public void rx_ListNameOfEachCountry() {
    List<String> expectedResult = CountriesTestProvider.namesOfCountries();
    TestObserver<String> testObserver = countriesService
            .listNameOfEachCountry(allCountries)
            .test();
    testObserver.assertValueSequence(expectedResult);
    testObserver.assertNoErrors();
}
 
Example #23
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 5 votes vote down vote up
@Test public void refCountToUpstream() {
  PublishSubject<String> subject = PublishSubject.create();

  final AtomicInteger count = new AtomicInteger();
  Observable<String> observable = subject //
      .doOnSubscribe(new Consumer<Disposable>() {
        @Override public void accept(Disposable disposable) throws Exception {
          count.incrementAndGet();
        }
      }) //
      .doOnDispose(new Action() {
        @Override public void run() throws Exception {
          count.decrementAndGet();
        }
      }) //
      .compose(ReplayingShare.<String>instance());

  Disposable disposable1 = observable.subscribeWith(new TestObserver<String>());
  assertEquals(1, count.get());

  Disposable disposable2 = observable.subscribeWith(new TestObserver<String>());
  assertEquals(1, count.get());

  Disposable disposable3 = observable.subscribeWith(new TestObserver<String>());
  assertEquals(1, count.get());

  disposable1.dispose();
  assertEquals(1, count.get());

  disposable3.dispose();
  assertEquals(1, count.get());

  disposable2.dispose();
  assertEquals(0, count.get());
}
 
Example #24
Source File: UntilLifecycleTransformerMaybeTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void noEvent() {
    TestObserver<String> testObserver = maybe
        .compose(RxLifecycle.<String, String>bind(lifecycle))
        .test();

    stream.onNext("1");
    testObserver.assertValue("1");
    testObserver.assertComplete();
}
 
Example #25
Source File: UntilCorrespondingEventTransformerCompletableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void openAndCloseEvent() {
    TestObserver<Void> testObserver = completable
        .compose(RxLifecycle.bind(lifecycle, CORRESPONDING_EVENTS))
        .test();

    lifecycle.onNext("create");
    lifecycle.onNext("destroy");
    subject.onComplete();
    testObserver.assertError(CancellationException.class);
}
 
Example #26
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 #27
Source File: UntilCorrespondingEventTransformerMaybeTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void oneStartEvent() {
    TestObserver<String> testObserver = maybe
        .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
        .test();

    lifecycle.onNext("create");
    subject.onNext("1");
    testObserver.assertValue("1");
    testObserver.assertComplete();
}
 
Example #28
Source File: TimeLimiterTransformerObservableTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void timeoutEmpty() {
    given(timeLimiter.getTimeLimiterConfig())
        .willReturn(toConfig(Duration.ZERO));
    TestObserver<?> observer = Observable.empty()
        .delay(1, TimeUnit.MINUTES)
        .compose(TimeLimiterTransformer.of(timeLimiter))
        .test();

    testScheduler.advanceTimeBy(1, TimeUnit.MINUTES);

    observer.assertError(TimeoutException.class);
    then(timeLimiter).should()
        .onError(any(TimeoutException.class));
}
 
Example #29
Source File: UntilEventTransformerMaybeTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void twoEvents() {
    TestObserver<String> testObserver = maybe
        .compose(RxLifecycle.<String, String>bindUntilEvent(lifecycle, "stop"))
        .test();

    lifecycle.onNext("keep going");
    lifecycle.onNext("stop");

    subject.onNext("1");
    testObserver.assertNoValues();
    testObserver.assertComplete();
}
 
Example #30
Source File: UntilCorrespondingEventTransformerMaybeTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void twoOpenEvents() {
    TestObserver<String> testObserver = maybe
        .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
        .test();

    lifecycle.onNext("create");
    lifecycle.onNext("start");
    subject.onNext("1");
    testObserver.assertValue("1");
    testObserver.assertComplete();
}