Search in sources :

Example 21 with Function

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

the class FlowableOnErrorReturnTest method normalBackpressure.

@Test
public void normalBackpressure() {
    TestSubscriber<Integer> ts = TestSubscriber.create(0);
    PublishProcessor<Integer> pp = PublishProcessor.create();
    pp.onErrorReturn(new Function<Throwable, Integer>() {

        @Override
        public Integer apply(Throwable e) {
            return 3;
        }
    }).subscribe(ts);
    ts.request(2);
    pp.onNext(1);
    pp.onNext(2);
    pp.onError(new TestException("Forced failure"));
    ts.assertValues(1, 2);
    ts.assertNoErrors();
    ts.assertNotComplete();
    ts.request(2);
    ts.assertValues(1, 2, 3);
    ts.assertNoErrors();
    ts.assertComplete();
}
Also used : Function(io.reactivex.rxjava3.functions.Function) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 22 with Function

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

the class FlowableOnErrorResumeNextViaFunctionTest method resumeNextWithAsyncExecution.

@Test
public void resumeNextWithAsyncExecution() {
    final AtomicReference<Throwable> receivedException = new AtomicReference<>();
    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) {
            receivedException.set(t1);
            return Flowable.just("twoResume", "threeResume");
        }
    };
    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());
    }
    verify(subscriber, Mockito.never()).onError(any(Throwable.class));
    verify(subscriber, times(1)).onComplete();
    verify(subscriber, times(1)).onNext("one");
    verify(subscriber, Mockito.never()).onNext("two");
    verify(subscriber, Mockito.never()).onNext("three");
    verify(subscriber, times(1)).onNext("twoResume");
    verify(subscriber, times(1)).onNext("threeResume");
    assertNotNull(receivedException.get());
}
Also used : Function(io.reactivex.rxjava3.functions.Function) AtomicReference(java.util.concurrent.atomic.AtomicReference) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 23 with Function

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

the class ObservableOnErrorResumeNextTest method functionThrowsError.

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

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

Example 24 with Function

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

the class ObservableOnErrorResumeNextTest method resumeNextWithAsyncExecution.

@Test
public void resumeNextWithAsyncExecution() {
    final AtomicReference<Throwable> receivedException = new AtomicReference<>();
    Subscription s = mock(Subscription.class);
    TestObservable w = new TestObservable(s, "one");
    Function<Throwable, Observable<String>> resume = new Function<Throwable, Observable<String>>() {

        @Override
        public Observable<String> apply(Throwable t1) {
            receivedException.set(t1);
            return Observable.just("twoResume", "threeResume");
        }
    };
    Observable<String> o = Observable.unsafeCreate(w).onErrorResumeNext(resume);
    Observer<String> observer = TestHelper.mockObserver();
    o.subscribe(observer);
    try {
        w.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");
    assertNotNull(receivedException.get());
}
Also used : Function(io.reactivex.rxjava3.functions.Function) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 25 with Function

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

the class FlowableTimerTest 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();
            TestSubscriber<Long> ts = Flowable.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);
            ts.cancel();
            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