io.reactivex.rxjava3.subscribers.TestSubscriber Java Examples

The following examples show how to use io.reactivex.rxjava3.subscribers.TestSubscriber. 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: ReplayingShareFlowableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void initialValueToNewSubscriberAfterUnsubscribe() {
  PublishProcessor<String> subject = PublishProcessor.create();
  Flowable<String> flowable = subject.compose(ReplayingShare.<String>instance());

  TestSubscriber<String> subscriber1 = new TestSubscriber<>();
  flowable.subscribe(subscriber1);
  subscriber1.assertNoValues();

  subject.onNext("Foo");
  subscriber1.assertValues("Foo");
  subscriber1.cancel();

  TestSubscriber<String> subscriber2 = new TestSubscriber<>();
  flowable.subscribe(subscriber2);
  subscriber2.assertValues("Foo");
}
 
Example #2
Source File: ReplayingShareFlowableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void valueMissedWhenNoSubscribers() {
  PublishProcessor<String> subject = PublishProcessor.create();
  Flowable<String> flowable = subject.compose(ReplayingShare.<String>instance());

  TestSubscriber<String> subscriber1 = new TestSubscriber<>();
  flowable.subscribe(subscriber1);
  subscriber1.assertNoValues();
  subscriber1.cancel();

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

  TestSubscriber<String> subscriber2 = new TestSubscriber<>();
  flowable.subscribe(subscriber2);
  subscriber2.assertNoValues();
}
 
Example #3
Source File: TimeLimiterTransformerPublisherTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void timeoutAfterInitial() throws InterruptedException {
    int timeout = 2;
    int initialDelay = 1;
    int periodDelay = 3;
    given(timeLimiter.getTimeLimiterConfig())
        .willReturn(toConfig(Duration.ofSeconds(timeout)));
    TestSubscriber<Long> subscriber = Flowable
        .interval(initialDelay, periodDelay, TimeUnit.SECONDS)
        .compose(TimeLimiterTransformer.of(timeLimiter))
        .test();

    testScheduler.advanceTimeBy(1, TimeUnit.MINUTES);

    subscriber.await()
        .assertValueCount(1)
        .assertError(TimeoutException.class);
    then(timeLimiter).should()
        .onSuccess();
    then(timeLimiter).should()
        .onError(any(TimeoutException.class));
}
 
Example #4
Source File: ReplayingShareFlowableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void backpressureHonoredWhenCached() {
  PublishProcessor<String> subject = PublishProcessor.create();
  Flowable<String> flowable = subject.compose(ReplayingShare.<String>instance());

  TestSubscriber<String> subscriber1 = new TestSubscriber<>();
  flowable.subscribe(subscriber1);
  subscriber1.assertNoValues();

  subject.onNext("Foo");
  subscriber1.assertValues("Foo");

  TestSubscriber<String> subscriber2 = new TestSubscriber<>(0);
  flowable.subscribe(subscriber2);
  subscriber2.assertNoValues();

  subject.onNext("Bar"); // Replace the cached value...
  subscriber2.request(1); // ...and ensure new requests see it.
  subscriber2.assertValues("Bar");
}
 
Example #5
Source File: ReplayingShareFlowableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void streamsDoNotShareInstances() {
  PublishProcessor<String> subjectA = PublishProcessor.create();
  Flowable<String> flowableA = subjectA.compose(ReplayingShare.<String>instance());
  TestSubscriber<String> subscriberA1 = new TestSubscriber<>();
  flowableA.subscribe(subscriberA1);

  PublishProcessor<String> subjectB = PublishProcessor.create();
  Flowable<String> flowableB = subjectB.compose(ReplayingShare.<String>instance());
  TestSubscriber<String> subscriberB1 = new TestSubscriber<>();
  flowableB.subscribe(subscriberB1);

  subjectA.onNext("Foo");
  subscriberA1.assertValues("Foo");
  subjectB.onNext("Bar");
  subscriberB1.assertValues("Bar");

  TestSubscriber<String> subscriberA2 = new TestSubscriber<>();
  flowableA.subscribe(subscriberA2);
  subscriberA2.assertValues("Foo");

  TestSubscriber<String> subscriberB2 = new TestSubscriber<>();
  flowableB.subscribe(subscriberB2);
  subscriberB2.assertValues("Bar");
}
 
