Search in sources :

Example 16 with CompositeDisposable

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

the class ScheduledRunnableTest method syncWorkerCancelRace.

@Test
public void syncWorkerCancelRace() {
    for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
        final CompositeDisposable set = new CompositeDisposable();
        final AtomicBoolean interrupted = new AtomicBoolean();
        final AtomicInteger sync = new AtomicInteger(2);
        final AtomicInteger syncb = new AtomicInteger(2);
        Runnable r0 = new Runnable() {

            @Override
            public void run() {
                set.dispose();
                if (sync.decrementAndGet() != 0) {
                    while (sync.get() != 0) {
                    }
                }
                if (syncb.decrementAndGet() != 0) {
                    while (syncb.get() != 0) {
                    }
                }
                for (int j = 0; j < 1000; j++) {
                    if (Thread.currentThread().isInterrupted()) {
                        interrupted.set(true);
                        break;
                    }
                }
            }
        };
        final ScheduledRunnable run = new ScheduledRunnable(r0, set);
        set.add(run);
        final FutureTask<Void> ft = new FutureTask<>(run, null);
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                if (sync.decrementAndGet() != 0) {
                    while (sync.get() != 0) {
                    }
                }
                run.setFuture(ft);
                if (syncb.decrementAndGet() != 0) {
                    while (syncb.get() != 0) {
                    }
                }
            }
        };
        TestHelper.race(ft, r2);
        assertFalse("The task was interrupted", interrupted.get());
    }
}
Also used : FutureTask(java.util.concurrent.FutureTask) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 17 with CompositeDisposable

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

the class ScheduledRunnableTest method runDispose.

@Test
public void runDispose() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        CompositeDisposable set = new CompositeDisposable();
        final ScheduledRunnable run = new ScheduledRunnable(Functions.EMPTY_RUNNABLE, set);
        set.add(run);
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                run.call();
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                run.dispose();
            }
        };
        TestHelper.race(r1, r2);
        assertEquals(0, set.size());
    }
}
Also used : CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 18 with CompositeDisposable

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

the class ScheduledRunnableTest method setFutureRunRace.

@Test
public void setFutureRunRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        CompositeDisposable set = new CompositeDisposable();
        final ScheduledRunnable run = new ScheduledRunnable(Functions.EMPTY_RUNNABLE, set);
        set.add(run);
        final FutureTask<Object> ft = new FutureTask<>(Functions.EMPTY_RUNNABLE, 0);
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                run.setFuture(ft);
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                run.run();
            }
        };
        TestHelper.race(r1, r2);
        assertEquals(0, set.size());
    }
}
Also used : FutureTask(java.util.concurrent.FutureTask) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 19 with CompositeDisposable

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

the class ScheduledRunnableTest method pluginCrash.

@Test
public void pluginCrash() {
    Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            throw new TestException("Second");
        }
    });
    CompositeDisposable set = new CompositeDisposable();
    final ScheduledRunnable run = new ScheduledRunnable(new Runnable() {

        @Override
        public void run() {
            throw new TestException("First");
        }
    }, set);
    set.add(run);
    try {
        run.run();
        fail("Should have thrown!");
    } catch (TestException ex) {
        assertEquals("Second", ex.getMessage());
    } finally {
        Thread.currentThread().setUncaughtExceptionHandler(null);
    }
    assertTrue(run.isDisposed());
    assertEquals(0, set.size());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 20 with CompositeDisposable

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

the class ScheduledRunnableTest method crashReported.

@Test
public void crashReported() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        CompositeDisposable set = new CompositeDisposable();
        final ScheduledRunnable run = new ScheduledRunnable(new Runnable() {

            @Override
            public void run() {
                throw new TestException("First");
            }
        }, set);
        set.add(run);
        try {
            run.run();
            fail("Should have thrown!");
        } catch (TestException expected) {
        // expected
        }
        assertTrue(run.isDisposed());
        assertEquals(0, set.size());
        TestHelper.assertUndeliverable(errors, 0, TestException.class, "First");
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) 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