Search in sources :

Example 91 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class SerializedSubjectTest method normal.

@Test
public void normal() {
    Subject<Integer> s = PublishSubject.<Integer>create().toSerialized();
    TestObserver<Integer> to = s.test();
    Observable.range(1, 10).subscribe(s);
    to.assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    assertFalse(s.hasObservers());
    s.onNext(11);
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        s.onError(new TestException());
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
    s.onComplete();
    Disposable bs = Disposable.empty();
    s.onSubscribe(bs);
    assertTrue(bs.isDisposed());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 92 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class FlowableSubscriberTest method forEachWhile.

@Test
public void forEachWhile() {
    PublishProcessor<Integer> pp = PublishProcessor.create();
    final List<Integer> list = new ArrayList<>();
    Disposable d = pp.forEachWhile(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) throws Exception {
            list.add(v);
            return v < 3;
        }
    });
    assertFalse(d.isDisposed());
    pp.onNext(1);
    pp.onNext(2);
    pp.onNext(3);
    assertFalse(pp.hasSubscribers());
    assertEquals(Arrays.asList(1, 2, 3), list);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Example 93 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class SerialDisposableTests method concurrentSetDisposableShouldNotInterleave.

@Test
public void concurrentSetDisposableShouldNotInterleave() throws InterruptedException {
    final int count = 10;
    final List<Disposable> subscriptions = new ArrayList<>();
    final CountDownLatch start = new CountDownLatch(1);
    final CountDownLatch end = new CountDownLatch(count);
    final List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        final Disposable subscription = mock(Disposable.class);
        subscriptions.add(subscription);
        final Thread t = new Thread() {

            @Override
            public void run() {
                try {
                    start.await();
                    serialDisposable.set(subscription);
                } catch (InterruptedException e) {
                    fail(e.getMessage());
                } finally {
                    end.countDown();
                }
            }
        };
        t.start();
        threads.add(t);
    }
    start.countDown();
    end.await();
    serialDisposable.dispose();
    for (final Disposable subscription : subscriptions) {
        verify(subscription).dispose();
    }
    for (final Thread t : threads) {
        t.join();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest)

Example 94 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class SerialDisposableTests method settingUnderlyingWhenUnsubscribedCausesImmediateUnsubscriptionConcurrently.

@Test
public void settingUnderlyingWhenUnsubscribedCausesImmediateUnsubscriptionConcurrently() throws InterruptedException {
    final Disposable firstSet = mock(Disposable.class);
    serialDisposable.set(firstSet);
    final CountDownLatch start = new CountDownLatch(1);
    final int count = 10;
    final CountDownLatch end = new CountDownLatch(count);
    final List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        final Thread t = new Thread() {

            @Override
            public void run() {
                try {
                    start.await();
                    serialDisposable.dispose();
                } catch (InterruptedException e) {
                    fail(e.getMessage());
                } finally {
                    end.countDown();
                }
            }
        };
        t.start();
        threads.add(t);
    }
    final Disposable underlying = mock(Disposable.class);
    start.countDown();
    serialDisposable.set(underlying);
    end.await();
    verify(firstSet).dispose();
    verify(underlying).dispose();
    for (final Thread t : threads) {
        t.join();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest)

Example 95 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class ExceptionsTest method stackOverflowWouldOccur.

@Test
public void stackOverflowWouldOccur() {
    final PublishSubject<Integer> a = PublishSubject.create();
    final PublishSubject<Integer> b = PublishSubject.create();
    final int MAX_STACK_DEPTH = 800;
    final AtomicInteger depth = new AtomicInteger();
    a.subscribe(new Observer<Integer>() {

        @Override
        public void onSubscribe(Disposable d) {
        }

        @Override
        public void onComplete() {
        }

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

        @Override
        public void onNext(Integer n) {
            b.onNext(n + 1);
        }
    });
    b.subscribe(new Observer<Integer>() {

        @Override
        public void onSubscribe(Disposable d) {
        // TODO Auto-generated method stub
        }

        @Override
        public void onComplete() {
        }

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

        @Override
        public void onNext(Integer n) {
            if (depth.get() < MAX_STACK_DEPTH) {
                depth.set(Thread.currentThread().getStackTrace().length);
                a.onNext(n + 1);
            }
        }
    });
    a.onNext(1);
    assertTrue(depth.get() >= MAX_STACK_DEPTH);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(io.reactivex.rxjava3.disposables.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)250 Disposable (io.reactivex.rxjava3.disposables.Disposable)219 TestException (io.reactivex.rxjava3.exceptions.TestException)74 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)56 IOException (java.io.IOException)37 Scheduler (io.reactivex.rxjava3.core.Scheduler)36 EmptyDisposable (io.reactivex.rxjava3.internal.disposables.EmptyDisposable)35 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)34 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)32 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 TestObserver (io.reactivex.rxjava3.observers.TestObserver)18 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)15 QueueDisposable (io.reactivex.rxjava3.operators.QueueDisposable)13 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)10 TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)9 Observable (io.reactivex.rxjava3.core.Observable)6 Observer (io.reactivex.rxjava3.core.Observer)6 ScalarDisposable (io.reactivex.rxjava3.internal.operators.observable.ObservableScalarXMap.ScalarDisposable)6 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)6