Search in sources :

Example 6 with ReactiveSubscriber

use of com.oath.cyclops.types.reactive.ReactiveSubscriber in project cyclops by aol.

the class IterableFlatMapTest method flatMapAsyncRS.

@Test
public void flatMapAsyncRS() {
    for (int k = 0; k < 1000; k++) {
        complete = new AtomicBoolean(false);
        count = new AtomicInteger(0);
        ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber();
        Spouts.of(1, 2, 3).peek(System.out::println).concatMap(i -> nextAsyncRS()).subscribe(new Subscriber<Integer>() {

            @Override
            public void onSubscribe(Subscription s) {
                subs = s;
            }

            @Override
            public void onNext(Integer integer) {
                System.out.println("RECIEVED " + integer);
                assertThat(integer, Matchers.isOneOf(1, 2));
                System.out.println("count " + count.incrementAndGet());
            }

            @Override
            public void onError(Throwable t) {
            }

            @Override
            public void onComplete() {
                complete.set(true);
            }
        });
        subs.request(Long.MAX_VALUE);
        while (!complete.get()) {
        }
        assertThat(count.get(), equalTo(6));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Maybe(cyclops.control.Maybe) Arrays(java.util.Arrays) Spouts.of(cyclops.reactive.Spouts.of) Spouts(cyclops.reactive.Spouts) ReactiveSubscriber(com.oath.cyclops.types.reactive.ReactiveSubscriber) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Matchers(org.hamcrest.Matchers) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Matchers.hasItems(org.hamcrest.Matchers.hasItems) ReactiveSeq(cyclops.reactive.ReactiveSeq) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForkJoinPool(java.util.concurrent.ForkJoinPool) Subscription(org.reactivestreams.Subscription) Schedulers(reactor.core.scheduler.Schedulers) AsyncSubscriber(com.oath.cyclops.types.reactive.AsyncSubscriber) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assert(org.junit.Assert) Subscriber(org.reactivestreams.Subscriber) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 7 with ReactiveSubscriber

use of com.oath.cyclops.types.reactive.ReactiveSubscriber in project cyclops by aol.

the class FlatMapTest method flatMapAsyncRS.

@Test
public void flatMapAsyncRS() {
    for (int k = 0; k < 1000; k++) {
        complete = new AtomicBoolean(false);
        count = new AtomicInteger(0);
        ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber();
        Spouts.of(1, 2, 3).peek(System.out::println).flatMap(i -> nextAsyncRS()).subscribe(new Subscriber<Integer>() {

            @Override
            public void onSubscribe(Subscription s) {
                subs = s;
            }

            @Override
            public void onNext(Integer integer) {
                System.out.println("RECIEVED " + integer);
                assertThat(integer, Matchers.isOneOf(1, 2));
                System.out.println("count " + count.incrementAndGet());
            }

            @Override
            public void onError(Throwable t) {
            }

            @Override
            public void onComplete() {
                complete.set(true);
            }
        });
        subs.request(Long.MAX_VALUE);
        while (!complete.get()) {
        }
        assertThat(count.get(), equalTo(6));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Maybe(cyclops.control.Maybe) Arrays(java.util.Arrays) Spouts.of(cyclops.reactive.Spouts.of) Spouts(cyclops.reactive.Spouts) ReactiveSubscriber(com.oath.cyclops.types.reactive.ReactiveSubscriber) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Matchers(org.hamcrest.Matchers) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Matchers.hasItems(org.hamcrest.Matchers.hasItems) ReactiveSeq(cyclops.reactive.ReactiveSeq) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Stream(java.util.stream.Stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForkJoinPool(java.util.concurrent.ForkJoinPool) Subscription(org.reactivestreams.Subscription) Schedulers(reactor.core.scheduler.Schedulers) AsyncSubscriber(com.oath.cyclops.types.reactive.AsyncSubscriber) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assert(org.junit.Assert) Subscriber(org.reactivestreams.Subscriber) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 8 with ReactiveSubscriber

use of com.oath.cyclops.types.reactive.ReactiveSubscriber in project cyclops by aol.

the class Spouts method schedule.

static <T> ReactiveSeq<T> schedule(final Stream<T> stream, final String cron, final ScheduledExecutorService exec) {
    ReactiveSubscriber<T> sub = reactiveSubscriber();
    AtomicBoolean isOpen = new AtomicBoolean(true);
    Subscription[] s = { null };
    sub.onSubscribe(new Subscription() {

        @Override
        public void request(long n) {
            s[0].request(n);
        }

        @Override
        public void cancel() {
            isOpen.set(false);
        }
    });
    s[0] = ReactiveSeq.fromStream(stream).takeWhile(e -> isOpen.get()).schedule(cron, exec).connect().forEach(0, e -> sub.onNext(e), t -> sub.onError(t), () -> sub.onComplete());
    return sub.reactiveStream();
}
Also used : CheckedSupplier(cyclops.function.checked.CheckedSupplier) Higher(com.oath.cyclops.hkt.Higher) Type(com.oath.cyclops.internal.stream.ReactiveStreamX.Type) Tuple2(cyclops.data.tuple.Tuple2) DataWitness.reactiveSeq(com.oath.cyclops.hkt.DataWitness.reactiveSeq) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) com.oath.cyclops.internal.stream.spliterators.push(com.oath.cyclops.internal.stream.spliterators.push) Seq(cyclops.data.Seq) UnfoldSpliterator(com.oath.cyclops.internal.stream.spliterators.UnfoldSpliterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Subscriber(org.reactivestreams.Subscriber) cyclops.control(cyclops.control) ReactiveSubscriber(com.oath.cyclops.types.reactive.ReactiveSubscriber) Executor(java.util.concurrent.Executor) ReactiveStreamX(com.oath.cyclops.internal.stream.ReactiveStreamX) Publisher(org.reactivestreams.Publisher) PushSubscriber(com.oath.cyclops.types.reactive.PushSubscriber) LockSupport(java.util.concurrent.locks.LockSupport) AtomicLong(java.util.concurrent.atomic.AtomicLong) Stream(java.util.stream.Stream) Subscription(org.reactivestreams.Subscription) IterableX(com.oath.cyclops.types.traversable.IterableX) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) ManyToManyConcurrentArrayQueue(org.agrona.concurrent.ManyToManyConcurrentArrayQueue) AsyncSubscriber(com.oath.cyclops.types.reactive.AsyncSubscriber) Queue(java.util.Queue) java.util.function(java.util.function) BufferOverflowPolicy(com.oath.cyclops.types.reactive.BufferOverflowPolicy) Spliterator(java.util.Spliterator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Subscription(org.reactivestreams.Subscription)

Example 9 with ReactiveSubscriber

use of com.oath.cyclops.types.reactive.ReactiveSubscriber in project cyclops by aol.

the class Spouts method interval.

static ReactiveSeq<Integer> interval(String cron, ScheduledExecutorService exec) {
    ReactiveSubscriber<Integer> sub = reactiveSubscriber();
    AtomicBoolean isOpen = new AtomicBoolean(true);
    Subscription[] s = { null };
    sub.onSubscribe(new Subscription() {

        @Override
        public void request(long n) {
            s[0].request(n);
        }

        @Override
        public void cancel() {
            isOpen.set(false);
        }
    });
    s[0] = ReactiveSeq.iterate(1, a -> a + 1).takeWhile(e -> isOpen.get()).schedule(cron, exec).connect().forEach(1, e -> sub.onNext(e));
    return sub.reactiveStream();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CheckedSupplier(cyclops.function.checked.CheckedSupplier) Higher(com.oath.cyclops.hkt.Higher) Type(com.oath.cyclops.internal.stream.ReactiveStreamX.Type) Tuple2(cyclops.data.tuple.Tuple2) DataWitness.reactiveSeq(com.oath.cyclops.hkt.DataWitness.reactiveSeq) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) com.oath.cyclops.internal.stream.spliterators.push(com.oath.cyclops.internal.stream.spliterators.push) Seq(cyclops.data.Seq) UnfoldSpliterator(com.oath.cyclops.internal.stream.spliterators.UnfoldSpliterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Subscriber(org.reactivestreams.Subscriber) cyclops.control(cyclops.control) ReactiveSubscriber(com.oath.cyclops.types.reactive.ReactiveSubscriber) Executor(java.util.concurrent.Executor) ReactiveStreamX(com.oath.cyclops.internal.stream.ReactiveStreamX) Publisher(org.reactivestreams.Publisher) PushSubscriber(com.oath.cyclops.types.reactive.PushSubscriber) LockSupport(java.util.concurrent.locks.LockSupport) AtomicLong(java.util.concurrent.atomic.AtomicLong) Stream(java.util.stream.Stream) Subscription(org.reactivestreams.Subscription) IterableX(com.oath.cyclops.types.traversable.IterableX) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) ManyToManyConcurrentArrayQueue(org.agrona.concurrent.ManyToManyConcurrentArrayQueue) AsyncSubscriber(com.oath.cyclops.types.reactive.AsyncSubscriber) Queue(java.util.Queue) java.util.function(java.util.function) BufferOverflowPolicy(com.oath.cyclops.types.reactive.BufferOverflowPolicy) Spliterator(java.util.Spliterator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Subscription(org.reactivestreams.Subscription)

Example 10 with ReactiveSubscriber

use of com.oath.cyclops.types.reactive.ReactiveSubscriber in project cyclops by aol.

the class Spouts method interval.

static ReactiveSeq<Integer> interval(final long millis, ScheduledExecutorService exec) {
    ReactiveSubscriber<Integer> sub = reactiveSubscriber();
    AtomicBoolean isOpen = new AtomicBoolean(true);
    Subscription[] s = { null };
    sub.onSubscribe(new Subscription() {

        @Override
        public void request(long n) {
            s[0].request(n);
        }

        @Override
        public void cancel() {
            isOpen.set(false);
        }
    });
    s[0] = ReactiveSeq.iterate(1, a -> a + 1).takeWhile(e -> isOpen.get()).scheduleFixedDelay(millis, exec).connect().forEach(1, e -> sub.onNext(e));
    return sub.reactiveStream();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CheckedSupplier(cyclops.function.checked.CheckedSupplier) Higher(com.oath.cyclops.hkt.Higher) Type(com.oath.cyclops.internal.stream.ReactiveStreamX.Type) Tuple2(cyclops.data.tuple.Tuple2) DataWitness.reactiveSeq(com.oath.cyclops.hkt.DataWitness.reactiveSeq) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) com.oath.cyclops.internal.stream.spliterators.push(com.oath.cyclops.internal.stream.spliterators.push) Seq(cyclops.data.Seq) UnfoldSpliterator(com.oath.cyclops.internal.stream.spliterators.UnfoldSpliterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Subscriber(org.reactivestreams.Subscriber) cyclops.control(cyclops.control) ReactiveSubscriber(com.oath.cyclops.types.reactive.ReactiveSubscriber) Executor(java.util.concurrent.Executor) ReactiveStreamX(com.oath.cyclops.internal.stream.ReactiveStreamX) Publisher(org.reactivestreams.Publisher) PushSubscriber(com.oath.cyclops.types.reactive.PushSubscriber) LockSupport(java.util.concurrent.locks.LockSupport) AtomicLong(java.util.concurrent.atomic.AtomicLong) Stream(java.util.stream.Stream) Subscription(org.reactivestreams.Subscription) IterableX(com.oath.cyclops.types.traversable.IterableX) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) ManyToManyConcurrentArrayQueue(org.agrona.concurrent.ManyToManyConcurrentArrayQueue) AsyncSubscriber(com.oath.cyclops.types.reactive.AsyncSubscriber) Queue(java.util.Queue) java.util.function(java.util.function) BufferOverflowPolicy(com.oath.cyclops.types.reactive.BufferOverflowPolicy) Spliterator(java.util.Spliterator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Subscription(org.reactivestreams.Subscription)

Aggregations

ReactiveSubscriber (com.oath.cyclops.types.reactive.ReactiveSubscriber)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 Subscriber (org.reactivestreams.Subscriber)12 Subscription (org.reactivestreams.Subscription)12 AsyncSubscriber (com.oath.cyclops.types.reactive.AsyncSubscriber)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)11 ReactiveSeq (cyclops.reactive.ReactiveSeq)10 Spouts (cyclops.reactive.Spouts)10 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)10 Test (org.junit.Test)10 Maybe (cyclops.control.Maybe)9 Spouts.of (cyclops.reactive.Spouts.of)9 Arrays (java.util.Arrays)9 List (java.util.List)9 ForkJoinPool (java.util.concurrent.ForkJoinPool)9 Collectors (java.util.stream.Collectors)9 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)9 Matchers (org.hamcrest.Matchers)9 Matchers.hasItems (org.hamcrest.Matchers.hasItems)9