Search in sources :

Example 6 with NonNull

use of io.reactivex.rxjava3.annotations.NonNull in project RxJava by ReactiveX.

the class SingleFlattenStreamAsFlowableTest method requestOneByOne.

@Test
public void requestOneByOne() {
    TestSubscriber<Integer> ts = new TestSubscriber<>();
    Single.just(1).flattenStreamAsFlowable(v -> Stream.of(1, 2, 3, 4, 5)).subscribe(new FlowableSubscriber<Integer>() {

        Subscription upstream;

        @Override
        public void onSubscribe(@NonNull Subscription s) {
            ts.onSubscribe(new BooleanSubscription());
            upstream = s;
            s.request(1);
        }

        @Override
        public void onNext(Integer t) {
            ts.onNext(t);
            upstream.request(1);
        }

        @Override
        public void onError(Throwable t) {
            ts.onError(t);
        }

        @Override
        public void onComplete() {
            ts.onComplete();
        }
    });
    ts.assertResult(1, 2, 3, 4, 5);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Iterator(java.util.Iterator) TestException(io.reactivex.rxjava3.exceptions.TestException) java.util.stream(java.util.stream) Test(org.junit.Test) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) NonNull(io.reactivex.rxjava3.annotations.NonNull) AtomicReference(java.util.concurrent.atomic.AtomicReference) SingleSubject(io.reactivex.rxjava3.subjects.SingleSubject) Mockito(org.mockito.Mockito) QueueFuseable(io.reactivex.rxjava3.operators.QueueFuseable) Function(io.reactivex.rxjava3.functions.Function) Subscription(org.reactivestreams.Subscription) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Assert(org.junit.Assert) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) QueueSubscription(io.reactivex.rxjava3.operators.QueueSubscription) io.reactivex.rxjava3.testsupport(io.reactivex.rxjava3.testsupport) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Subscription(org.reactivestreams.Subscription) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) QueueSubscription(io.reactivex.rxjava3.operators.QueueSubscription) Test(org.junit.Test)

Example 7 with NonNull

use of io.reactivex.rxjava3.annotations.NonNull in project RxJava by ReactiveX.

the class SingleSafeSubscribeTest method onSuccessCrash.

@Test
public void onSuccessCrash() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        @SuppressWarnings("unchecked") SingleObserver<Integer> consumer = mock(SingleObserver.class);
        doThrow(new TestException()).when(consumer).onSuccess(any());
        new Single<Integer>() {

            @Override
            protected void subscribeActual(@NonNull SingleObserver<? super Integer> observer) {
                observer.onSubscribe(Disposable.empty());
                observer.onSuccess(1);
            }
        }.safeSubscribe(consumer);
        InOrder order = inOrder(consumer);
        order.verify(consumer).onSubscribe(any(Disposable.class));
        order.verify(consumer).onSuccess(1);
        order.verifyNoMoreInteractions();
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    });
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 8 with NonNull

use of io.reactivex.rxjava3.annotations.NonNull in project RxJava by ReactiveX.

the class SingleSafeSubscribeTest method onSubscribeCrash.

@Test
public void onSubscribeCrash() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        @SuppressWarnings("unchecked") SingleObserver<Integer> consumer = mock(SingleObserver.class);
        doThrow(new TestException()).when(consumer).onSubscribe(any());
        Disposable d = Disposable.empty();
        new Single<Integer>() {

            @Override
            protected void subscribeActual(@NonNull SingleObserver<? super Integer> observer) {
                observer.onSubscribe(d);
                // none of the following should arrive at the consumer
                observer.onSuccess(1);
                observer.onError(new IOException());
            }
        }.safeSubscribe(consumer);
        InOrder order = inOrder(consumer);
        order.verify(consumer).onSubscribe(any(Disposable.class));
        order.verifyNoMoreInteractions();
        assertTrue(d.isDisposed());
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
        TestHelper.assertUndeliverable(errors, 1, IOException.class);
    });
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) InOrder(org.mockito.InOrder) IOException(java.io.IOException) Test(org.junit.Test)

Example 9 with NonNull

use of io.reactivex.rxjava3.annotations.NonNull in project RxJava by ReactiveX.

the class ParallelMapTest method conditionalCancelIgnored.

