Java Code Examples for io.reactivex.rxjava3.subjects.PublishSubject#onNext()

The following examples show how to use io.reactivex.rxjava3.subjects.PublishSubject#onNext() . 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: RxEventSourcesTest.java    From mobius with Apache License 2.0 6 votes vote down vote up
@Test
public void disposePreventsFurtherEvents() throws Exception {
  PublishSubject<Integer> subject = PublishSubject.create();
  EventSource<Integer> source = RxEventSources.fromObservables(subject);
  RecordingConsumer<Integer> consumer = new RecordingConsumer<>();

  Disposable d = source.subscribe(consumer);

  subject.onNext(1);
  subject.onNext(2);
  d.dispose();
  subject.onNext(3);

  consumer.waitForChange(50);
  consumer.assertValues(1, 2);
}
 
Example 2
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 3
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void streamsDoNotShareInstances() {
  PublishSubject<String> subjectA = PublishSubject.create();
  Observable<String> observableA = subjectA.compose(ReplayingShare.<String>instance());
  TestObserver<String> observerA1 = new TestObserver<>();
  observableA.subscribe(observerA1);

  PublishSubject<String> subjectB = PublishSubject.create();
  Observable<String> observableB = subjectB.compose(ReplayingShare.<String>instance());
  TestObserver<String> observerB1 = new TestObserver<>();
  observableB.subscribe(observerB1);

  subjectA.onNext("Foo");
  observerA1.assertValues("Foo");
  subjectB.onNext("Bar");
  observerB1.assertValues("Bar");

  TestObserver<String> observerA2 = new TestObserver<>();
  observableA.subscribe(observerA2);
  observerA2.assertValues("Foo");

  TestObserver<String> observerB2 = new TestObserver<>();
  observableB.subscribe(observerB2);
  observerB2.assertValues("Bar");
}
 
Example 4
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Ignore("No backpressure in Observable")
@Test public void backpressureHonoredWhenCached() {
  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<>(/*0*/);
  observable.subscribe(observer2);
  observer2.assertNoValues();

  subject.onNext("Bar"); // Replace the cached value...
  observer1.assertValues("Foo", "Bar");

  //observer2.requestMore(1); // ...and ensure new requests see it.
  observer2.assertValues("Bar");
}
 
Example 5
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("CheckReturnValue")
@Test public void fatalExceptionDuringReplayThrown() {
  PublishSubject<String> subject = PublishSubject.create();
  Observable<String> observable = subject.compose(ReplayingShare.<String>instance());

  observable.subscribe();
  subject.onNext("Foo");

  Consumer<String> brokenAction = new Consumer<String>() {
    @Override public void accept(String s) {
      throw new OutOfMemoryError("broken!");
    }
  };
  try {
    observable.subscribe(brokenAction);
    fail();
  } catch (OutOfMemoryError e) {
    assertEquals("broken!", e.getMessage());
  }
}
 
Example 6
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 7
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 8
Source File: TransformersTest.java    From mobius with Apache License 2.0 5 votes vote down vote up
@Test
public void effectPerformerInvokesFunctionWithReceivedEffectAndErrorsForUnhandledExceptions() {
  PublishSubject<String> upstream = PublishSubject.create();
  TestScheduler scheduler = new TestScheduler();
  Function<String, Integer> function =
      s -> {
        throw new RuntimeException("Something bad happened");
      };
  TestObserver<Integer> observer =
      upstream.compose(Transformers.fromFunction(function, scheduler)).test();

  upstream.onNext("Hello");
  scheduler.triggerActions();
  observer.assertError(RuntimeException.class);
}
 
Example 9
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 10
Source File: TransformersTest.java    From mobius with Apache License 2.0 5 votes vote down vote up
@Test
public void effectPerformerInvokesFunctionWithReceivedEffectAndEmitsReturnedEvents() {
  PublishSubject<String> upstream = PublishSubject.create();
  TestScheduler scheduler = new TestScheduler();
  Function<String, Integer> function = String::length;
  TestObserver<Integer> observer =
      upstream.compose(Transformers.fromFunction(function, scheduler)).test();

  upstream.onNext("Hello");
  scheduler.triggerActions();
  observer.assertValue(5);
}
 
Example 11
Source File: TransformersTest.java    From mobius with Apache License 2.0 5 votes vote down vote up
@Test
public void effectPerformerInvokesConsumerOnSchedulerAndPassesTheRequestedEffect()
    throws Exception {
  PublishSubject<String> upstream = PublishSubject.create();
  TestConsumer<String> consumer = new TestConsumer<>();
  TestScheduler scheduler = new TestScheduler();
  upstream.compose(Transformers.fromConsumer(consumer, scheduler)).subscribe();

  upstream.onNext("First Time");
  assertThat(consumer.getCurrentValue(), is(equalTo(null)));
  scheduler.triggerActions();
  assertThat(consumer.getCurrentValue(), is("First Time"));
}
 
