Search in sources :

Example 56 with PublishSubject

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

the class ObservableWindowWithTimeTest method skipTimeAndSizeBoundNoInterruptWindowOutputOnError.

@Test
@SuppressUndeliverable
public void skipTimeAndSizeBoundNoInterruptWindowOutputOnError() 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(90, 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 57 with PublishSubject

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

the class ObservableWindowWithTimeTest method skipOnError.

@Test
@SuppressUndeliverable
public void skipOnError() {
    TestScheduler scheduler = new TestScheduler();
    PublishSubject<Integer> ps = PublishSubject.create();
    TestObserver<Integer> to = ps.window(1, 2, 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 58 with PublishSubject

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

the class ObservableWindowWithStartEndObservableTest method endError.

@Test
@SuppressUndeliverable
public void endError() {
    PublishSubject<Integer> source = PublishSubject.create();
    PublishSubject<Integer> start = PublishSubject.create();
    final PublishSubject<Integer> end = PublishSubject.create();
    TestObserver<Integer> to = source.window(start, new Function<Integer, ObservableSource<Integer>>() {

        @Override
        public ObservableSource<Integer> apply(Integer v) throws Exception {
            return end;
        }
    }).flatMap(Functions.<Observable<Integer>>identity()).test();
    start.onNext(1);
    end.onError(new TestException());
    to.assertFailure(TestException.class);
    assertFalse("Source has observers!", source.hasObservers());
    assertFalse("Start has observers!", start.hasObservers());
    assertFalse("End has observers!", end.hasObservers());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Observable(io.reactivex.rxjava3.core.Observable)

Example 59 with PublishSubject

use of io.reactivex.rxjava3.subjects.PublishSubject in project RxPermissions by tbruyelle.

the class RxPermissionsTest method eachSubscriptionCombined_trigger_granted.

@Test
@TargetApi(Build.VERSION_CODES.M)
public void eachSubscriptionCombined_trigger_granted() {
    TestObserver<Permission> sub = new TestObserver<>();
    String permission = Manifest.permission.READ_PHONE_STATE;
    when(mRxPermissions.isGranted(permission)).thenReturn(false);
    int[] result = new int[] { PackageManager.PERMISSION_GRANTED };
    PublishSubject<Object> trigger = PublishSubject.create();
    trigger.compose(mRxPermissions.ensureEachCombined(permission)).subscribe(sub);
    trigger.onNext(1);
    mRxPermissions.onRequestPermissionsResult(new String[] { permission }, result);
    sub.assertNoErrors();
    sub.assertValue(new Permission(permission, true));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

Example 60 with PublishSubject

use of io.reactivex.rxjava3.subjects.PublishSubject in project RxPermissions by tbruyelle.

the class RxPermissionsTest method eachSubscription_trigger_granted.

@Test
@TargetApi(Build.VERSION_CODES.M)
public void eachSubscription_trigger_granted() {
    TestObserver<Permission> sub = new TestObserver<>();
    String permission = Manifest.permission.READ_PHONE_STATE;
    when(mRxPermissions.isGranted(permission)).thenReturn(false);
    int[] result = new int[] { PackageManager.PERMISSION_GRANTED };
    PublishSubject<Object> trigger = PublishSubject.create();
    trigger.compose(mRxPermissions.ensureEach(permission)).subscribe(sub);
    trigger.onNext(1);
    mRxPermissions.onRequestPermissionsResult(new String[] { permission }, result);
    sub.assertNoErrors();
    sub.assertValue(new Permission(permission, true));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

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