Search in sources :

Example 1 with Cancellable

use of io.reactivex.rxjava3.functions.Cancellable in project RxJava by ReactiveX.

the class ObservableCreateTest method basicWithCancellable.

@Test
@SuppressUndeliverable
public void basicWithCancellable() {
    final Disposable d1 = Disposable.empty();
    final Disposable d2 = Disposable.empty();
    Observable.<Integer>create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> e) throws Exception {
            e.setDisposable(d1);
            e.setCancellable(new Cancellable() {

                @Override
                public void cancel() throws Exception {
                    d2.dispose();
                }
            });
            e.onNext(1);
            e.onNext(2);
            e.onNext(3);
            e.onComplete();
            e.onError(new TestException());
            e.onNext(4);
            e.onError(new TestException());
            e.onComplete();
        }
    }).test().assertResult(1, 2, 3);
    assertTrue(d1.isDisposed());
    assertTrue(d2.isDisposed());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) Cancellable(io.reactivex.rxjava3.functions.Cancellable) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with Cancellable

use of io.reactivex.rxjava3.functions.Cancellable in project RxJava by ReactiveX.

the class CancellableDisposableTest method cancelThrows.

@Test
public void cancelThrows() {
    final AtomicInteger count = new AtomicInteger();
    Cancellable c = new Cancellable() {

        @Override
        public void cancel() throws Exception {
            count.getAndIncrement();
            throw new TestException();
        }
    };
    CancellableDisposable cd = new CancellableDisposable(c);
    assertFalse(cd.isDisposed());
    List<Throwable> list = TestHelper.trackPluginErrors();
    try {
        cd.dispose();
        cd.dispose();
        TestHelper.assertUndeliverable(list, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
    assertTrue(cd.isDisposed());
    assertEquals(1, count.get());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(io.reactivex.rxjava3.functions.Cancellable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 3 with Cancellable

use of io.reactivex.rxjava3.functions.Cancellable in project RxJava by ReactiveX.

the class CancellableDisposableTest method disposeRace.

@Test
public void disposeRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        final AtomicInteger count = new AtomicInteger();
        Cancellable c = new Cancellable() {

            @Override
            public void cancel() throws Exception {
                count.getAndIncrement();
            }
        };
        final CancellableDisposable cd = new CancellableDisposable(c);
        Runnable r = new Runnable() {

            @Override
            public void run() {
                cd.dispose();
            }
        };
        TestHelper.race(r, r);
        assertEquals(1, count.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(io.reactivex.rxjava3.functions.Cancellable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 4 with Cancellable

use of io.reactivex.rxjava3.functions.Cancellable in project RxJava by ReactiveX.

the class FlowableCreateTest method basicWithCancellable.

@Test
public void basicWithCancellable() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        final Disposable d1 = Disposable.empty();
        final Disposable d2 = Disposable.empty();
        Flowable.<Integer>create(new FlowableOnSubscribe<Integer>() {

            @Override
            public void subscribe(FlowableEmitter<Integer> e) throws Exception {
                e.setDisposable(d1);
                e.setCancellable(new Cancellable() {

                    @Override
                    public void cancel() throws Exception {
                        d2.dispose();
                    }
                });
                e.onNext(1);
                e.onNext(2);
                e.onNext(3);
                e.onComplete();
                e.onError(new TestException("first"));
                e.onNext(4);
                e.onError(new TestException("second"));
                e.onComplete();
            }
        }, BackpressureStrategy.BUFFER).test().assertResult(1, 2, 3);
        assertTrue(d1.isDisposed());
        assertTrue(d2.isDisposed());
        TestHelper.assertUndeliverable(errors, 0, TestException.class, "first");
        TestHelper.assertUndeliverable(errors, 1, TestException.class, "second");
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) Cancellable(io.reactivex.rxjava3.functions.Cancellable) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with Cancellable

use of io.reactivex.rxjava3.functions.Cancellable in project RxJava by ReactiveX.

the class CancellableDisposableTest method normal.

@Test
public void normal() {
    final AtomicInteger count = new AtomicInteger();
    Cancellable c = new Cancellable() {

        @Override
        public void cancel() throws Exception {
            count.getAndIncrement();
        }
    };
    CancellableDisposable cd = new CancellableDisposable(c);
    assertFalse(cd.isDisposed());
    cd.dispose();
    cd.dispose();
    assertTrue(cd.isDisposed());
    assertEquals(1, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(io.reactivex.rxjava3.functions.Cancellable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Aggregations

Cancellable (io.reactivex.rxjava3.functions.Cancellable)5 Test (org.junit.Test)5 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)3 TestException (io.reactivex.rxjava3.exceptions.TestException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Disposable (io.reactivex.rxjava3.disposables.Disposable)2 IOException (java.io.IOException)2