Search in sources :

Example 6 with QueueFactories

use of com.oath.cyclops.async.QueueFactories in project cyclops by aol.

the class QueueTest method parallelStreamSmallBounds.

@Test
public void parallelStreamSmallBounds() {
    for (int x = 0; x < 10; x++) {
        System.out.println("Run  " + x);
        success = false;
        AtomicLong threadId = new AtomicLong(Thread.currentThread().getId());
        Queue<Integer> q = QueueFactories.<Integer>boundedQueue(100).build();
        for (int i = 0; i < 10000; i++) {
            q.add(i);
        }
        System.out.println(" queue " + q.size());
        System.out.println(threadId.get());
        q.jdkStream().parallel().peek(System.out::println).peek(i -> {
            System.out.println(Thread.currentThread().getId());
            if (threadId.get() != Thread.currentThread().getId()) {
                System.out.println("closing");
                success = true;
                q.close();
            }
        }).peek(i -> System.out.println(Thread.currentThread().getId())).forEach(System.out::println);
        assertTrue(success);
    }
}
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) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 7 with QueueFactories

use of com.oath.cyclops.async.QueueFactories in project cyclops by aol.

the class BatchingInvestigationsTest method streamBatch.

@Test
public void streamBatch() {
    Queue<String> queue = QueueFactories.<String>unboundedQueue().build();
    new Thread(() -> {
        for (int i = 0; i < 10; i++) {
            queue.offer("New message " + i);
            sleep(10000);
        }
        queue.close();
    }).start();
    long toRun = TimeUnit.MILLISECONDS.toNanos(500l);
    queue.streamBatch(new Subscription(), source -> {
        return () -> {
            List<String> result = new ArrayList<>();
            long start = System.nanoTime();
            while (result.size() < 10 && (System.nanoTime() - start) < toRun) {
                try {
                    String next = source.apply(1l, TimeUnit.MILLISECONDS);
                    if (next != null) {
                        result.add(next);
                    }
                } catch (Queue.QueueTimeoutException e) {
                }
            }
            if (result.size() > 0) {
                System.out.println("Result " + result);
            }
            start = System.nanoTime();
            return result;
        };
    }).filter(l -> l.size() > 0).to(s -> new LazyReact(ThreadPools.getSequential()).fromStream(s)).async().peek(System.out::println).run();
    while (true) {
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Queue(com.oath.cyclops.async.adapters.Queue) QueueFactories(com.oath.cyclops.async.QueueFactories) Ignore(org.junit.Ignore) LazyReact(cyclops.futurestream.LazyReact) ThreadPools(com.oath.cyclops.react.ThreadPools) Test(org.junit.Test) StreamSource(cyclops.stream.StreamSource) Subscription(com.oath.cyclops.react.async.subscription.Subscription) ArrayList(java.util.ArrayList) LazyReact(cyclops.futurestream.LazyReact) ArrayList(java.util.ArrayList) Subscription(com.oath.cyclops.react.async.subscription.Subscription) Queue(com.oath.cyclops.async.adapters.Queue) Test(org.junit.Test)

Example 8 with QueueFactories

use of com.oath.cyclops.async.QueueFactories in project cyclops by aol.

the class BatchingInvestigationsTest method batchIssue.

@Test
public void batchIssue() throws InterruptedException {
    Queue<String> queue = QueueFactories.<String>unboundedQueue().build();
    new Thread(() -> {
        for (int i = 0; i < 10; i++) {
            queue.offer("New message " + i);
            sleep(10000);
        }
    }).start();
    queue.stream().groupedBySizeAndTime(10, 500, TimeUnit.MILLISECONDS).to(s -> new LazyReact(ThreadPools.getSequential()).fromStream(s)).async().peek(System.out::println).run();
    while (true) {
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Queue(com.oath.cyclops.async.adapters.Queue) QueueFactories(com.oath.cyclops.async.QueueFactories) Ignore(org.junit.Ignore) LazyReact(cyclops.futurestream.LazyReact) ThreadPools(com.oath.cyclops.react.ThreadPools) Test(org.junit.Test) StreamSource(cyclops.stream.StreamSource) Subscription(com.oath.cyclops.react.async.subscription.Subscription) ArrayList(java.util.ArrayList) LazyReact(cyclops.futurestream.LazyReact) Test(org.junit.Test)

Example 9 with QueueFactories

use of com.oath.cyclops.async.QueueFactories in project cyclops by aol.

the class PipesTest method futureStreamTest.

@Test
public void futureStreamTest() {
    Pipes<String, Integer> bus = Pipes.of();
    bus.register("reactor", QueueFactories.<Integer>boundedNonBlockingQueue(1000).build());
    bus.publishTo("reactor", ReactiveSeq.of(10, 20, 30));
    bus.close("reactor");
    System.out.println(Thread.currentThread().getId());
    List<String> res = bus.futureStream("reactor", new LazyReact()).toOptional().get().map(i -> "fan-out toNested handle blocking I/O:" + Thread.currentThread().getId() + ":" + i).toList();
    System.out.println(res);
    assertThat(res.size(), equalTo(3));
}
Also used : ListX(cyclops.reactive.collections.mutable.ListX) Arrays(java.util.Arrays) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) NoSuchElementException(java.util.NoSuchElementException) Pipes(cyclops.futurestream.Pipes) Before(org.junit.Before) cyclops.control(cyclops.control) Executor(java.util.concurrent.Executor) lombok.val(lombok.val) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Executors(java.util.concurrent.Executors) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ReactiveSeq(cyclops.reactive.ReactiveSeq) Flux(reactor.core.publisher.Flux) List(java.util.List) Queue(com.oath.cyclops.async.adapters.Queue) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) ForkJoinPool(java.util.concurrent.ForkJoinPool) Matchers.equalTo(org.hamcrest.Matchers.equalTo) QueueBasedSubscriber(com.oath.cyclops.types.reactive.QueueBasedSubscriber) LazyReact(cyclops.futurestream.LazyReact) Test(org.junit.Test)

Aggregations

QueueFactories (com.oath.cyclops.async.QueueFactories)9 LazyReact (cyclops.futurestream.LazyReact)9 Ignore (org.junit.Ignore)9 Test (org.junit.Test)9 List (java.util.List)8 Assert.assertThat (org.junit.Assert.assertThat)7 Queue (com.oath.cyclops.async.adapters.Queue)6 ArrayList (java.util.ArrayList)6 Before (org.junit.Before)6 ReactiveSeq (cyclops.reactive.ReactiveSeq)5 Assert.assertFalse (org.junit.Assert.assertFalse)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 SimpleReact (cyclops.futurestream.SimpleReact)4 Arrays (java.util.Arrays)4 Executors (java.util.concurrent.Executors)4 ForkJoinPool (java.util.concurrent.ForkJoinPool)4 TimeUnit (java.util.concurrent.TimeUnit)4 Matchers.equalTo (org.hamcrest.Matchers.equalTo)4 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)4 BaseSimpleReactStream (com.oath.cyclops.types.futurestream.BaseSimpleReactStream)3