Search in sources :

Example 1 with ReactiveSubscriber

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

the class FutureStreamImplTest method subcribeOnce.

public void subcribeOnce() {
    AtomicBoolean called1 = new AtomicBoolean(false);
    AtomicBoolean called2 = new AtomicBoolean(false);
    ReactiveSeq<Integer> initialStream = ReactiveSeq.of(1, 2, 3, 4, 5, 6);
    ReactiveSubscriber<Integer> sub1 = Spouts.reactiveSubscriber();
    ReactiveSubscriber<Integer> sub2 = Spouts.reactiveSubscriber();
    FutureStream<Integer> futureStream = FutureStream.builder().fromStream(initialStream).map(v -> v - 1);
    futureStream.subscribe(sub1);
    futureStream.subscribe(sub2);
    CompletableFuture future1 = CompletableFuture.runAsync(() -> sub1.reactiveStream().peek(a -> called1.set(true)).forEach(v -> System.out.println("1 -> " + v)));
    CompletableFuture future2 = CompletableFuture.runAsync(() -> sub2.reactiveStream().peek(a -> called2.set(true)).forEach(v -> System.out.println("2 -> " + v)));
    try {
        future1.get();
        future2.get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    assertThat(called1.get(), equalTo(true));
    assertThat(called1.get(), equalTo(false));
}
Also used : Spouts(cyclops.reactive.Spouts) ReactiveSubscriber(com.oath.cyclops.types.reactive.ReactiveSubscriber) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) Topic(com.oath.cyclops.async.adapters.Topic) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionException(java.util.concurrent.ExecutionException) ReactiveSeq(cyclops.reactive.ReactiveSeq) Assert.assertFalse(org.junit.Assert.assertFalse) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) FutureStreamImpl(com.oath.cyclops.internal.react.FutureStreamImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ReactiveSubscriber

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

the class FlatMapTest method flatMapAsyncRS2.

@Test
public void flatMapAsyncRS2() {
    for (int k = 0; k < 1000; k++) {
        System.out.println("********0---------------------K " + k);
        ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber();
        Spouts.of(1, 2, 3).peek(System.out::println).flatMap(i -> nextAsyncRS()).subscribe(sub);
        List<Integer> res = sub.reactiveStream().collect(Collectors.toList());
        System.out.println(res);
        assertThat(res.size(), equalTo(Arrays.asList(1, 2, 1, 2, 1, 2).size()));
        assertThat(res, hasItems(1, 2));
        int one = 0;
        int two = 0;
        for (Integer next : res) {
            if (next == 1) {
                one++;
            }
            if (next == 2) {
                two++;
            }
        }
        assertThat(one, equalTo(3));
        assertThat(two, equalTo(3));
    }
}
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) Test(org.junit.Test)

Example 3 with ReactiveSubscriber

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

the class FlatMapPublisherTest method flatMapPAsyncRS.

@Test
public void flatMapPAsyncRS() {
    for (int k = 0; k < 1000; k++) {
        System.out.println("****************************NEXT ITERATION " + k);
        System.out.println("****************************NEXT ITERATION " + k);
        System.out.println("****************************NEXT ITERATION " + k);
        System.out.println("****************************NEXT ITERATION " + k);
        System.out.println("****************************NEXT ITERATION " + k);
        System.out.println("****************************NEXT ITERATION " + k);
        System.out.println("****************************NEXT ITERATION " + k + "*************************!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        complete = new AtomicBoolean(false);
        count = new AtomicInteger(0);
        ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber();
        Spouts.of(1, 2, 3).peek(System.out::println).mergeMap(i -> nextAsync()).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) Arrays(java.util.Arrays) Spouts.of(cyclops.reactive.Spouts.of) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Matchers.hasItems(org.hamcrest.Matchers.hasItems) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(reactor.core.scheduler.Schedulers) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Subscriber(org.reactivestreams.Subscriber) Maybe(cyclops.control.Maybe) Spouts(cyclops.reactive.Spouts) Iterator(java.util.Iterator) ReactiveSubscriber(com.oath.cyclops.types.reactive.ReactiveSubscriber) Executor(java.util.concurrent.Executor) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ReactiveSeq(cyclops.reactive.ReactiveSeq) Flux(reactor.core.publisher.Flux) List(java.util.List) MatcherAssert(org.hamcrest.MatcherAssert) ForkJoinPool(java.util.concurrent.ForkJoinPool) Subscription(org.reactivestreams.Subscription) Assert(org.junit.Assert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 4 with ReactiveSubscriber

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

the class FlatMapTest method flatMapAsyncRS2.

@Test
public void flatMapAsyncRS2() {
    for (int k = 0; k < 1000; k++) {
        System.out.println("********0---------------------K " + k);
        ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber();
        Spouts.of(1, 2, 3).peek(System.out::println).flatMap(i -> nextAsyncRS()).subscribe(sub);
        List<Integer> res = sub.reactiveStream().collect(Collectors.toList());
        System.out.println(res);
        assertThat(res.size(), equalTo(Arrays.asList(1, 2, 1, 2, 1, 2).size()));
        assertThat(res, hasItems(1, 2));
        int one = 0;
        int two = 0;
        for (Integer next : res) {
            if (next == 1) {
                one++;
            }
            if (next == 2) {
                two++;
            }
        }
        assertThat(one, equalTo(3));
        assertThat(two, equalTo(3));
    }
}
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) Test(org.junit.Test)

Example 5 with ReactiveSubscriber

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

the class IterableFlatMapTest method flatMapAsyncRS2.

@Test
public void flatMapAsyncRS2() {
    for (int k = 0; k < 1000; k++) {
        System.out.println("********0---------------------K " + k);
        ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber();
        Spouts.of(1, 2, 3).peek(System.out::println).concatMap(i -> nextAsyncRS()).subscribe(sub);
        List<Integer> res = sub.reactiveStream().collect(Collectors.toList());
        System.out.println(res);
        assertThat(res.size(), equalTo(Arrays.asList(1, 2, 1, 2, 1, 2).size()));
        assertThat(res, hasItems(1, 2));
        int one = 0;
        int two = 0;
        for (Integer next : res) {
            if (next == 1) {
                one++;
            }
            if (next == 2) {
                two++;
            }
        }
        assertThat(one, equalTo(3));
        assertThat(two, equalTo(3));
    }
}
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) Test(org.junit.Test)

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