use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.
the class LazySeqObjectPoolingTest method shouldZipInfiniteWithFiniteSeq.
@Test
public void shouldZipInfiniteWithFiniteSeq() throws Exception {
ThreadPools.setUseCommon(false);
// <-- MEMORY LEAK!- no auto-closing yet, so writes infinetely to it's async queue
final FutureStream<Integer> units = new LazyReact(ThreadPools.getCommonFreeThread()).iterate(1, n -> n + 1);
final ReactiveSeq<Integer> hundreds = new LazyReact(ThreadPools.getCommonFreeThread()).iterate(100, n -> n + 100).limit(5);
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()));
ThreadPools.setUseCommon(true);
}
use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.
the class LazySeqObjectPoolingTest method zipFastSlow.
@Test
public void zipFastSlow() {
Queue q = new Queue();
LazyReact.parallelBuilder().generate(() -> sleep(100)).then(it -> q.add("100")).runThread(new Thread());
new LazyReact().of(1, 2, 3, 4, 5, 6).zip(q.stream()).peek(it -> System.out.println(it)).collect(Collectors.toList());
}
use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.
the class LazySeqTest method testBackPressureWhenZippingUnevenStreams2.
@Test
public void testBackPressureWhenZippingUnevenStreams2() {
Queue fast = LazyReact.parallelBuilder().withExecutor(new ForkJoinPool(2)).generateAsync(() -> "100").peek(System.out::println).withQueueFactory(QueueFactories.boundedQueue(10)).toQueue();
new Thread(() -> {
LazyReact.parallelBuilder().withExecutor(new ForkJoinPool(2)).range(0, 1000).peek(System.out::println).peek(c -> sleep(10)).zip(fast.stream()).forEach(it -> {
});
}).start();
;
fast.setSizeSignal(Signal.queueBackedSignal());
int max = fast.getSizeSignal().getContinuous().stream().mapToInt(it -> (int) it).limit(50).max().getAsInt();
assertThat(max, lessThan(11));
}
use of com.oath.cyclops.async.adapters.Queue in project cyclops by aol.
the class BaseSequentialSeqTest method shardStreams.
@Test
public void shardStreams() {
for (int index = 0; index < 100; index++) {
ImmutableMap<Integer, Queue<Integer>> shards = cyclops.data.HashMap.of(0, new Queue<Integer>()).put(1, new Queue());
Map<Integer, ? extends FutureStream<Integer>> sharded = of(1, 2, 3, 4, 5, 6).shard(shards.javaMap(), i -> i % 2);
sharded.get(0).forEach(next -> {
System.out.println("next is " + next);
});
}
}
use of com.oath.cyclops.async.adapters.Queue 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) {
}
}
Aggregations