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());
}
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());
}
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());
}
}
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();
}
}
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());
}
Aggregations