Search in sources :

Example 11 with Predicate

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

the class TestObserverExTest method assertError.

@Test
public void assertError() {
    TestObserverEx<Integer> to = new TestObserverEx<>();
    try {
        to.assertError(TestException.class);
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError ex) {
    // expected
    }
    try {
        to.assertError(new TestException());
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError ex) {
    // expected
    }
    try {
        to.assertError(Functions.<Throwable>alwaysTrue());
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError ex) {
    // expected
    }
    try {
        to.assertErrorMessage("");
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    try {
        to.assertSubscribed();
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    try {
        to.assertTerminated();
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    to.onSubscribe(Disposable.empty());
    to.assertSubscribed();
    to.assertNoErrors();
    TestException ex = new TestException("Forced failure");
    to.onError(ex);
    to.assertError(ex);
    to.assertError(TestException.class);
    to.assertError(Functions.<Throwable>alwaysTrue());
    to.assertError(new Predicate<Throwable>() {

        @Override
        public boolean test(Throwable t) throws Exception {
            return t.getMessage() != null && t.getMessage().contains("Forced");
        }
    });
    to.assertErrorMessage("Forced failure");
    try {
        to.assertErrorMessage("");
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    try {
        to.assertError(new RuntimeException());
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    try {
        to.assertError(IOException.class);
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    try {
        to.assertError(Functions.<Throwable>alwaysFalse());
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    try {
        to.assertNoErrors();
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    to.assertTerminated();
    to.assertValueCount(0);
    to.assertNoValues();
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 12 with Predicate

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

the class FlowableSubscriberTest method doubleSubscribe.

@Test
public void doubleSubscribe() {
    ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) throws Exception {
            return true;
        }
    }, Functions.<Throwable>emptyConsumer(), Functions.EMPTY_ACTION);
    List<Throwable> list = TestHelper.trackPluginErrors();
    try {
        s.onSubscribe(new BooleanSubscription());
        BooleanSubscription bs = new BooleanSubscription();
        s.onSubscribe(bs);
        assertTrue(bs.isCancelled());
        TestHelper.assertError(list, 0, IllegalStateException.class, "Subscription already set!");
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : ForEachWhileSubscriber(io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 13 with Predicate

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

the class FlowableSubscriberTest method onNextCrashes.

@Test
public void onNextCrashes() {
    final TestSubscriber<Integer> ts = new TestSubscriber<>();
    ts.onSubscribe(new BooleanSubscription());
    ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) throws Exception {
            throw new TestException();
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable e) throws Exception {
            ts.onError(e);
        }
    }, new Action() {

        @Override
        public void run() throws Exception {
            ts.onComplete();
        }
    });
    BooleanSubscription b = new BooleanSubscription();
    s.onSubscribe(b);
    s.onNext(1);
    assertTrue(b.isCancelled());
    ts.assertFailure(TestException.class);
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) ForEachWhileSubscriber(io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber) Test(org.junit.Test)

Example 14 with Predicate

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

the class FlowableSubscriberTest method suppressAfterCompleteEvents.

@Test
public void suppressAfterCompleteEvents() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        final TestSubscriber<Integer> ts = new TestSubscriber<>();
        ts.onSubscribe(new BooleanSubscription());
        ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() {

            @Override
            public boolean test(Integer v) throws Exception {
                ts.onNext(v);
                return true;
            }
        }, new Consumer<Throwable>() {

            @Override
            public void accept(Throwable e) throws Exception {
                ts.onError(e);
            }
        }, new Action() {

            @Override
            public void run() throws Exception {
                ts.onComplete();
            }
        });
        s.onComplete();
        s.onNext(1);
        s.onError(new TestException());
        s.onComplete();
        ts.assertResult();
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) ForEachWhileSubscriber(io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber) Test(org.junit.Test)

Example 15 with Predicate

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

the class FlowableSubscriberTest method forEachWhile.

@Test
public void forEachWhile() {
    PublishProcessor<Integer> pp = PublishProcessor.create();
    final List<Integer> list = new ArrayList<>();
    Disposable d = pp.forEachWhile(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) throws Exception {
            list.add(v);
            return v < 3;
        }
    });
    assertFalse(d.isDisposed());
    pp.onNext(1);
    pp.onNext(2);
    pp.onNext(3);
    assertFalse(pp.hasSubscribers());
    assertEquals(Arrays.asList(1, 2, 3), list);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)31 TestException (io.reactivex.rxjava3.exceptions.TestException)25 Predicate (io.reactivex.rxjava3.functions.Predicate)12 InOrder (org.mockito.InOrder)12 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)11 IOException (java.io.IOException)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)5 CompletableSubject (io.reactivex.rxjava3.subjects.CompletableSubject)3 Observable (io.reactivex.rxjava3.core.Observable)2 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)2 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)2 SubscriptionArbiter (io.reactivex.rxjava3.internal.subscriptions.SubscriptionArbiter)2 ConditionalSubscriber (io.reactivex.rxjava3.operators.ConditionalSubscriber)2 Callable (java.util.concurrent.Callable)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Flowable (io.reactivex.rxjava3.core.Flowable)1 Disposable (io.reactivex.rxjava3.disposables.Disposable)1 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)1