Example 12
Source File: TransformersTest.java    From mobius with Apache License 2.0 5 votes vote down vote up
@Test
public void effectPerformerInvokesConsumerAndPassesTheRequestedEffect() throws Exception {
  PublishSubject<String> upstream = PublishSubject.create();
  TestConsumer<String> consumer = new TestConsumer<>();
  upstream.compose(Transformers.fromConsumer(consumer)).subscribe();

  upstream.onNext("First Time");
  assertThat(consumer.getCurrentValue(), is("First Time"));

  upstream.onNext("Do it again!");
  assertThat(consumer.getCurrentValue(), is("Do it again!"));
}
 
Example 13
Source File: TransformersTest.java    From mobius with Apache License 2.0 5 votes vote down vote up
@Test
public void effectPerformerRunsActionOnSchedulerWheneverEffectIsRequested() throws Exception {
  PublishSubject<String> upstream = PublishSubject.create();
  TestAction action = new TestAction();
  TestScheduler scheduler = new TestScheduler();
  upstream.compose(Transformers.fromAction(action, scheduler)).subscribe();

  upstream.onNext("First Time");
  assertThat(action.getRunCount(), is(0));
  scheduler.triggerActions();
  assertThat(action.getRunCount(), is(1));
}
 
Example 14
Source File: TransformersTest.java    From mobius with Apache License 2.0 5 votes vote down vote up
@Test
public void effectPerformerRunsActionWheneverEffectIsRequested() throws Exception {
  PublishSubject<String> upstream = PublishSubject.create();
  TestAction action = new TestAction();
  upstream.compose(Transformers.fromAction(action)).subscribe();
  upstream.onNext("First Time");
  assertThat(action.getRunCount(), is(1));

  upstream.onNext("One more!");
  assertThat(action.getRunCount(), is(2));
}
 
Example 15
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 16
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 5 votes vote down vote up
@Test public void defaultValueOnSubscribe() {
  PublishSubject<String> subject = PublishSubject.create();
  Observable<String> observable = subject.compose(ReplayingShare.createWithDefault("default"));

  TestObserver<String> observer1 = new TestObserver<>();
  observable.subscribe(observer1);
  observer1.assertValues("default");

  subject.onNext("Foo");
  observer1.assertValues("default", "Foo");
}
 
Example 17
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 5 votes vote down vote up
@Test public void defaultValueIsOverriddenByLatestEmissionForNewSubscriber() {
  PublishSubject<String> subject = PublishSubject.create();
  Observable<String> observable = subject.compose(ReplayingShare.createWithDefault("default"));

  TestObserver<String> observer1 = new TestObserver<>();
  observable.subscribe(observer1);
  observer1.assertValues("default");

  subject.onNext("Foo");
  observer1.assertValues("default", "Foo");

  TestObserver<String> observer2 = new TestObserver<>();
  observable.subscribe(observer2);
  observer2.assertValues("Foo");
}
 
Example 18
Source File: OutsideLifecycleExceptionTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void eventThrowsBadException() {
    PublishSubject<String> stream = PublishSubject.create();
    PublishSubject<String> lifecycle = PublishSubject.create();

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

    // We get an error from the function for this lifecycle event
    lifecycle.onNext("ick");
    stream.onNext("1");

    testObserver.assertNoValues();

    // We only want to check for our IllegalArgumentException, but may have
    // to wade through a CompositeException to get at it.
    testObserver.assertError(new Predicate<Throwable>() {
        @Override
        public boolean test(Throwable throwable) throws Exception {
            if (throwable instanceof CompositeException) {
                CompositeException ce = (CompositeException) throwable;
                for (Throwable t : ce.getExceptions()) {
                    if (t instanceof IllegalArgumentException) {
                        return true;
                    }
                }
            }

            return false;
        }
    });
}
 
Example 19
Source File: RxConnectables.java    From mobius with Apache License 2.0 4 votes vote down vote up
public static <I, O> Connectable<I, O> fromTransformer(
    @NonNull final ObservableTransformer<I, O> transformer) {
  checkNotNull(transformer);
  return new Connectable<I, O>() {
    @Nonnull
    @Override
    public Connection<I> connect(com.spotify.mobius.functions.Consumer<O> output) {
      final PublishSubject<I> subject = PublishSubject.create();

      final Disposable disposable =
          subject
              .compose(transformer)
              .subscribe(
                  new Consumer<O>() {
                    @Override
                    public void accept(O value) throws Throwable {
                      output.accept(value);
                    }
                  },
                  new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable error) throws Throwable {
                      RxJavaPlugins.onError(error);
                    }
                  },
                  new Action() {
                    @Override
                    public void run() throws Throwable {}
                  });

      return new Connection<I>() {
        public void accept(I effect) {
          subject.onNext(effect);
        }

        @Override
        public void dispose() {
          disposable.dispose();
        }
      };
    }
  };
}