Search in sources :

Example 66 with Consumer

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());
    });
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 67 with Consumer

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);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException)

Example 68 with Consumer

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());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable)

Example 69 with Consumer

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");
        }
    });
}
Also used : Action(io.reactivex.rxjava3.functions.Action) RxPermissions(com.tbruyelle.rxpermissions3.RxPermissions) Consumer(io.reactivex.rxjava3.functions.Consumer) Permission(com.tbruyelle.rxpermissions3.Permission) IOException(java.io.IOException)

Example 70 with Consumer

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();
}
Also used : RFuture(org.redisson.api.RFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) Single(io.reactivex.rxjava3.core.Single) Flowable(io.reactivex.rxjava3.core.Flowable) Publisher(org.reactivestreams.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(io.reactivex.rxjava3.functions.Consumer) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) Action(io.reactivex.rxjava3.functions.Action) ReplayProcessor(io.reactivex.rxjava3.processors.ReplayProcessor) Action(io.reactivex.rxjava3.functions.Action) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

Test (org.junit.Test)98 TestException (io.reactivex.rxjava3.exceptions.TestException)57 Disposable (io.reactivex.rxjava3.disposables.Disposable)39 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)22 IOException (java.io.IOException)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 InOrder (org.mockito.InOrder)17 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)8 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Observable (io.reactivex.rxjava3.core.Observable)5 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)5 Consumer (io.reactivex.rxjava3.functions.Consumer)5 CompositeException (io.reactivex.rxjava3.exceptions.CompositeException)4 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Observer (io.reactivex.rxjava3.core.Observer)3 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)3 ArgsToString (io.reactivex.rxjava3.internal.operators.flowable.FlowableZipTest.ArgsToString)3