Search in sources :

Example 46 with SimpleReact

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

the class ResultCollectionTest method testBreakoutAllCompletedAndTime.

@Test
public void testBreakoutAllCompletedAndTime() throws InterruptedException, ExecutionException {
    count = 0;
    List<Integer> results = new SimpleReact().<Integer>ofAsync(() -> 1, () -> 2, () -> 3).then(it -> it * 100).then(it -> {
        sleep(it);
        return it;
    }).capture(e -> count++).block(status -> status.getAllCompleted() > 1 && status.getElapsedMillis() > 200);
    assertThat(results.size(), greaterThan(1));
    assertThat(count, is(0));
}
Also used : Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) SimpleReact(cyclops.futurestream.SimpleReact) Set(java.util.Set) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SimpleReact(cyclops.futurestream.SimpleReact) Test(org.junit.Test)

Example 47 with SimpleReact

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

the class AlgorithmCompareTest method testFastestLessBlocking.

@Test
public void testFastestLessBlocking() throws InterruptedException, ExecutionException {
    ArrayList<Integer> arrayList = new ArrayList<>();
    LinkedList<Integer> linkedList = new LinkedList<>();
    for (int i = 0; i < 1001; i++) {
        arrayList.add(i);
        linkedList.add(i);
    }
    SimpleTimer timer = new SimpleTimer();
    Result result = new SimpleReact().<Result>ofAsync(() -> Result.builder().name("approach1 : arrayList").result(retrieval(arrayList)).build(), () -> Result.builder().name("approach2 : linkedList").result(retrieval(linkedList)).build()).then(it -> it.withTime(timer.getElapsedNanoseconds())).filter(it -> it.getResult() == 1000).block().firstValue(null);
    assertThat(result.getName(), is("approach1 : arrayList"));
}
Also used : Getter(lombok.Getter) Wither(lombok.experimental.Wither) Test(org.junit.Test) SimpleTimer(com.oath.cyclops.util.SimpleTimer) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Builder(lombok.experimental.Builder) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) LinkedList(java.util.LinkedList) AllArgsConstructor(lombok.AllArgsConstructor) SimpleTimer(com.oath.cyclops.util.SimpleTimer) SimpleReact(cyclops.futurestream.SimpleReact) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 48 with SimpleReact

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

the class QueueTest method backPressureTimeoutTestVeryHigh.

@Test
public void backPressureTimeoutTestVeryHigh() {
    Queue<Integer> q = new Queue<Integer>(new LinkedBlockingQueue<>(2)).withOfferTimeout(1).withOfferTimeUnit(TimeUnit.DAYS);
    BaseSimpleReactStream<Boolean> s = new SimpleReact().ofAsync(() -> offerAndIncrementFound(q), () -> offerAndIncrementFound(q), () -> offerAndIncrementFound(q), () -> offerAndIncrementFound(q));
    sleep(10);
    assertThat(found.get(), is(2));
    assertThat(q.stream().limit(4).collect(Collectors.toList()).size(), is(4));
    Set<Boolean> results = s.block(Collectors.toSet());
    assertThat(found.get(), is(4));
    assertThat(results.size(), is(1));
    // some offers failed.
    assertThat(results, not(hasItem(false)));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SimpleReact(cyclops.futurestream.SimpleReact) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Example 49 with SimpleReact

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

the class QueueTest method queueTestRun.

@Test
public void queueTestRun() {
    try {
        Queue<Integer> q = new Queue<>(new LinkedBlockingQueue<>());
        new SimpleReact().ofAsync(() -> q.offer(1), () -> q.offer(2), () -> {
            sleep(20);
            return q.offer(4);
        }, () -> {
            sleep(400);
            q.close();
            return 1;
        });
        List<String> result = parallel().fromStream(q.stream()).then(it -> "*" + it).peek(it -> found.getAndAdd(1)).peek(it -> System.out.println(it)).block();
        assertThat(result, hasItem("*1"));
    } finally {
        assertThat(found.get(), is(3));
    }
}
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 50 with SimpleReact

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

the class QueueTest method queueTestBlock.

@Test
public void queueTestBlock() {
    try {
        Queue q = new Queue<>(new LinkedBlockingQueue<>());
        new SimpleReact().ofAsync(() -> q.offer(1), () -> q.offer(2), () -> {
            sleep(50);
            return q.offer(4);
        }, () -> {
            sleep(400);
            q.close();
            return 1;
        });
        parallel().fromStream(q.streamCompletableFutures()).then(it -> "*" + it).peek(it -> found.getAndAdd(1)).peek(it -> System.out.println(it)).block();
    } finally {
        assertThat(found.get(), is(3));
    }
}
Also used : 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)

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