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

The following examples show how to use io.reactivex.rxjava3.observers.TestObserver#create() . 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: 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 2
Source File: MobiusEffectRouterTest.java    From mobius with Apache License 2.0 6 votes vote down vote up
@Test
public void effectHandlersShouldBeImmutable() throws Exception {
  // redo some test setup for test case specific conditions
  publishSubject = PublishSubject.create();
  testSubscriber = TestObserver.create();

  RxMobius.SubtypeEffectHandlerBuilder<TestEffect, TestEvent> builder =
      RxMobius.<TestEffect, TestEvent>subtypeEffectHandler()
          .addTransformer(A.class, (Observable<A> as) -> as.map(a -> AEvent.create(a.id())));

  ObservableTransformer<TestEffect, TestEvent> router = builder.build();

  // this should not lead to the effects router being capable of handling B effects
  builder.addTransformer(B.class, bs -> bs.map(b -> BEvent.create(b.id())));

  publishSubject.compose(router).subscribe(testSubscriber);

  B effect = B.create(84);
  publishSubject.onNext(effect);
  publishSubject.onComplete();

  testSubscriber.await();
  testSubscriber.assertError(new UnknownEffectException(effect));
}
 
Example 3
Source File: MobiusEffectRouterTest.java    From mobius with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleNullRxJavaErrorHandler() throws Exception {
  // given no RxJava error handler
  RxJavaPlugins.setErrorHandler(null);

  // and a router with a broken effect handler
  publishSubject = PublishSubject.create();
  testSubscriber = TestObserver.create();

  final RuntimeException expected = new RuntimeException("expected!");
  ObservableTransformer<TestEffect, TestEvent> router =
      RxMobius.<TestEffect, TestEvent>subtypeEffectHandler()
          .addFunction(
              A.class,
              a -> {
                throw expected;
              })
          .build();

  publishSubject.compose(router).subscribe(testSubscriber);

  // when an event is sent, it doesn't crash (the exception does get printed to stderr)
  publishSubject.onNext(A.create(1));

  // and the right exception is forwarded to the test subscriber
  testSubscriber.assertError(t -> t == expected);
}
 
Example 4
Source File: MobiusEffectRouterTest.java    From mobius with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldSupportCustomErrorHandler() throws Exception {
  // redo some test setup for test case specific conditions
  publishSubject = PublishSubject.create();
  testSubscriber = TestObserver.create();

  final RuntimeException expectedException = new RuntimeException("expected!");
  final AtomicBoolean gotRightException = new AtomicBoolean(false);

  RxMobius.SubtypeEffectHandlerBuilder<TestEffect, TestEvent> builder =
      RxMobius.<TestEffect, TestEvent>subtypeEffectHandler()
          .addFunction(
              A.class,
              a -> {
                throw expectedException;
              })
          .withFatalErrorHandler(
              new Function<
                  ObservableTransformer<? extends TestEffect, TestEvent>, Consumer<Throwable>>() {
                @Override
                public Consumer<Throwable> apply(
                    ObservableTransformer<? extends TestEffect, TestEvent> testEventTransformer) {
                  return new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                      if (throwable.equals(expectedException)) {
                        gotRightException.set(true);
                      } else {
                        throwable.printStackTrace();
                        Assert.fail("got the wrong exception!");
                      }
                    }
                  };
                }
              });

  ObservableTransformer<TestEffect, TestEvent> router = builder.build();

  publishSubject.compose(router).subscribe(testSubscriber);

  publishSubject.onNext(A.create(1));

  assertThat(gotRightException.get(), is(true));

  testSubscriber.await();
  testSubscriber.assertError(expectedException);
}