Search in sources :

Example 6 with CompositeDisposable

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

the class CompositeDisposableTest method shouldUnsubscribeAll.

@Test
public void shouldUnsubscribeAll() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    final CompositeDisposable cd = new CompositeDisposable();
    final int count = 10;
    final CountDownLatch start = new CountDownLatch(1);
    for (int i = 0; i < count; i++) {
        cd.add(Disposable.fromRunnable(new Runnable() {

            @Override
            public void run() {
                counter.incrementAndGet();
            }
        }));
    }
    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();
                    cd.dispose();
                } catch (final InterruptedException e) {
                    fail(e.getMessage());
                }
            }
        };
        t.start();
        threads.add(t);
    }
    start.countDown();
    for (final Thread t : threads) {
        t.join();
    }
    assertEquals(count, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 7 with CompositeDisposable

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

the class CompletableMergeArrayDelayError method subscribeActual.

@Override
public void subscribeActual(final CompletableObserver observer) {
    final CompositeDisposable set = new CompositeDisposable();
    final AtomicInteger wip = new AtomicInteger(sources.length + 1);
    final AtomicThrowable errors = new AtomicThrowable();
    set.add(new TryTerminateAndReportDisposable(errors));
    observer.onSubscribe(set);
    for (CompletableSource c : sources) {
        if (set.isDisposed()) {
            return;
        }
        if (c == null) {
            Throwable ex = new NullPointerException("A completable source is null");
            errors.tryAddThrowableOrReport(ex);
            wip.decrementAndGet();
            continue;
        }
        c.subscribe(new MergeInnerCompletableObserver(observer, set, errors, wip));
    }
    if (wip.decrementAndGet() == 0) {
        errors.tryTerminateConsumer(observer);
    }
}
Also used : AtomicThrowable(io.reactivex.rxjava3.internal.util.AtomicThrowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicThrowable(io.reactivex.rxjava3.internal.util.AtomicThrowable)

Example 8 with CompositeDisposable

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

the class CompletableMergeDelayErrorIterable method subscribeActual.

@Override
public void subscribeActual(final CompletableObserver observer) {
    final CompositeDisposable set = new CompositeDisposable();
    observer.onSubscribe(set);
    Iterator<? extends CompletableSource> iterator;
    try {
        iterator = Objects.requireNonNull(sources.iterator(), "The source iterator returned is null");
    } catch (Throwable e) {
        Exceptions.throwIfFatal(e);
        observer.onError(e);
        return;
    }
    final AtomicInteger wip = new AtomicInteger(1);
    final AtomicThrowable errors = new AtomicThrowable();
    set.add(new TryTerminateAndReportDisposable(errors));
    for (; ; ) {
        if (set.isDisposed()) {
            return;
        }
        boolean b;
        try {
            b = iterator.hasNext();
        } catch (Throwable e) {
            Exceptions.throwIfFatal(e);
            errors.tryAddThrowableOrReport(e);
            break;
        }
        if (!b) {
            break;
        }
        if (set.isDisposed()) {
            return;
        }
        CompletableSource c;
        try {
            c = Objects.requireNonNull(iterator.next(), "The iterator returned a null CompletableSource");
        } catch (Throwable e) {
            Exceptions.throwIfFatal(e);
            errors.tryAddThrowableOrReport(e);
            break;
        }
        if (set.isDisposed()) {
            return;
        }
        wip.getAndIncrement();
        c.subscribe(new MergeInnerCompletableObserver(observer, set, errors, wip));
    }
    if (wip.decrementAndGet() == 0) {
        errors.tryTerminateConsumer(observer);
    }
}
Also used : AtomicThrowable(io.reactivex.rxjava3.internal.util.AtomicThrowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicThrowable(io.reactivex.rxjava3.internal.util.AtomicThrowable) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable)

Example 9 with CompositeDisposable

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

the class CompletableTimeoutTest method ambRace.

@Test
public void ambRace() {
    TestObserver<Void> to = new TestObserver<>();
    to.onSubscribe(Disposable.empty());
    CompositeDisposable cd = new CompositeDisposable();
    AtomicBoolean once = new AtomicBoolean();
    TimeOutObserver a = new TimeOutObserver(cd, once, to);
    a.onComplete();
    a.onComplete();
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        a.onError(new TestException());
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestException(io.reactivex.rxjava3.exceptions.TestException) TimeOutObserver(io.reactivex.rxjava3.internal.operators.completable.CompletableTimeout.TimeOutObserver) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.Test)

Example 10 with CompositeDisposable

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

the class CompletableAmbTest method ambRace.

@Test
public void ambRace() {
    TestObserver<Void> to = new TestObserver<>();
    to.onSubscribe(Disposable.empty());
    CompositeDisposable cd = new CompositeDisposable();
    AtomicBoolean once = new AtomicBoolean();
    Amb a = new Amb(once, cd, to);
    a.onSubscribe(Disposable.empty());
    a.onComplete();
    a.onComplete();
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        a.onError(new TestException());
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestException(io.reactivex.rxjava3.exceptions.TestException) Amb(io.reactivex.rxjava3.internal.operators.completable.CompletableAmb.Amb) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)28 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)24 CompositeDisposable (io.reactivex.rxjava3.disposables.CompositeDisposable)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)4 Disposable (io.reactivex.rxjava3.disposables.Disposable)4 TestException (io.reactivex.rxjava3.exceptions.TestException)4 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)4 FutureTask (java.util.concurrent.FutureTask)4 WebSocketUnavailableException (org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException)4 AtomicThrowable (io.reactivex.rxjava3.internal.util.AtomicThrowable)2 TestObserver (io.reactivex.rxjava3.observers.TestObserver)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Scheduler (io.reactivex.rxjava3.core.Scheduler)1 CompositeException (io.reactivex.rxjava3.exceptions.CompositeException)1 Amb (io.reactivex.rxjava3.internal.operators.completable.CompletableAmb.Amb)1 TimeOutObserver (io.reactivex.rxjava3.internal.operators.completable.CompletableTimeout.TimeOutObserver)1 NewThreadWorker (io.reactivex.rxjava3.internal.schedulers.NewThreadWorker)1 WorkerCallback (io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport.WorkerCallback)1