@Test
public void conditionalCancelIgnored() {
    Flowable<Integer> f = new Flowable<Integer>() {

        @Override
        protected void subscribeActual(@NonNull Subscriber<@NonNull ? super @NonNull Integer> s) {
            @SuppressWarnings("unchecked") ConditionalSubscriber<Integer> subscriber = (ConditionalSubscriber<Integer>) s;
            subscriber.onSubscribe(new BooleanSubscription());
            subscriber.tryOnNext(1);
            subscriber.tryOnNext(2);
        }
    };
    ParallelFlowable.fromArray(f).map(v -> {
        throw new TestException();
    }).filter(v -> true).sequential().test().assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) io.reactivex.rxjava3.functions(io.reactivex.rxjava3.functions) ConditionalSubscriber(io.reactivex.rxjava3.operators.ConditionalSubscriber) Test(org.junit.Test) NonNull(io.reactivex.rxjava3.annotations.NonNull) TimeUnit(java.util.concurrent.TimeUnit) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) List(java.util.List) TestHelper(io.reactivex.rxjava3.testsupport.TestHelper) Functions(io.reactivex.rxjava3.internal.functions.Functions) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Assert(org.junit.Assert) Subscriber(org.reactivestreams.Subscriber) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) RxJavaPlugins(io.reactivex.rxjava3.plugins.RxJavaPlugins) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.rxjava3.exceptions.TestException) ConditionalSubscriber(io.reactivex.rxjava3.operators.ConditionalSubscriber) Subscriber(org.reactivestreams.Subscriber) NonNull(io.reactivex.rxjava3.annotations.NonNull) ConditionalSubscriber(io.reactivex.rxjava3.operators.ConditionalSubscriber) Test(org.junit.Test)

Example 10 with NonNull

use of io.reactivex.rxjava3.annotations.NonNull in project RxJava by ReactiveX.

the class Scheduler method schedulePeriodicallyDirect.

/**
 * Schedules a periodic execution of the given task with the given initial time delay and repeat period.
 *
 * <p>
 * This method is safe to be called from multiple threads but there are no
 * ordering guarantees between tasks.
 *
 * <p>
 * The periodic execution is at a fixed rate, that is, the first execution will be after the
 * {@code initialDelay}, the second after {@code initialDelay + period}, the third after
 * {@code initialDelay + 2 * period}, and so on.
 *
 * @param run the task to schedule
 * @param initialDelay the initial delay amount, non-positive values indicate non-delayed scheduling
 * @param period the period at which the task should be re-executed
 * @param unit the unit of measure of the delay amount
 * @return the Disposable that let's one cancel this particular delayed task.
 * @throws NullPointerException if {@code run} or {@code unit} is {@code null}
 * @since 2.0
 */
@NonNull
public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, @NonNull TimeUnit unit) {
    final Worker w = createWorker();
    final Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
    PeriodicDirectTask periodicTask = new PeriodicDirectTask(decoratedRun, w);
    Disposable d = w.schedulePeriodically(periodicTask, initialDelay, period, unit);
    if (d == EmptyDisposable.INSTANCE) {
        return d;
    }
    return periodicTask;
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable)

Aggregations

Test (org.junit.Test)20 Disposable (io.reactivex.rxjava3.disposables.Disposable)15 NonNull (io.reactivex.rxjava3.annotations.NonNull)10 InOrder (org.mockito.InOrder)10 TestException (io.reactivex.rxjava3.exceptions.TestException)9 IOException (java.io.IOException)9 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)7 Assert (org.junit.Assert)7 java.util.stream (java.util.stream)6 Iterator (java.util.Iterator)5 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)4 QueueFuseable (io.reactivex.rxjava3.operators.QueueFuseable)4 QueueSubscription (io.reactivex.rxjava3.operators.QueueSubscription)4 io.reactivex.rxjava3.testsupport (io.reactivex.rxjava3.testsupport)4 Mockito (org.mockito.Mockito)4 Subscription (org.reactivestreams.Subscription)4 Schedulers (io.reactivex.rxjava3.schedulers.Schedulers)3 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)3 TestHelper (io.reactivex.rxjava3.testsupport.TestHelper)3 Function (io.reactivex.rxjava3.functions.Function)2