Search in sources :

Example 21 with SimpleReact

use of cyclops.futurestream.SimpleReact in project cyclops by aol.

the class QueueTest method backPressureJDKTest.

@Test
public void backPressureJDKTest() {
    Queue<String> q = new Queue<>(new LinkedBlockingQueue<>(2));
    new SimpleReact().ofAsync(() -> {
        Stream.of("1", "2", "3", "4").forEach(it -> {
            q.offer(it);
            found.getAndAdd(1);
        });
        return 1;
    });
    sleep(10);
    assertThat(found.get(), is(2));
    assertThat(q.stream().limit(2).collect(Collectors.toList()).size(), is(2));
    assertThat(q.stream().limit(2).collect(Collectors.toList()).size(), is(2));
    sleep(10);
    assertThat(found.get(), is(4));
}
Also used : SimpleReact(cyclops.futurestream.SimpleReact) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Example 22 with SimpleReact

use of cyclops.futurestream.SimpleReact in project cyclops by aol.

the class QueueTest method queueTestTimeout.

@Test
public void queueTestTimeout() {
    Queue<Integer> q = new Queue<>(new LinkedBlockingQueue<Integer>()).withTimeout(1).withTimeUnit(TimeUnit.MILLISECONDS);
    new SimpleReact().ofAsync(() -> q.offer(1), () -> q.offer(2), () -> {
        sleep(500);
        return q.offer(4);
    }, () -> q.offer(5), () -> {
        sleep(1000);
        return q.close();
    });
    Collection<String> results = parallel().fromStream(q.stream()).then(it -> "*" + it).block();
    assertThat(results.size(), equalTo(4));
    assertThat(results, hasItem("*5"));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Matchers.not(org.hamcrest.Matchers.not) BaseSimpleReactStream(com.oath.cyclops.types.futurestream.BaseSimpleReactStream) Assert.assertThat(org.junit.Assert.assertThat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Before(org.junit.Before) ReactiveSeq.of(cyclops.reactive.ReactiveSeq.of) Collection(java.util.Collection) BaseSimpleReactStream.parallel(com.oath.cyclops.types.futurestream.BaseSimpleReactStream.parallel) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Stream(java.util.stream.Stream) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) SimpleReact(cyclops.futurestream.SimpleReact) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Example 23 with SimpleReact

use of cyclops.futurestream.SimpleReact in project cyclops by aol.

the class QueueTest method backPressureTest.

@Test
public void backPressureTest() {
    Queue<Integer> q = new Queue<>(new LinkedBlockingQueue<>(2));
    new SimpleReact().ofAsync(() -> {
        q.offer(1);
        return found.getAndAdd(1);
    }, () -> {
        q.offer(1);
        return found.getAndAdd(1);
    }, () -> {
        q.offer(6);
        return found.getAndAdd(1);
    }, () -> {
        q.offer(5);
        return found.getAndAdd(1);
    });
    sleep(10);
    assertThat(found.get(), is(2));
    assertThat(q.stream().limit(2).collect(Collectors.toList()).size(), is(2));
    assertThat(q.stream().limit(2).collect(Collectors.toList()).size(), is(2));
    sleep(10);
    assertThat(found.get(), is(4));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SimpleReact(cyclops.futurestream.SimpleReact) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Example 24 with SimpleReact

use of cyclops.futurestream.SimpleReact in project cyclops by aol.

the class LazyStream method run.

/**
 * Trigger a lazy stream as a task on the provided Executor
 */
default void run() {
    final SimpleReact reactor = SequentialElasticPools.simpleReact.nextReactor();
    reactor.ofAsync(() -> run(new NonCollector<>())).peek(n -> SequentialElasticPools.simpleReact.populate(reactor)).onFail(n -> {
        SequentialElasticPools.simpleReact.populate(reactor);
        return 1;
    });
}
Also used : MissingValue(com.oath.cyclops.internal.react.stream.MissingValue) SimpleReactProcessingException(com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException) Iterator(java.util.Iterator) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) LazyResultConsumer(com.oath.cyclops.react.collectors.lazy.LazyResultConsumer) BiFunction(java.util.function.BiFunction) Runner(com.oath.cyclops.internal.react.stream.Runner) MaxActive(com.oath.cyclops.react.collectors.lazy.MaxActive) IncrementalReducer(com.oath.cyclops.react.collectors.lazy.IncrementalReducer) Function(java.util.function.Function) Supplier(java.util.function.Supplier) EmptyCollector(com.oath.cyclops.react.collectors.lazy.EmptyCollector) BinaryOperator(java.util.function.BinaryOperator) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) LazyStreamWrapper(com.oath.cyclops.internal.react.stream.LazyStreamWrapper) SequentialElasticPools(com.oath.cyclops.react.threads.SequentialElasticPools) BiConsumer(java.util.function.BiConsumer) SimpleReact(cyclops.futurestream.SimpleReact) Optional(java.util.Optional) Collector(java.util.stream.Collector) SimpleReact(cyclops.futurestream.SimpleReact)

Example 25 with SimpleReact

use of cyclops.futurestream.SimpleReact in project cyclops by aol.

the class OnFailTest method chained2.

@Test
public void chained2() {
    new SimpleReact().ofAsync(() -> 1, () -> 2).then(this::throwException).then(i -> i + 2).onFail(IOException.class, e -> shouldNeverBeCalled.incrementAndGet()).onFail(RuntimeException.class, e -> shouldBeCalled.incrementAndGet()).onFail(ClosedQueueException.class, e -> shouldNeverBeReached.incrementAndGet()).block();
    assertThat(shouldNeverBeCalled.get(), equalTo(0));
    assertThat(shouldBeCalled.get(), equalTo(2));
    assertThat(shouldNeverBeReached.get(), equalTo(0));
}
Also used : Assert.assertThat(org.junit.Assert.assertThat) ClosedQueueException(com.oath.cyclops.async.adapters.Queue.ClosedQueueException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Matchers.equalTo(org.hamcrest.Matchers.equalTo) SimpleReact(cyclops.futurestream.SimpleReact) IOException(java.io.IOException) Test(org.junit.Test) Before(org.junit.Before) SimpleReact(cyclops.futurestream.SimpleReact) IOException(java.io.IOException) ClosedQueueException(com.oath.cyclops.async.adapters.Queue.ClosedQueueException) Test(org.junit.Test)

Aggregations

SimpleReact (cyclops.futurestream.SimpleReact)71 Test (org.junit.Test)70 Assert.assertThat (org.junit.Assert.assertThat)54 Matchers.is (org.hamcrest.Matchers.is)52 List (java.util.List)50 ExecutionException (java.util.concurrent.ExecutionException)46 Collectors (java.util.stream.Collectors)36 Set (java.util.Set)33 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)31 Matchers.hasItem (org.hamcrest.Matchers.hasItem)30 Matchers.greaterThan (org.hamcrest.Matchers.greaterThan)25 Arrays (java.util.Arrays)24 CompletableFuture (java.util.concurrent.CompletableFuture)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 Stream (java.util.stream.Stream)21 LazyReact (cyclops.futurestream.LazyReact)20 BaseSimpleReactStream (com.oath.cyclops.types.futurestream.BaseSimpleReactStream)17 ArrayList (java.util.ArrayList)16 ForkJoinPool (java.util.concurrent.ForkJoinPool)16 Ignore (org.junit.Ignore)14