Search in sources :

Example 51 with Function

use of org.apache.cassandra.cql3.functions.Function in project RxJava by ReactiveX.

the class FlowableOnErrorResumeNextViaFunctionTest method functionThrowsError.

/**
 * Test that when a function throws an exception this is propagated through onError.
 */
@Test
public void functionThrowsError() {
    Subscription s = mock(Subscription.class);
    TestFlowable w = new TestFlowable(s, "one");
    Function<Throwable, Flowable<String>> resume = new Function<Throwable, Flowable<String>>() {

        @Override
        public Flowable<String> apply(Throwable t1) {
            throw new RuntimeException("exception from function");
        }
    };
    Flowable<String> flowable = Flowable.unsafeCreate(w).onErrorResumeNext(resume);
    Subscriber<String> subscriber = TestHelper.mockSubscriber();
    flowable.subscribe(subscriber);
    try {
        w.t.join();
    } catch (InterruptedException e) {
        fail(e.getMessage());
    }
    // we should get the "one" value before the error
    verify(subscriber, times(1)).onNext("one");
    // we should have received an onError call on the Observer since the resume function threw an exception
    verify(subscriber, times(1)).onError(any(Throwable.class));
    verify(subscriber, times(0)).onComplete();
}
Also used : Function(io.reactivex.rxjava3.functions.Function) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 52 with Function

use of org.apache.cassandra.cql3.functions.Function in project RxJava by ReactiveX.

the class ObservableOnErrorResumeWithTest method mapResumeAsyncNext.

@Test
public void mapResumeAsyncNext() {
    Disposable sr = mock(Disposable.class);
    // Trigger multiple failures
    Observable<String> w = Observable.just("one", "fail", "two", "three", "fail");
    // Resume Observable is async
    TestObservable f = new TestObservable(sr, "twoResume", "threeResume");
    Observable<String> resume = Observable.unsafeCreate(f);
    // Introduce map function that fails intermittently (Map does not prevent this when the Observer is a
    // rx.operator incl onErrorResumeNextViaObservable)
    w = w.map(new Function<String, String>() {

        @Override
        public String apply(String s) {
            if ("fail".equals(s)) {
                throw new RuntimeException("Forced Failure");
            }
            System.out.println("BadMapper:" + s);
            return s;
        }
    });
    Observable<String> observable = w.onErrorResumeWith(resume);
    Observer<String> observer = TestHelper.mockObserver();
    observable.subscribe(observer);
    try {
        f.t.join();
    } catch (InterruptedException e) {
        fail(e.getMessage());
    }
    verify(observer, Mockito.never()).onError(any(Throwable.class));
    verify(observer, times(1)).onComplete();
    verify(observer, times(1)).onNext("one");
    verify(observer, Mockito.never()).onNext("two");
    verify(observer, Mockito.never()).onNext("three");
    verify(observer, times(1)).onNext("twoResume");
    verify(observer, times(1)).onNext("threeResume");
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Function(io.reactivex.rxjava3.functions.Function) Test(org.junit.Test)

Example 53 with Function

use of org.apache.cassandra.cql3.functions.Function in project RxJava by ReactiveX.

the class FlowableDebounceTest method emitLate.

@Test
public void emitLate() {
    final AtomicReference<Subscriber<? super Integer>> ref = new AtomicReference<>();
    TestSubscriber<Integer> ts = Flowable.range(1, 2).debounce(new Function<Integer, Flowable<Integer>>() {

        @Override
        public Flowable<Integer> apply(Integer o) throws Exception {
            if (o != 1) {
                return Flowable.never();
            }
            return new Flowable<Integer>() {

                @Override
                protected void subscribeActual(Subscriber<? super Integer> subscriber) {
                    subscriber.onSubscribe(new BooleanSubscription());
                    ref.set(subscriber);
                }
            };
        }
    }).test();
    ref.get().onNext(1);
    ts.assertResult(2);
}
Also used : Function(io.reactivex.rxjava3.functions.Function) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 54 with Function

use of org.apache.cassandra.cql3.functions.Function in project RxJava by ReactiveX.

the class MaybeTimerTest method timerInterruptible.

@Test
public void timerInterruptible() throws Exception {
    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    try {
        for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec, true) }) {
            final AtomicBoolean interrupted = new AtomicBoolean();
            TestObserver<Long> to = Maybe.timer(1, TimeUnit.MILLISECONDS, s).map(new Function<Long, Long>() {

                @Override
                public Long apply(Long v) throws Exception {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException ex) {
                        interrupted.set(true);
                    }
                    return v;
                }
            }).test();
            Thread.sleep(500);
            to.dispose();
            Thread.sleep(500);
            assertTrue(s.getClass().getSimpleName(), interrupted.get());
        }
    } finally {
        exec.shutdown();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(io.reactivex.rxjava3.functions.Function) Test(org.junit.Test)

Example 55 with Function

use of org.apache.cassandra.cql3.functions.Function in project RxJava by ReactiveX.

the class ObservableTimerTest method timerInterruptible.

@Test
public void timerInterruptible() throws Exception {
    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    try {
        for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec, true) }) {
            final AtomicBoolean interrupted = new AtomicBoolean();
            TestObserver<Long> to = Observable.timer(1, TimeUnit.MILLISECONDS, s).map(new Function<Long, Long>() {

                @Override
                public Long apply(Long v) throws Exception {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException ex) {
                        interrupted.set(true);
                    }
                    return v;
                }
            }).test();
            Thread.sleep(500);
            to.dispose();
            Thread.sleep(500);
            assertTrue(s.getClass().getSimpleName(), interrupted.get());
        }
    } finally {
        exec.shutdown();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(io.reactivex.rxjava3.functions.Function)

Aggregations

Test (org.junit.Test)34 Function (io.reactivex.rxjava3.functions.Function)20 FunctionName (org.apache.cassandra.cql3.functions.FunctionName)13 ByteBuffer (java.nio.ByteBuffer)7 Function (org.apache.cassandra.cql3.functions.Function)7 AbstractType (org.apache.cassandra.db.marshal.AbstractType)6 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)6 KeyspaceMetadata (org.apache.cassandra.schema.KeyspaceMetadata)6 TableMetadata (org.apache.cassandra.schema.TableMetadata)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)5 ProtocolVersion (org.apache.cassandra.transport.ProtocolVersion)5 InOrder (org.mockito.InOrder)5 List (java.util.List)4 UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)4 UDFunction (org.apache.cassandra.cql3.functions.UDFunction)4 ViewMetadata (org.apache.cassandra.schema.ViewMetadata)4 TestException (io.reactivex.rxjava3.exceptions.TestException)3 java.util (java.util)3 ArrayList (java.util.ArrayList)3