Search in sources :

Example 1 with Action

use of io.reactivex.rxjava3.functions.Action in project redisson by redisson.

the class RedissonTopicRx method getMessages.

public <M> Flowable<M> getMessages(Class<M> type) {
    ReplayProcessor<M> p = ReplayProcessor.create();
    return p.doOnRequest(new LongConsumer() {

        @Override
        public void accept(long n) throws Exception {
            AtomicLong counter = new AtomicLong(n);
            RFuture<Integer> t = topic.addListenerAsync(type, new MessageListener<M>() {

                @Override
                public void onMessage(CharSequence channel, M msg) {
                    p.onNext(msg);
                    if (counter.decrementAndGet() == 0) {
                        topic.removeListenerAsync(this);
                        p.onComplete();
                    }
                }
            });
            t.whenComplete((id, e) -> {
                if (e != null) {
                    p.onError(e);
                    return;
                }
                p.doOnCancel(new Action() {

                    @Override
                    public void run() throws Exception {
                        topic.removeListenerAsync(id);
                    }
                });
            });
        }
    });
}
Also used : RFuture(org.redisson.api.RFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flowable(io.reactivex.rxjava3.core.Flowable) RTopic(org.redisson.api.RTopic) MessageListener(org.redisson.api.listener.MessageListener) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) Action(io.reactivex.rxjava3.functions.Action) ReplayProcessor(io.reactivex.rxjava3.processors.ReplayProcessor) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Action(io.reactivex.rxjava3.functions.Action) MessageListener(org.redisson.api.listener.MessageListener) RFuture(org.redisson.api.RFuture)

Example 2 with Action

use of io.reactivex.rxjava3.functions.Action in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method directSchedulePeriodicallyUsesHookOnce.

@Test
@Ignore("Implementation delegated to default RxJava implementation")
public void directSchedulePeriodicallyUsesHookOnce() {
    final CountingRunnable newCounter = new CountingRunnable();
    final AtomicReference<Runnable> runnableRef = new AtomicReference<>();
    RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() {

        @Override
        public Runnable apply(Runnable runnable) {
            runnableRef.set(runnable);
            return newCounter;
        }
    });
    CountingRunnable counter = new CountingRunnable();
    scheduler.schedulePeriodicallyDirect(counter, 1, 1, MINUTES);
    // Verify our action was passed to the schedulers hook.
    assertSame(counter, runnableRef.get());
    runnableRef.set(null);
    idleMainLooper(1, MINUTES);
    runUiThreadTasks();
    // Verify the scheduled action was the one returned from the hook.
    assertEquals(1, newCounter.get());
    assertEquals(0, counter.get());
    // Ensure the hook was not called again when the runnable re-scheduled itself.
    assertNull(runnableRef.get());
}
Also used : CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with Action

use of io.reactivex.rxjava3.functions.Action in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method throwingActionRoutedToRxJavaPlugins.

@Test
public void throwingActionRoutedToRxJavaPlugins() {
    Consumer<? super Throwable> originalErrorHandler = RxJavaPlugins.getErrorHandler();
    try {
        final AtomicReference<Throwable> throwableRef = new AtomicReference<>();
        RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {

            @Override
            public void accept(Throwable throwable) throws Exception {
                throwableRef.set(throwable);
            }
        });
        Worker worker = scheduler.createWorker();
        final NullPointerException npe = new NullPointerException();
        Runnable action = new Runnable() {

            @Override
            public void run() {
                throw npe;
            }
        };
        worker.schedule(action);
        runUiThreadTasks();
        assertSame(npe, throwableRef.get());
    } finally {
        RxJavaPlugins.setErrorHandler(originalErrorHandler);
    }
}
Also used : CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 4 with Action

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

the class CompletableConsumersTest method onCompleteCrash.

@Test
public void onCompleteCrash() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        processor.subscribe(new Action() {

            @Override
            public void run() throws Exception {
                throw new IOException();
            }
        }, this, composite);
        processor.onComplete();
        assertTrue(events.toString(), events.isEmpty());
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : IOException(java.io.IOException) CompositeException(io.reactivex.rxjava3.exceptions.CompositeException) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with Action

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

the class MaybeDoOnTerminateTest method doOnTerminateSuccess.

@Test
public void doOnTerminateSuccess() {
    final AtomicBoolean atomicBoolean = new AtomicBoolean();
    Maybe.just(1).doOnTerminate(new Action() {

        @Override
        public void run() {
            atomicBoolean.set(true);
        }
    }).test().assertResult(1);
    assertTrue(atomicBoolean.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Action(io.reactivex.rxjava3.functions.Action) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)109 TestException (io.reactivex.rxjava3.exceptions.TestException)64 Action (io.reactivex.rxjava3.functions.Action)49 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)13 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 TestObserver (io.reactivex.rxjava3.observers.TestObserver)10 Disposable (io.reactivex.rxjava3.disposables.Disposable)9 IOException (java.io.IOException)9 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)7 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)7 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 Observable (io.reactivex.rxjava3.core.Observable)4 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)4 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)3 Flowable (io.reactivex.rxjava3.core.Flowable)3 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)3 ConditionalSubscriber (io.reactivex.rxjava3.operators.ConditionalSubscriber)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3