use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class SingleBlockingSubscribeTest method twoArgSuccessFails.
@Test
public void twoArgSuccessFails() throws Throwable {
TestHelper.withErrorTracking(errors -> {
@SuppressWarnings("unchecked") Consumer<Integer> success = mock(Consumer.class);
doThrow(new TestException()).when(success).accept(any());
@SuppressWarnings("unchecked") Consumer<? super Throwable> consumer = mock(Consumer.class);
Single.just(1).blockingSubscribe(success, consumer);
TestHelper.assertUndeliverable(errors, 0, TestException.class);
verify(success).accept(any());
verify(consumer, never()).accept(any());
});
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class ObservableReplayTest method connectConsumerThrows.
@Test
public void connectConsumerThrows() {
ConnectableObservable<Integer> co = Observable.range(1, 2).replay();
try {
co.connect(new Consumer<Disposable>() {
@Override
public void accept(Disposable t) throws Exception {
throw new TestException();
}
});
fail("Should have thrown");
} catch (TestException ex) {
// expected
}
co.test().assertEmpty().dispose();
co.connect();
co.test().assertResult(1, 2);
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class ObservableWindowWithTimeTest method skipTimeAndSizeBoundNoInterruptWindowOutputOnError.
@Test
@SuppressUndeliverable
public void skipTimeAndSizeBoundNoInterruptWindowOutputOnError() throws Exception {
final AtomicBoolean isInterrupted = new AtomicBoolean();
final PublishSubject<Integer> ps = PublishSubject.create();
final CountDownLatch doOnNextDone = new CountDownLatch(1);
final CountDownLatch secondWindowProcessing = new CountDownLatch(1);
ps.window(90, 100, TimeUnit.MILLISECONDS).doOnNext(new Consumer<Observable<Integer>>() {
int count;
@Override
public void accept(Observable<Integer> v) throws Exception {
System.out.println(Thread.currentThread());
if (count++ == 1) {
secondWindowProcessing.countDown();
try {
Thread.sleep(200);
isInterrupted.set(Thread.interrupted());
} catch (InterruptedException ex) {
isInterrupted.set(true);
}
doOnNextDone.countDown();
}
}
}).test();
ps.onNext(1);
assertTrue(secondWindowProcessing.await(5, TimeUnit.SECONDS));
ps.onError(new TestException());
assertTrue(doOnNextDone.await(5, TimeUnit.SECONDS));
assertFalse("The doOnNext got interrupted!", isInterrupted.get());
}
use of io.reactivex.rxjava3.functions.Consumer in project RxPermissions by tbruyelle.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RxPermissions rxPermissions = new RxPermissions(this);
rxPermissions.setLogging(true);
setContentView(R.layout.act_main);
surfaceView = findViewById(R.id.surfaceView);
disposable = RxView.clicks(findViewById(R.id.enableCamera)).compose(rxPermissions.ensureEach(permission.CAMERA)).subscribe(new Consumer<Permission>() {
@Override
public void accept(Permission permission) {
Log.i(TAG, "Permission result " + permission);
if (permission.granted) {
releaseCamera();
camera = Camera.open(0);
try {
camera.setPreviewDisplay(surfaceView.getHolder());
camera.startPreview();
} catch (IOException e) {
Log.e(TAG, "Error while trying to display the camera preview", e);
}
} else if (permission.shouldShowRequestPermissionRationale) {
// Denied permission without ask never again
Toast.makeText(MainActivity.this, "Denied permission without ask never again", Toast.LENGTH_SHORT).show();
} else {
// Denied permission with ask never again
// Need to go to the settings
Toast.makeText(MainActivity.this, "Permission denied, can't enable the camera", Toast.LENGTH_SHORT).show();
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable t) {
Log.e(TAG, "onError", t);
}
}, new Action() {
@Override
public void run() {
Log.i(TAG, "OnComplete");
}
});
}
use of io.reactivex.rxjava3.functions.Consumer in project redisson by redisson.
the class PublisherAdder method addAll.
public Single<Boolean> addAll(Publisher<? extends V> c) {
final Flowable<? extends V> cc = Flowable.fromPublisher(c);
final ReplayProcessor<Boolean> p = ReplayProcessor.create();
return p.doOnRequest(new LongConsumer() {
@Override
public void accept(long t) throws Exception {
final AtomicBoolean completed = new AtomicBoolean();
final AtomicLong values = new AtomicLong();
final AtomicBoolean lastSize = new AtomicBoolean();
cc.subscribe(new Consumer<V>() {
@Override
public void accept(V t) throws Exception {
values.getAndIncrement();
add(t).whenComplete((res, e) -> {
if (e != null) {
p.onError(e);
return;
}
if (res) {
lastSize.set(true);
}
if (values.decrementAndGet() == 0 && completed.get()) {
p.onNext(lastSize.get());
p.onComplete();
}
});
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable t) throws Exception {
p.onError(t);
}
}, new Action() {
@Override
public void run() throws Exception {
completed.set(true);
if (values.get() == 0) {
p.onNext(lastSize.get());
p.onComplete();
}
}
});
}
}).singleOrError();
}
Aggregations