Search in sources :

Example 81 with PublishSubject

use of io.reactivex.rxjava3.subjects.PublishSubject in project RxJava by ReactiveX.

the class ObservableTimeoutWithSelectorTest method timeoutSelectorNormal1.

@Test
public void timeoutSelectorNormal1() {
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> timeout = PublishSubject.create();
    Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return timeout;
        }
    };
    Observable<Integer> other = Observable.fromIterable(Arrays.asList(100));
    Observer<Object> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    source.timeout(timeout, timeoutFunc, other).subscribe(o);
    source.onNext(1);
    source.onNext(2);
    source.onNext(3);
    timeout.onNext(1);
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onNext(2);
    inOrder.verify(o).onNext(3);
    inOrder.verify(o).onNext(100);
    inOrder.verify(o).onComplete();
    verify(o, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable) Test(org.junit.Test)

Example 82 with PublishSubject

use of io.reactivex.rxjava3.subjects.PublishSubject in project RxJava by ReactiveX.

the class ObservableWindowWithTimeTest method exactTimeBoundNoInterruptWindowOutputOnError.

@Test
@SuppressUndeliverable
public void exactTimeBoundNoInterruptWindowOutputOnError() throws Exception {
    final AtomicBoolean isInterrupted = new AtomicBoolean();
    final PublishSubject<Integer> ps = PublishSubject.create();
    final CountDownLatch doOnNextDone = new CountDownLatch(1);
    final CountDownLatch secondWindowProcessing = new CountDownLatch(1);
    ps.window(100, TimeUnit.MILLISECONDS).doOnNext(new Consumer<Observable<Integer>>() {

        int count;

        @Override
        public void accept(Observable<Integer> v) throws Exception {
            System.out.println(Thread.currentThread());
            if (count++ == 1) {
                secondWindowProcessing.countDown();
                try {
                    Thread.sleep(200);
                    isInterrupted.set(Thread.interrupted());
                } catch (InterruptedException ex) {
                    isInterrupted.set(true);
                }
                doOnNextDone.countDown();
            }
        }
    }).test();
    ps.onNext(1);
    assertTrue(secondWindowProcessing.await(5, TimeUnit.SECONDS));
    ps.onError(new TestException());
    assertTrue(doOnNextDone.await(5, TimeUnit.SECONDS));
    assertFalse("The doOnNext got interrupted!", isInterrupted.get());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable)

Example 83 with PublishSubject

use of io.reactivex.rxjava3.subjects.PublishSubject in project RxJava by ReactiveX.

the class ObservableWindowWithTimeTest method exactOnError.

@Test
@SuppressUndeliverable
public void exactOnError() {
    TestScheduler scheduler = new TestScheduler();
    PublishSubject<Integer> ps = PublishSubject.create();
    TestObserver<Integer> to = ps.window(1, 1, TimeUnit.SECONDS, scheduler).flatMap(Functions.<Observable<Integer>>identity()).test();
    ps.onError(new TestException());
    to.assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable)

Example 84 with PublishSubject

use of io.reactivex.rxjava3.subjects.PublishSubject in project RxJava by ReactiveX.

the class ObservableWindowWithTimeTest method exactTimeAndSizeBoundNoInterruptWindowOutputOnError.

@Test
@SuppressUndeliverable
public void exactTimeAndSizeBoundNoInterruptWindowOutputOnError() throws Exception {
    final AtomicBoolean isInterrupted = new AtomicBoolean();
    final PublishSubject<Integer> ps = PublishSubject.create();
    final CountDownLatch doOnNextDone = new CountDownLatch(1);
    final CountDownLatch secondWindowProcessing = new CountDownLatch(1);
    ps.window(100, TimeUnit.MILLISECONDS, 10).doOnNext(new Consumer<Observable<Integer>>() {

        int count;

        @Override
        public void accept(Observable<Integer> v) throws Exception {
            System.out.println(Thread.currentThread());
            if (count++ == 1) {
                secondWindowProcessing.countDown();
                try {
                    Thread.sleep(200);
                    isInterrupted.set(Thread.interrupted());
                } catch (InterruptedException ex) {
                    isInterrupted.set(true);
                }
                doOnNextDone.countDown();
            }
        }
    }).test();
    ps.onNext(1);
    assertTrue(secondWindowProcessing.await(5, TimeUnit.SECONDS));
    ps.onError(new TestException());
    assertTrue(doOnNextDone.await(5, TimeUnit.SECONDS));
    assertFalse("The doOnNext got interrupted!", isInterrupted.get());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable)

Example 85 with PublishSubject

use of io.reactivex.rxjava3.subjects.PublishSubject in project RxJava by ReactiveX.

the class ObservableWithLatestFromTest method simple.

@Test
public void simple() {
    PublishSubject<Integer> source = PublishSubject.create();
    PublishSubject<Integer> other = PublishSubject.create();
    Observer<Integer> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    Observable<Integer> result = source.withLatestFrom(other, COMBINER);
    result.subscribe(o);
    source.onNext(1);
    inOrder.verify(o, never()).onNext(anyInt());
    other.onNext(1);
    inOrder.verify(o, never()).onNext(anyInt());
    source.onNext(2);
    inOrder.verify(o).onNext((2 << 8) + 1);
    other.onNext(2);
    inOrder.verify(o, never()).onNext(anyInt());
    other.onComplete();
    inOrder.verify(o, never()).onComplete();
    source.onNext(3);
    inOrder.verify(o).onNext((3 << 8) + 2);
    source.onComplete();
    inOrder.verify(o).onComplete();
    verify(o, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Aggregations

TestException (io.reactivex.rxjava3.exceptions.TestException)69 Test (org.junit.Test)68 InOrder (org.mockito.InOrder)36 Observable (io.reactivex.rxjava3.core.Observable)28 Disposable (io.reactivex.rxjava3.disposables.Disposable)17 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)17 TestObserver (io.reactivex.rxjava3.observers.TestObserver)14 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 ObservableReplay (io.reactivex.rxjava3.internal.operators.observable.ObservableReplay)4 IOException (java.io.IOException)4 TargetApi (android.annotation.TargetApi)3 Function (io.reactivex.rxjava3.functions.Function)3 CrashingIterable (io.reactivex.rxjava3.internal.util.CrashingIterable)3 PublishSubject (io.reactivex.rxjava3.subjects.PublishSubject)3 Matchers.anyString (org.mockito.Matchers.anyString)3 NonNull (io.reactivex.rxjava3.annotations.NonNull)2 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)1 Observer (io.reactivex.rxjava3.core.Observer)1 GroupedObservable (io.reactivex.rxjava3.observables.GroupedObservable)1