Example #6
Source File: ReplayingShareFlowableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void completeClearsCacheAndResubscribes() {
  List<String> start = new ArrayList<>();
  start.add("initA");

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

  TestSubscriber<String> subscriber1 = new TestSubscriber<>();
  replayed.subscribe(subscriber1);
  subscriber1.assertValues("initA");

  TestSubscriber<String> observer2 = new TestSubscriber<>();
  replayed.subscribe(observer2);
  subscriber1.assertValues("initA");

  upstream.onComplete();
  subscriber1.assertComplete();
  observer2.assertComplete();

  start.set(0, "initB");

  TestSubscriber<String> observer3 = new TestSubscriber<>();
  replayed.subscribe(observer3);
  observer3.assertValues("initB");
}
 
Example #7
Source File: ReplayingShareFlowableTest.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");

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

  TestSubscriber<String> subscriber1 = new TestSubscriber<>();
  replayed.subscribe(subscriber1);
  subscriber1.assertValues("initA");

  TestSubscriber<String> observer2 = new TestSubscriber<>();
  replayed.subscribe(observer2);
  subscriber1.assertValues("initA");

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

  start.set(0, "initB");

  TestSubscriber<String> observer3 = new TestSubscriber<>();
  replayed.subscribe(observer3);
  observer3.assertValues("initB");
}
 
Example #8
Source File: JAXRSRxJava3FlowableTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetHelloWorldJson() throws Exception {
    String address = "http://localhost:" + PORT + "/rx3/flowable/textJson";
    List<Object> providers = new LinkedList<>();
    providers.add(new JacksonJsonProvider());
    providers.add(new FlowableRxInvokerProvider());
    WebClient wc = WebClient.create(address, providers);
    Flowable<HelloWorldBean> obs = wc.accept("application/json")
        .rx(FlowableRxInvoker.class)
        .get(HelloWorldBean.class);

    final TestSubscriber<HelloWorldBean> subscriber = new TestSubscriber<>();
    obs.subscribe(subscriber);

    subscriber.await(3, TimeUnit.SECONDS);
    subscriber.assertResult(new HelloWorldBean("Hello", "World"));
}
 
Example #9
Source File: TimeLimiterTransformerPublisherTest.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)));
    TestSubscriber<Long> subscriber = Flowable.interval(1, TimeUnit.SECONDS)
        .take(2)
        .compose(TimeLimiterTransformer.of(timeLimiter))
        .test();

    testScheduler.advanceTimeBy(1, TimeUnit.MINUTES);

    subscriber.assertValueCount(2)
        .assertComplete();
    then(timeLimiter).should(times(3))
        .onSuccess();
}
 
Example #10
Source File: ReplayingShareFlowableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void completeClearsCacheAndResubscribesStartingWithDefault() {
  List<String> start = new ArrayList<>();
  start.add("initA");

  PublishProcessor<String> upstream = PublishProcessor.create();
  Flowable<String> replayed =
      upstream.startWithIterable(start).compose(ReplayingShare.createWithDefault("default"));

  TestSubscriber<String> subscriber1 = new TestSubscriber<>();
  replayed.subscribe(subscriber1);
  subscriber1.assertValues("default", "initA");

  TestSubscriber<String> observer2 = new TestSubscriber<>();
  replayed.subscribe(observer2);
  subscriber1.assertValues("default", "initA");

  upstream.onComplete();
  subscriber1.assertComplete();
  observer2.assertComplete();

  start.set(0, "initB");

  TestSubscriber<String> observer3 = new TestSubscriber<>();
  replayed.subscribe(observer3);
  observer3.assertValues("default", "initB");
}
 
Example #11
Source File: UntilEventTransformerFlowableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void twoEvents() {
    TestSubscriber<String> testSubscriber = stream
        .compose(RxLifecycle.<String, String>bindUntilEvent(lifecycle, "stop"))
        .test();

    stream.onNext("1");
    lifecycle.onNext("keep going");
    stream.onNext("2");
    lifecycle.onNext("stop");
    stream.onNext("3");

    testSubscriber.assertValues("1", "2");
    testSubscriber.assertComplete();
}
 
Example #12
Source File: UntilCorrespondingEventTransformerFlowableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void oneStartEvent() {
    TestSubscriber<String> testSubscriber = stream
        .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
        .test();

    lifecycle.onNext("create");
    stream.onNext("1");
    stream.onNext("2");

    testSubscriber.assertValues("1", "2");
    testSubscriber.assertNotComplete();
}
 
Example #13
Source File: UntilCorrespondingEventTransformerFlowableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void twoOpenEvents() {
    TestSubscriber<String> testSubscriber = stream
        .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
        .test();

    lifecycle.onNext("create");
    stream.onNext("1");
    lifecycle.onNext("start");
    stream.onNext("2");

    testSubscriber.assertValues("1", "2");
    testSubscriber.assertNotComplete();
}
 
