io.reactivex.rxjava3.core.Observer Java Examples

The following examples show how to use io.reactivex.rxjava3.core.Observer. 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: ExtentSparkReporter.java    From extentreports-java with Apache License 2.0 6 votes vote down vote up
@Override
public Observer<ReportEntity> getReportObserver() {
    return new Observer<ReportEntity>() {
        @Override
        public void onSubscribe(Disposable d) {
            start(d);
        }

        @Override
        public void onNext(ReportEntity value) {
            flush(value);
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onComplete() {
        }
    };
}
 
Example #2
Source File: JsonFormatter.java    From extentreports-java with Apache License 2.0 6 votes vote down vote up
@Override
public Observer<ReportEntity> getReportObserver() {
    return new Observer<ReportEntity>() {
        @Override
        public void onSubscribe(Disposable d) {
        }

        @Override
        public void onNext(ReportEntity value) {
            flush(value);
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onComplete() {
        }
    };
}
 
Example #3
Source File: BehaviorRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void testThatSubscriberReceivesLatestAndThenSubsequentEvents() {
    BehaviorRelay<String> subject = BehaviorRelay.createDefault("default");

    subject.accept("one");

    Observer<String> observer = TestHelper.mockObserver();
    subject.subscribe(observer);

    subject.accept("two");
    subject.accept("three");

    verify(observer, Mockito.never()).onNext("default");
    verify(observer, times(1)).onNext("one");
    verify(observer, times(1)).onNext("two");
    verify(observer, times(1)).onNext("three");
    verify(observer, Mockito.never()).onError(any(Throwable.class));
    verify(observer, Mockito.never()).onComplete();
}
 
Example #4
Source File: BehaviorRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void testThatSubscriberReceivesDefaultValueAndSubsequentEvents() {
    BehaviorRelay<String> subject = BehaviorRelay.createDefault("default");

    Observer<String> observer = TestHelper.mockObserver();
    subject.subscribe(observer);

    subject.accept("one");
    subject.accept("two");
    subject.accept("three");

    verify(observer, times(1)).onNext("default");
    verify(observer, times(1)).onNext("one");
    verify(observer, times(1)).onNext("two");
    verify(observer, times(1)).onNext("three");
    verify(observer, Mockito.never()).onError(any(Throwable.class));
    verify(observer, Mockito.never()).onComplete();
}
 
Example #5
Source File: ReportEntityObserverTest.java    From extentreports-java with Apache License 2.0 6 votes vote down vote up
@Override
public Observer<ReportEntity> getReportObserver() {
    return new Observer<ReportEntity>() {
        @Override
        public void onSubscribe(Disposable d) {
            disp = d;
        }

        @Override
        public void onNext(ReportEntity value) {
            entity = value;
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onComplete() {
        }
    };
}
 
Example #6
Source File: BehaviorRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void testThatSubscriberReceivesLatestAndThenSubsequentEvents() {
    BehaviorRelay<String> subject = BehaviorRelay.createDefault("default");

    subject.accept("one");

    Observer<String> observer = TestHelper.mockObserver();
    subject.subscribe(observer);

    subject.accept("two");
    subject.accept("three");

    verify(observer, Mockito.never()).onNext("default");
    verify(observer, times(1)).onNext("one");
    verify(observer, times(1)).onNext("two");
    verify(observer, times(1)).onNext("three");
    verify(observer, Mockito.never()).onError(any(Throwable.class));
    verify(observer, Mockito.never()).onComplete();
}
 
Example #7
Source File: RxJava3Test.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
private static <T>Observer<T> observer(final String name, final List<T> result) {
  return new Observer<T>() {
    @Override
    public void onSubscribe(final Disposable d) {
    }

    @Override
    public void onNext(final T next) {
      logger.fine(name + ": " + next);
      result.add(next);
    }

    @Override
    public void onError(final Throwable e) {
      e.printStackTrace();
    }

    @Override
    public void onComplete() {
      logger.fine(name + ": onComplete");
    }
  };
}
 
Example #8
Source File: BehaviorRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void testThatSubscriberReceivesDefaultValueAndSubsequentEvents() {
    BehaviorRelay<String> subject = BehaviorRelay.createDefault("default");

    Observer<String> observer = TestHelper.mockObserver();
    subject.subscribe(observer);

    subject.accept("one");
    subject.accept("two");
    subject.accept("three");

    verify(observer, times(1)).onNext("default");
    verify(observer, times(1)).onNext("one");
    verify(observer, times(1)).onNext("two");
    verify(observer, times(1)).onNext("three");
    verify(observer, Mockito.never()).onError(any(Throwable.class));
    verify(observer, Mockito.never()).onComplete();
}
 
Example #9
Source File: BehaviorRelayTest.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 1000)
public void testUnsubscriptionCase() {
    BehaviorRelay<String> src = BehaviorRelay.createDefault("null"); // FIXME was plain null which is not allowed

    for (int i = 0; i < 10; i++) {
        final Observer<Object> o = TestHelper.mockObserver();
        InOrder inOrder = inOrder(o);
        String v = "" + i;
        src.accept(v);
        System.out.printf("Turn: %d%n", i);
        src.firstElement()
            .toObservable()
            .flatMap(new Function<String, Observable<String>>() {

                @Override
                public Observable<String> apply(String t1) {
                    return Observable.just(t1 + ", " + t1);
                }
            })
            .subscribe(new DefaultObserver<String>() {
                @Override
                public void onNext(String t) {
                    o.onNext(t);
                }

                @Override
                public void onError(Throwable e) {
                    o.onError(e);
                }

                @Override
                public void onComplete() {
                    o.onComplete();
                }
            });
        inOrder.verify(o).onNext(v + ", " + v);
        inOrder.verify(o).onComplete();
        verify(o, never()).onError(any(Throwable.class));
    }
}
 
Example #10
Source File: BehaviorRelay.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(Observer<? super T> observer) {
    BehaviorDisposable<T> bs = new BehaviorDisposable<T>(observer, this);
    observer.onSubscribe(bs);
    add(bs);
    if (bs.cancelled) {
        remove(bs);
    } else {
        bs.emitFirst();
    }
}
 
Example #11
Source File: BehaviorRelayTest.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Test
public void testTakeOneSubscriber() {
    BehaviorRelay<Integer> source = BehaviorRelay.createDefault(1);
    final Observer<Object> o = TestHelper.mockObserver();

    source.take(1).subscribe(o);

    verify(o).onNext(1);
    verify(o).onComplete();
    verify(o, never()).onError(any(Throwable.class));

    assertEquals(0, source.subscriberCount());
    assertFalse(source.hasObservers());
}
 
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: PublishRelay.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(Observer<? super T> t) {
    PublishDisposable<T> ps = new PublishDisposable<T>(t, this);
    t.onSubscribe(ps);
    add(ps);
    // if cancellation happened while a successful add, the remove() didn't work
    // so we need to do it again
    if (ps.isDisposed()) {
        remove(ps);
    }
}
 
Example #14
Source File: BehaviorRelayTest.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Test
public void innerDisposed() {
    BehaviorRelay.create()
    .subscribe(new Observer<Object>() {
        @Override
        public void onSubscribe(Disposable d) {
            assertFalse(d.isDisposed());

            d.dispose();

            assertTrue(d.isDisposed());
        }

        @Override
        public void onNext(Object value) {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    });
}
 
Example #15
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 #16
Source File: BehaviorRelayTest.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Test
public void innerDisposed() {
    BehaviorRelay.create()
    .subscribe(new Observer<Object>() {
        @Override
        public void onSubscribe(Disposable d) {
            assertFalse(d.isDisposed());

            d.dispose();

            assertTrue(d.isDisposed());
        }

        @Override
        public void onNext(Object value) {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    });
}
 
Example #17
Source File: BehaviorRelayTest.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 1000)
public void testUnsubscriptionCase() {
    BehaviorRelay<String> src = BehaviorRelay.createDefault("null"); // FIXME was plain null which is not allowed

    for (int i = 0; i < 10; i++) {
        final Observer<Object> o = TestHelper.mockObserver();
        InOrder inOrder = inOrder(o);
        String v = "" + i;
        src.accept(v);
        System.out.printf("Turn: %d%n", i);
        src.firstElement()
            .toObservable()
            .flatMap(new Function<String, Observable<String>>() {

                @Override
                public Observable<String> apply(String t1) {
                    return Observable.just(t1 + ", " + t1);
                }
            })
            .subscribe(new DefaultObserver<String>() {
                @Override
                public void onNext(String t) {
                    o.onNext(t);
                }

                @Override
                public void onError(Throwable e) {
                    o.onError(e);
                }

                @Override
                public void onComplete() {
                    o.onComplete();
                }
            });
        inOrder.verify(o).onNext(v + ", " + v);
        inOrder.verify(o).onComplete();
        verify(o, never()).onError(any(Throwable.class));
    }
}
 
Example #18
Source File: BehaviorRelayTest.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Test
public void testTakeOneSubscriber() {
    BehaviorRelay<Integer> source = BehaviorRelay.createDefault(1);
    final Observer<Object> o = TestHelper.mockObserver();

    source.take(1).subscribe(o);

    verify(o).onNext(1);
    verify(o).onComplete();
    verify(o, never()).onError(any(Throwable.class));

    assertEquals(0, source.subscriberCount());
    assertFalse(source.hasObservers());
}
 
Example #19
Source File: ReplayRelay.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(Observer<? super T> observer) {
    ReplayDisposable<T> rs = new ReplayDisposable<T>(observer, this);
    observer.onSubscribe(rs);

    if (!rs.cancelled) {
        if (add(rs)) {
            if (rs.cancelled) {
                remove(rs);
                return;
            }
        }
        buffer.replay(rs);
    }
}
 
Example #20
Source File: ObserverCircuitBreaker.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(Observer<? super T> downstream) {
    if (circuitBreaker.tryAcquirePermission()) {
        upstream.subscribe(new CircuitBreakerObserver(downstream));
    } else {
        downstream.onSubscribe(EmptyDisposable.INSTANCE);
        downstream.onError(createCallNotPermittedException(circuitBreaker));
    }
}
 
Example #21
Source File: ObserverBulkhead.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(Observer<? super T> downstream) {
    if (bulkhead.tryAcquirePermission()) {
        upstream.subscribe(new BulkheadObserver(downstream));
    } else {
        downstream.onSubscribe(EmptyDisposable.INSTANCE);
        downstream.onError(BulkheadFullException.createBulkheadFullException(bulkhead));
    }
}
 
Example #22
Source File: ObserverRateLimiter.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(Observer<? super T> downstream) {
    long waitDuration = rateLimiter.reservePermission();
    if (waitDuration >= 0) {
        if (waitDuration > 0) {
            Completable.timer(waitDuration, TimeUnit.NANOSECONDS)
                .subscribe(() -> upstream.subscribe(new RateLimiterObserver(downstream)));
        } else {
            upstream.subscribe(new RateLimiterObserver(downstream));
        }
    } else {
        downstream.onSubscribe(EmptyDisposable.INSTANCE);
        downstream.onError(createRequestNotPermitted(rateLimiter));
    }
}
 
Example #23
Source File: RxJava3AgentIntercept.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static Object enter(final Object thiz, final int argc, final Object arg0, final Object arg1, final Object arg2) {
  if (arg0 == null || arg0.getClass().getName().startsWith("io.reactivex.rxjava3.internal") || arg0 instanceof TracingConsumer)
    return NULL;

  if (!isTracingEnabled) {
    isTracingEnabled = true;
    TracingRxJava3Utils.enableTracing();
  }

  if (arg0 instanceof Observer)
    return new TracingObserver<>((Observer<?>)arg0, "observer", GlobalTracer.get());

  if (!(arg0 instanceof Consumer))
    return NULL;

  final TracingConsumer<Object> tracingConsumer;
  if (argc == 1)
    tracingConsumer = new TracingConsumer<>((Consumer<Object>)arg0, "consumer", GlobalTracer.get());
  else if (argc == 2)
    tracingConsumer = new TracingConsumer<>((Consumer<Object>)arg0, (Consumer<Throwable>)arg1, "consumer", GlobalTracer.get());
  else if (argc == 3)
    tracingConsumer = new TracingConsumer<>((Consumer<Object>)arg0, (Consumer<Throwable>)arg1, (Action)arg2, "consumer", GlobalTracer.get());
  else
    tracingConsumer = null;

  if (tracingConsumer != null)
    ((Observable<Object>)thiz).subscribe(tracingConsumer);

  return null;
}
 
Example #24
Source File: BehaviorRelay.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(Observer<? super T> observer) {
    BehaviorDisposable<T> bs = new BehaviorDisposable<T>(observer, this);
    observer.onSubscribe(bs);
    add(bs);
    if (bs.cancelled) {
        remove(bs);
    } else {
        bs.emitFirst();
    }
}
 
Example #25
Source File: ReplayRelay.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(Observer<? super T> observer) {
    ReplayDisposable<T> rs = new ReplayDisposable<T>(observer, this);
    observer.onSubscribe(rs);

    if (!rs.cancelled) {
        if (add(rs)) {
            if (rs.cancelled) {
                remove(rs);
                return;
            }
        }
        buffer.replay(rs);
    }
}
 
Example #26
Source File: PublishRelay.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(Observer<? super T> t) {
    PublishDisposable<T> ps = new PublishDisposable<T>(t, this);
    t.onSubscribe(ps);
    add(ps);
    // if cancellation happened while a successful add, the remove() didn't work
    // so we need to do it again
    if (ps.isDisposed()) {
        remove(ps);
    }
}
 
Example #27
Source File: ReplayRelay.java    From RxRelay with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void replay(ReplayDisposable<T> rs) {
    if (rs.getAndIncrement() != 0) {
        return;
    }

    int missed = 1;
    final Observer<? super T> a = rs.downstream;

    TimedNode<T> index = (TimedNode<T>)rs.index;
    if (index == null) {
        index = getHead();
    }

    for (;;) {

        if (rs.cancelled) {
            rs.index = null;
            return;
        }

        for (;;) {
            if (rs.cancelled) {
                rs.index = null;
                return;
            }

            TimedNode<T> n = index.get();

            if (n == null) {
                break;
            }

            T o = n.value;

            a.onNext(o);

            index = n;
        }

        if (index.get() != null) {
            continue;
        }

        rs.index = index;

        missed = rs.addAndGet(-missed);
        if (missed == 0) {
            break;
        }
    }
}
 
Example #28
Source File: ReplayRelayTest.java    From RxRelay with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 1000)
public void testUnsubscriptionCase() {
    ReplayRelay<String> src = ReplayRelay.create();

    for (int i = 0; i < 10; i++) {
        final Observer<Object> o = TestHelper.mockObserver();
        InOrder inOrder = inOrder(o);
        String v = "" + i;
        src.accept(v);
        System.out.printf("Turn: %d%n", i);
        src.firstElement()
            .toObservable()
            .flatMap(new Function<String, Observable<String>>() {

                @Override
                public Observable<String> apply(String t1) {
                    return Observable.just(t1 + ", " + t1);
                }
            })
            .subscribe(new DefaultObserver<String>() {
                @Override
                public void onNext(String t) {
                    System.out.println(t);
                    o.onNext(t);
                }

                @Override
                public void onError(Throwable e) {
                    o.onError(e);
                }

                @Override
                public void onComplete() {
                    o.onComplete();
                }
            });
        inOrder.verify(o).onNext("0, 0");
        inOrder.verify(o).onComplete();
        verify(o, never()).onError(any(Throwable.class));
    }
}
 
Example #29
Source File: RequestContextConnectableObservable.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
protected void subscribeActual(Observer<? super T> s) {
    try (SafeCloseable ignored = assemblyContext.push()) {
        source.subscribe(new RequestContextObserver<>(s, assemblyContext));
    }
}
 
Example #30
Source File: RequestContextSupplierObservable.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
protected void subscribeActual(Observer<? super T> s) {
    try (SafeCloseable ignored = assemblyContext.push()) {
        source.subscribe(new RequestContextObserver<>(s, assemblyContext));
    }
}