Search in sources :

Example 36 with LazyReact

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

the class ParallelTest method runOnCurrent.

@Test
public void runOnCurrent() {
    LazyReact lazy = LazyReact.parallelBuilder().autoOptimizeOn();
    System.out.println("Starting");
    lazy.range(0, 100).map(i -> i + 2).thenSync(i -> {
        try {
            Thread.sleep(500);
        } catch (Exception e) {
        }
        return i;
    }).thenSync(i -> "hello" + i).peekSync(val -> value = val).runOnCurrent();
    assertNotNull(value);
}
Also used : LazyReact(cyclops.futurestream.LazyReact) Assert.assertNotNull(org.junit.Assert.assertNotNull) FutureStream(cyclops.futurestream.FutureStream) MaxActive(com.oath.cyclops.react.collectors.lazy.MaxActive) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Function(java.util.function.Function) LazyReact(cyclops.futurestream.LazyReact) Test(org.junit.Test)

Example 37 with LazyReact

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

the class LazySeqTest method shouldZipTwoInfiniteSequences.

@Test
@Ignore
public void shouldZipTwoInfiniteSequences() throws Exception {
    final FutureStream<Integer> units = new LazyReact(ThreadPools.getCommonFreeThread()).iterate(1, n -> n + 1);
    final FutureStream<Integer> hundreds = new LazyReact(ThreadPools.getCommonFreeThread()).iterate(100, n -> n + 100);
    final ReactiveSeq<String> zipped = units.zip(hundreds, (n, p) -> n + ": " + p);
    assertThat(zipped.limit(5).join(), equalTo(of("1: 100", "2: 200", "3: 300", "4: 400", "5: 500").join()));
}
Also used : LazyReact(cyclops.futurestream.LazyReact) Ignore(org.junit.Ignore) BaseSeqTest(cyclops.futurestream.react.base.BaseSeqTest) Test(org.junit.Test)

Example 38 with LazyReact

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

the class BatchingTest method windowByTimeFiltered.

@Test
public void windowByTimeFiltered() {
    for (int x = 0; x < 10; x++) {
        count2 = new AtomicInteger(0);
        List<Collection<Map>> result = new ArrayList<>();
        new LazyReact(ThreadPools.getCommonFreeThread()).iterate("", last -> "hello").limit(1000).peek(i -> System.out.println(++otherCount)).groupedByTime(1, TimeUnit.MICROSECONDS).peek(batch -> System.out.println("batched : " + batch + ":" + (++peek))).peek(batch -> count3 = count3 + (int) batch.stream().count()).forEach(next -> {
            count2.getAndAdd((int) next.stream().count());
        });
        System.out.println("In flight count " + count3 + " :" + otherCount);
        System.out.println(result.size());
        System.out.println(result);
        System.out.println("x" + x);
        assertThat(count2.get(), equalTo(1000));
    }
}
Also used : ListX(cyclops.reactive.collections.mutable.ListX) Arrays(java.util.Arrays) LazyReact(cyclops.futurestream.LazyReact) Matchers.not(org.hamcrest.Matchers.not) Vector(cyclops.data.Vector) Seq(cyclops.data.Seq) ArrayList(java.util.ArrayList) Value(lombok.Value) Assert.assertThat(org.junit.Assert.assertThat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Before(org.junit.Before) Collection(java.util.Collection) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SimpleTimer(com.oath.cyclops.util.SimpleTimer) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) DuplicationTest(cyclops.futurestream.react.lazy.DuplicationTest) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ThreadPools(com.oath.cyclops.react.ThreadPools) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) TreeSet(cyclops.data.TreeSet) LazyReact(cyclops.futurestream.LazyReact) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Test(org.junit.Test) DuplicationTest(cyclops.futurestream.react.lazy.DuplicationTest)

Example 39 with LazyReact

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

the class AutoMemoizationTest method autoMemoizeSet.

@Test
public void autoMemoizeSet() {
    called.set(0);
    Map cache = new ConcurrentHashMap<>();
    LazyReact react = new LazyReact().autoMemoizeOn((key, fn) -> cache.computeIfAbsent(key, fn));
    Set<Integer> result = react.of(1, 1, 1, 1).capture(e -> e.printStackTrace()).map(i -> calc(i)).peek(System.out::println).toSet();
    System.out.println(result);
    assertThat(called.get(), equalTo(1));
    assertThat(result.size(), equalTo(1));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.assertThat(org.junit.Assert.assertThat) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LazyReact(cyclops.futurestream.LazyReact) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Test(org.junit.Test) LazyReact(cyclops.futurestream.LazyReact) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 40 with LazyReact

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

the class AutoMemoizationTest method autoMemoize.

@Test
public void autoMemoize() {
    called.set(0);
    Map cache = new ConcurrentHashMap<>();
    LazyReact react = new LazyReact().autoMemoizeOn((key, fn) -> cache.computeIfAbsent(key, fn));
    List result = react.of(1, 1, 1, 1).capture(e -> e.printStackTrace()).map(i -> calc(i)).peek(System.out::println).toList();
    System.out.println(result);
    assertThat(called.get(), equalTo(1));
    assertThat(result.size(), equalTo(4));
}
Also used : Assert.assertThat(org.junit.Assert.assertThat) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LazyReact(cyclops.futurestream.LazyReact) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Test(org.junit.Test) LazyReact(cyclops.futurestream.LazyReact) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

LazyReact (cyclops.futurestream.LazyReact)60 Test (org.junit.Test)58 List (java.util.List)27 Assert.assertThat (org.junit.Assert.assertThat)25 Queue (com.oath.cyclops.async.adapters.Queue)21 Matchers.equalTo (org.hamcrest.Matchers.equalTo)21 Ignore (org.junit.Ignore)20 ReactiveSeq (cyclops.reactive.ReactiveSeq)19 Matchers.greaterThan (org.hamcrest.Matchers.greaterThan)19 QueueFactories (com.oath.cyclops.async.QueueFactories)17 Arrays.asList (java.util.Arrays.asList)16 Collectors (java.util.stream.Collectors)16 Matchers.is (org.hamcrest.Matchers.is)16 ArrayList (java.util.ArrayList)15 ForkJoinPool (java.util.concurrent.ForkJoinPool)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 Tuple2 (cyclops.data.tuple.Tuple2)14 FutureStream (cyclops.futurestream.FutureStream)13 ThreadPools (com.oath.cyclops.react.ThreadPools)12 Tuple.tuple (cyclops.data.tuple.Tuple.tuple)12