Example #14
Source File: UntilCorrespondingEventTransformerFlowableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void openAndCloseEvent() {
    TestSubscriber<String> testSubscriber = stream
        .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
        .test();

    lifecycle.onNext("create");
    stream.onNext("1");
    lifecycle.onNext("destroy");
    stream.onNext("2");

    testSubscriber.assertValues("1");
    testSubscriber.assertComplete();
}
 
Example #15
Source File: UntilLifecycleTransformerFlowableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void noEvent() {
    TestSubscriber<String> testSubscriber = stream
        .compose(RxLifecycle.<String, String>bind(lifecycle))
        .test();

    stream.onNext("1");
    stream.onNext("2");
    testSubscriber.assertValues("1", "2");
    testSubscriber.assertNotComplete();
}
 
Example #16
Source File: UntilLifecycleTransformerFlowableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void oneEvent() {
    TestSubscriber<String> testSubscriber = stream
        .compose(RxLifecycle.<String, String>bind(lifecycle))
        .test();

    stream.onNext("1");
    lifecycle.onNext("stop");
    stream.onNext("2");

    testSubscriber.assertValues("1");
    testSubscriber.assertComplete();
}
 
Example #17
Source File: UntilEventTransformerFlowableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void noEvents() {
    TestSubscriber<String> testSubscriber = stream
        .compose(RxLifecycle.<String, String>bindUntilEvent(lifecycle, "stop"))
        .test();

    stream.onNext("1");
    stream.onNext("2");
    testSubscriber.assertValues("1", "2");
    testSubscriber.assertNotComplete();
}
 
Example #18
Source File: UntilEventTransformerFlowableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void oneWrongEvent() {
    TestSubscriber<String> testSubscriber = stream
        .compose(RxLifecycle.<String, String>bindUntilEvent(lifecycle, "stop"))
        .test();

    stream.onNext("1");
    lifecycle.onNext("keep going");
    stream.onNext("2");

    testSubscriber.assertValues("1", "2");
    testSubscriber.assertNotComplete();
}
 
Example #19
Source File: TimeLimiterTransformerPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void timeout() {
    given(timeLimiter.getTimeLimiterConfig())
        .willReturn(toConfig(Duration.ZERO));
    TestSubscriber<Long> subscriber = Flowable.interval(1, TimeUnit.MINUTES)
        .compose(TimeLimiterTransformer.of(timeLimiter))
        .test();

    testScheduler.advanceTimeBy(1, TimeUnit.MINUTES);

    subscriber.assertError(TimeoutException.class);
    then(timeLimiter).should()
        .onError(any(TimeoutException.class));
}
 
Example #20
Source File: JAXRSRxJava3FlowableTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetHelloWorldAsyncObservable() throws Exception {
    String address = "http://localhost:" + PORT + "/rx3/flowable/textAsync";
    WebClient wc = WebClient.create(address,
                                    Collections.singletonList(new FlowableRxInvokerProvider()));
    Flowable<String> obs = wc.accept("text/plain")
        .rx(FlowableRxInvoker.class)
        .get(String.class);

    final TestSubscriber<String> subscriber = new TestSubscriber<>();
    obs.map(s -> s + s).subscribe(subscriber);
    
    subscriber.await(2, TimeUnit.SECONDS);
    subscriber.assertResult("Hello, world!Hello, world!");
}
 
Example #21
Source File: JAXRSRxJava3FlowableTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetHelloWorldAsyncObservable404() throws Exception {
    String address = "http://localhost:" + PORT + "/rx3/flowable/textAsync404";
    Invocation.Builder b = ClientBuilder.newClient().register(new FlowableRxInvokerProvider())
        .target(address).request();

    final TestSubscriber<String> subscriber = new TestSubscriber<>();
    b.rx(FlowableRxInvoker.class)
        .get(String.class)
        .subscribe(subscriber);
    
    subscriber.await(1, TimeUnit.SECONDS);
    subscriber.assertError(NotFoundException.class);
}
 
Example #22
Source File: UntilCorrespondingEventTransformerFlowableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void noEvents() {
    TestSubscriber<String> testSubscriber = stream
        .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
        .test();

    stream.onNext("1");
    stream.onNext("2");
    testSubscriber.assertValues("1", "2");
    testSubscriber.assertNotComplete();
}
 
Example #23
Source File: TimeLimiterTransformerPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void doNotTimeoutEmpty() {
    given(timeLimiter.getTimeLimiterConfig())
        .willReturn(toConfig(Duration.ofMinutes(1)));

    TestSubscriber<Object> subscriber = Flowable.empty()
        .compose(TimeLimiterTransformer.of(timeLimiter))
        .test();

    subscriber.assertComplete();
    then(timeLimiter).should()
        .onSuccess();
}
 
