Search in sources :

Example 46 with Function

use of org.apache.cassandra.cql3.functions.Function in project RxRelay by JakeWharton.

the class ReplayRelayTest method testUnsubscriptionCase.

@Test(timeout = 1000)
public void testUnsubscriptionCase() {
    ReplayRelay<String> src = ReplayRelay.create();
    for (int i = 0; i < 10; i++) {
        final Observer<Object> o = TestHelper.mockObserver();
        InOrder inOrder = inOrder(o);
        String v = "" + i;
        src.accept(v);
        System.out.printf("Turn: %d%n", i);
        src.firstElement().toObservable().flatMap(new Function<String, Observable<String>>() {

            @Override
            public Observable<String> apply(String t1) {
                return Observable.just(t1 + ", " + t1);
            }
        }).subscribe(new DefaultObserver<String>() {

            @Override
            public void onNext(String t) {
                System.out.println(t);
                o.onNext(t);
            }

            @Override
            public void onError(Throwable e) {
                o.onError(e);
            }

            @Override
            public void onComplete() {
                o.onComplete();
            }
        });
        inOrder.verify(o).onNext("0, 0");
        inOrder.verify(o).onComplete();
        verify(o, never()).onError(any(Throwable.class));
    }
}
Also used : Function(io.reactivex.rxjava3.functions.Function) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 47 with Function

use of org.apache.cassandra.cql3.functions.Function in project RxRelay by JakeWharton.

the class PublishRelayTest method testUnsubscriptionCase.

@Test(timeout = 1000)
public void testUnsubscriptionCase() {
    PublishRelay<String> src = PublishRelay.create();
    for (int i = 0; i < 10; i++) {
        final Observer<Object> o = TestHelper.mockObserver();
        InOrder inOrder = inOrder(o);
        String v = "" + i;
        System.out.printf("Turn: %d%n", i);
        src.firstElement().toObservable().flatMap(new Function<String, Observable<String>>() {

            @Override
            public Observable<String> apply(String t1) {
                return Observable.just(t1 + ", " + t1);
            }
        }).subscribe(new DefaultObserver<String>() {

            @Override
            public void onNext(String t) {
                o.onNext(t);
            }

            @Override
            public void onError(Throwable e) {
                o.onError(e);
            }

            @Override
            public void onComplete() {
                o.onComplete();
            }
        });
        src.accept(v);
        inOrder.verify(o).onNext(v + ", " + v);
        inOrder.verify(o).onComplete();
        verify(o, never()).onError(any(Throwable.class));
    }
}
Also used : Function(io.reactivex.rxjava3.functions.Function) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 48 with Function

use of org.apache.cassandra.cql3.functions.Function in project RxRelay by JakeWharton.

the class PublishRelayTest method testNestedSubscribe.

@Test
@SuppressWarnings("CheckReturnValue")
public void testNestedSubscribe() {
    final PublishRelay<Integer> s = PublishRelay.create();
    final AtomicInteger countParent = new AtomicInteger();
    final AtomicInteger countChildren = new AtomicInteger();
    final AtomicInteger countTotal = new AtomicInteger();
    final ArrayList<String> list = new ArrayList<String>();
    s.flatMap(new Function<Integer, Observable<String>>() {

        @Override
        public Observable<String> apply(final Integer v) {
            countParent.incrementAndGet();
            // then subscribe to subject again (it will not receive the previous value)
            return s.map(new Function<Integer, String>() {

                @Override
                public String apply(Integer v2) {
                    countChildren.incrementAndGet();
                    return "Parent: " + v + " Child: " + v2;
                }
            });
        }
    }).subscribe(new Consumer<String>() {

        @Override
        public void accept(String v) {
            countTotal.incrementAndGet();
            list.add(v);
        }
    });
    for (int i = 0; i < 10; i++) {
        s.accept(i);
    }
    // System.out.println("countParent: " + countParent.get());
    // System.out.println("countChildren: " + countChildren.get());
    // System.out.println("countTotal: " + countTotal.get());
    // 9+8+7+6+5+4+3+2+1+0 == 45
    assertEquals(45, list.size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(io.reactivex.rxjava3.functions.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 49 with Function

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

the class SingleTimerTest 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 = Single.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 50 with Function

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

the class FlowableOnErrorResumeNextViaFunctionTest method normalBackpressure.

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

        @Override
        public Flowable<Integer> apply(Throwable v) {
            return Flowable.range(3, 2);
        }
    }).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, 4);
    ts.assertNoErrors();
    ts.assertComplete();
}
Also used : Function(io.reactivex.rxjava3.functions.Function) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

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