use of cyclops.reactive.ReactiveSeq in project cyclops by aol.
the class TopicTest method disconnectAllStreams.
@Test
public void disconnectAllStreams() {
Topic<Integer> topic = new Topic<>();
ReactiveSeq s1 = topic.stream();
// 3 Queues
ReactiveSeq s2 = topic.stream();
topic.disconnect(s1);
topic.disconnect(s2);
assertThat(topic.getDistributor().getSubscribers().size(), is(0));
assertThat(topic.getStreamToQueue().size(), is(0));
}
use of cyclops.reactive.ReactiveSeq in project cyclops by aol.
the class LazySeqAgronaTest 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 cyclops.reactive.ReactiveSeq in project cyclops by aol.
the class SimpleReactStream method duplicate.
/**
* Duplicate a Stream into two equivalent LazyFutureStreams, underlying
* Stream of Futures is duplicated
* <pre>
* {@code
* EagerFutureStream.of(1, 2, 3).duplicate()
*
* results in
*
* tuple((1,2,3),(1,2,3))
* }</pre>
* Care should be taken not to use this method with infinite streams!
*
* @return Two equivalent Streams
*
* @see #duplicate()
*/
default Tuple2<SimpleReactStream<U>, SimpleReactStream<U>> duplicate() {
// unblocking impl
final Stream stream = getLastActive().stream();
final Tuple2<ReactiveSeq<CompletableFuture<U>>, ReactiveSeq<CompletableFuture<U>>> duplicated = ReactiveSeq.fromStream((Stream<CompletableFuture<U>>) stream).duplicate();
final Tuple2 dup = Tuple.tuple(fromStreamOfFutures(duplicated._1()), fromStreamOfFutures(duplicated._2()));
return dup;
}
use of cyclops.reactive.ReactiveSeq in project cyclops by aol.
the class BaseSeqTest method testPartition100.
@Test
public void testPartition100() {
for (int i = 0; i < 0; i++) {
Supplier<ReactiveSeq<Tuple2<Integer, Integer>>> s = () -> of(tuple(1, 0), tuple(2, 1), tuple(3, 2), tuple(4, 3), tuple(5, 4), tuple(6, 5));
Tuple2<ReactiveSeq<Tuple2<Integer, Integer>>, ReactiveSeq<Tuple2<Integer, Integer>>> tuple = s.get().partition(t -> t._2() < 3);
Tuple2<ReactiveSeq<Integer>, ReactiveSeq<Integer>> t2 = tuple.transform((v1, v2) -> Tuple.<ReactiveSeq<Integer>, ReactiveSeq<Integer>>tuple(v1.map(t -> t._1()), v2.map(t -> t._1())));
List l1 = t2._1().toList();
System.out.println("l1:" + l1);
List l2 = t2._2().toList();
System.out.println("l2:" + l2);
assertThat(l1.size(), is(3));
assertThat(l2.size(), is(3));
}
}
use of cyclops.reactive.ReactiveSeq in project cyclops by aol.
the class ReactiveStreamX method multicast.
public Seq<ReactiveSeq<T>> multicast(int num) {
if (this.async == Type.NO_BACKPRESSURE) {
ConcurrentLinkedQueue<Subscriber> subs = new ConcurrentLinkedQueue<>();
Seq<ReactiveSeq<T>> result = Seq.empty();
for (int i = 0; i < num; i++) {
ReactiveSeq<T> seq = Spouts.<T>async(s1 -> {
subs.add(s1.asSubscriber());
if (subs.size() == num) {
this.forEach(e -> subs.forEach(s -> s.onNext(e)), ex -> subs.forEach(s -> s.onError(ex)), () -> subs.forEach(s -> s.onComplete()));
}
});
result = result.plus(seq);
}
return result;
}
if (this.async == BACKPRESSURE) {
ConcurrentLinkedQueue<Subscriber> subs = new ConcurrentLinkedQueue<>();
Seq<ReactiveSeq<T>> result = Seq.empty();
Subscription sub = forEachSubscribe(e -> subs.forEach(s -> s.onNext(e)), ex -> subs.forEach(s -> s.onError(ex)), () -> subs.forEach(s -> s.onComplete()));
for (int i = 0; i < num; i++) {
ReactiveSeq<T> seq = new ReactiveStreamX<T>(new PublisherToOperator<T>(new Publisher<T>() {
@Override
public void subscribe(Subscriber<? super T> s) {
subs.add(s);
s.onSubscribe(sub);
}
}));
result = result.plus(seq);
}
return result;
}
return Streams.toBufferingCopier(() -> iterator(), num, () -> new ArrayDeque<T>(100)).map(ReactiveSeq::fromIterable);
}
Aggregations