Example #24
Source File: TimeLimiterTransformerPublisherTest.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));
    TestSubscriber<Object> subscriber = Flowable.empty()
        .delay(1, TimeUnit.MINUTES)
        .compose(TimeLimiterTransformer.of(timeLimiter))
        .test();

    testScheduler.advanceTimeBy(1, TimeUnit.MINUTES);

    subscriber.assertError(TimeoutException.class);
    then(timeLimiter).should()
        .onError(any(TimeoutException.class));
}
 
Example #25
Source File: ReplayingShareFlowableTest.java    From RxReplayingShare with Apache License 2.0 5 votes vote down vote up
@Test public void noInitialValue() {
  PublishProcessor<String> subject = PublishProcessor.create();
  Flowable<String> flowable = subject.compose(ReplayingShare.<String>instance());

  TestSubscriber<String> subscriber = new TestSubscriber<>();
  flowable.subscribe(subscriber);
  subscriber.assertNoValues();
}
 
Example #26
Source File: TimeLimiterTransformerPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void otherError() {
    given(timeLimiter.getTimeLimiterConfig())
        .willReturn(toConfig(Duration.ZERO));
    TestSubscriber<Object> subscriber = Flowable.error(new RuntimeException())
        .compose(TimeLimiterTransformer.of(timeLimiter))
        .test();

    testScheduler.advanceTimeBy(1, TimeUnit.MINUTES);

    subscriber.assertError(RuntimeException.class);
    then(timeLimiter).should()
        .onError(any(RuntimeException.class));
}
 
Example #27
Source File: ReplayingShareFlowableTest.java    From RxReplayingShare with Apache License 2.0 5 votes vote down vote up
@Test public void errorClearsCacheAndResubscribesStartingWithDefault() {
  List<String> start = new ArrayList<>();
  start.add("initA");

  PublishProcessor<String> upstream = PublishProcessor.create();
  Flowable<String> replayed =
      upstream.startWithIterable(start).compose(ReplayingShare.createWithDefault("default"));

  TestSubscriber<String> subscriber1 = new TestSubscriber<>();
  replayed.subscribe(subscriber1);
  subscriber1.assertValues("default", "initA");

  TestSubscriber<String> observer2 = new TestSubscriber<>();
  replayed.subscribe(observer2);
  subscriber1.assertValues("default", "initA");

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

  start.set(0, "initB");

  TestSubscriber<String> observer3 = new TestSubscriber<>();
  replayed.subscribe(observer3);
  observer3.assertValues("default", "initB");
}
 
Example #28
Source File: ReplayingShareFlowableTest.java    From RxReplayingShare with Apache License 2.0 5 votes vote down vote up
@Test public void defaultValueIsOverriddenByLatestEmissionForNewSubscriber() {
  PublishProcessor<String> subject = PublishProcessor.create();
  Flowable<String> flowable = subject.compose(ReplayingShare.createWithDefault("default"));

  TestSubscriber<String> subscriber1 = new TestSubscriber<>();
  flowable.subscribe(subscriber1);
  subscriber1.assertValues("default");

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

  TestSubscriber<String> observer2 = new TestSubscriber<>();
  flowable.subscribe(observer2);
  observer2.assertValues("Foo");
}
 
Example #29
Source File: ReplayingShareFlowableTest.java    From RxReplayingShare with Apache License 2.0 5 votes vote down vote up
@Test public void defaultValueOnSubscribe() {
  PublishProcessor<String> subject = PublishProcessor.create();
  Flowable<String> flowable = subject.compose(ReplayingShare.createWithDefault("default"));

  TestSubscriber<String> subscriber1 = new TestSubscriber<>();
  flowable.subscribe(subscriber1);
  subscriber1.assertValues("default");

  subject.onNext("Foo");
  subscriber1.assertValues("default", "Foo");
}
 
Example #30
Source File: ReplayingShareFlowableTest.java    From RxReplayingShare with Apache License 2.0 5 votes vote down vote up
@Test public void unsubscribeBeforeSubscribePreventsCacheEmission() {
  PublishProcessor<String> upstream = PublishProcessor.create();
  Flowable<String> replayed = upstream.compose(ReplayingShare.<String>instance());
  replayed.subscribe();
  upstream.onNext("something to cache");

  TestSubscriber<String> testSubscriber = new TestSubscriber<>();
  testSubscriber.cancel();
  replayed.subscribe(testSubscriber);
  testSubscriber.assertNoValues();
}