@Test public void testBackPressureWhenZippingUnevenStreams2() { Queue fast = LazyReact.parallelBuilder() .withExecutor(new ForkJoinPool(2)) .reactInfinitely(() -> "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)); }
@Test public void zipFastSlow() { Queue q = new Queue(); LazyReact.parallelBuilder() .reactInfinitely(() -> sleep(100)) .then(it -> q.add("100")) .runThread(new Thread()); parallel(1, 2, 3, 4, 5, 6) .zip(q.stream()) .peek(it -> System.out.println(it)) .collect(Collectors.toList()); }
/** * flatten nested SimpleReactStreams * * @param stream Stream to flatten * @return flattened Stream */ static <U, R> SimpleReactStream<R> join(SimpleReactStream<SimpleReactStream<U>> stream) { Queue queue = stream.getQueueFactory().build(); stream.then(it -> it.sync().then(queue::offer)).allOf(it -> queue.close()); return stream.fromStream(queue.stream(stream.getSubscription())); }