static ReactiveSeq<Integer> interval(String cron,ScheduledExecutorService exec) { ReactiveSubscriber<Integer> sub = reactiveSubscriber(); AtomicBoolean isOpen = new AtomicBoolean(true); Subscription[] s= {null}; sub.onSubscribe(new Subscription() { @Override public void request(long n) { s[0].request(n); } @Override public void cancel() { isOpen.set(false); } }); s[0] = ReactiveSeq.iterate(1, a -> a + 1) .takeWhile(e -> isOpen.get()) .schedule(cron, exec) .connect() .forEach(1, e -> sub.onNext(e)); return sub.reactiveStream(); } static ReactiveSeq<Integer> interval(final long millis,ScheduledExecutorService exec) {
@Test public void forEachWithError(){ ReactiveSubscriber<String> pushable = Spouts.reactiveSubscriber(); ReactiveSeq<String> stream = pushable.reactiveStream(); pushable.onNext("hello"); pushable.onComplete(); stream.map(s->s.length()) .flatMap(s-> IntStream.range(0,s).boxed()) .forEach(System.out::println,System.err::println); pushable.onNext("world"); }
@Test public void testFlatMapOrdering(){ ReactiveSubscriber<String> pushable = Spouts.reactiveSubscriber(); pushable.onComplete(); pushable.onNext("hello"); ReactiveSeq<String> stream = pushable.reactiveStream(); stream.map(s->s.length()) .flatMap(s-> IntStream.range(0,s).boxed()) .forEach(System.out::println); pushable.onNext("world"); }
@Test public void testFlatMap(){ ReactiveSubscriber<String> pushable = Spouts.reactiveSubscriber(); pushable.onNext("hello"); pushable.onComplete(); ReactiveSeq<String> stream = pushable.reactiveStream(); stream.map(s->s.length()) .flatMap(s-> IntStream.range(0,s).boxed()) .forEach(System.out::println); pushable.onNext("world"); } @Test
static ReactiveSeq<Integer> interval(final long millis,ScheduledExecutorService exec) { ReactiveSubscriber<Integer> sub = reactiveSubscriber(); AtomicBoolean isOpen = new AtomicBoolean(true); Subscription[] s= {null}; sub.onSubscribe(new Subscription() { @Override public void request(long n) { s[0].request(n); } @Override public void cancel() { isOpen.set(false); } }); s[0] = ReactiveSeq.iterate(1, a -> a + 1) .takeWhile(e -> isOpen.get()) .scheduleFixedDelay(millis, exec) .connect() .forEach(1, e -> sub.onNext(e)); return sub.reactiveStream(); } static <T> ReactiveSeq<T> schedule(final Stream<T> stream,final String cron,final ScheduledExecutorService exec) {
@Test @Ignore public void zip(){ Stream<Integer> s = Stream.of(1,2,3); Iterator<Integer> it = s.iterator(); int i = it.next(); ReactiveSubscriber<String> pushable = Spouts.reactiveSubscriber(); ReactiveSeq<String> stream = pushable.reactiveStream(); new Thread(()->{ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } pushable.onNext("hello"); pushable.onNext("hello"); active.set(false); pushable.onComplete(); }).start(); assertThat(stream.zipWithStream(Stream.of(1,2)).toList().size(),equalTo(1)); assertFalse(active.get()); }
private ReactiveSeq<Integer> nextAsyncRS() { ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber(); AtomicLong req = new AtomicLong(0); int id = start.incrementAndGet(); sub.onSubscribe(new Subscription() { @Override public void request(long n) { req.addAndGet(n); } @Override public void cancel() { } public String toString(){ return "subscription " + id; } }); new Thread(()->{ int sent=0; while(sent<2){ if(req.get()>0){ sub.onNext( ++sent); req.decrementAndGet(); } } sub.onComplete(); // Flux.just(1,2).forEachAsync(sub); }).start(); return sub.reactiveStream(); } private ReactiveSeq<Integer> nextAsync() {
private ReactiveSeq<Integer> nextAsyncRS() { ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber(); AtomicLong req = new AtomicLong(0); int id = start.incrementAndGet(); sub.onSubscribe(new Subscription() { @Override public void request(long n) { req.addAndGet(n); } @Override public void cancel() { } public String toString(){ return "subscription " + id; } }); new Thread(()->{ int sent=0; while(sent<2){ if(req.get()>0){ sub.onNext( ++sent); req.decrementAndGet(); } } sub.onComplete(); // Flux.just(1,2).forEachAsync(sub); }).start(); return sub.reactiveStream(); } private ReactiveSeq<Integer> nextAsync() {
static <T> ReactiveSeq<T> schedule(final Stream<T> stream,final String cron,final ScheduledExecutorService exec) { ReactiveSubscriber<T> sub = reactiveSubscriber(); AtomicBoolean isOpen = new AtomicBoolean(true); Subscription[] s= {null}; sub.onSubscribe(new Subscription() { @Override public void request(long n) { s[0].request(n); } @Override public void cancel() { isOpen.set(false); } }); s[0] = ReactiveSeq.fromStream(stream) .takeWhile(e -> isOpen.get()) .schedule(cron, exec) .connect() .forEach(0, e -> sub.onNext(e),t->sub.onError(t),()->sub.onComplete()); return sub.reactiveStream(); }
@Test @Ignore public void testIterator(){ ReactiveSubscriber<String> pushable = Spouts.reactiveSubscriber(); ReactiveSeq<String> stream = pushable.reactiveStream(); pushable.onNext("hello"); pushable.onComplete(); Iterator<Integer> it = stream.map(s->s.length()) .flatMap(s-> IntStream.range(0,s).boxed()) .iterator(); List<Integer> result = new ArrayList<>(); while(it.hasNext()){ result.add(it.next()); } assertThat(result,equalTo(Arrays.asList(0,1,2,3,4))); } @Test
private Stream<Integer> nextAsyncRS() { ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber(); AtomicLong req = new AtomicLong(0); int id = start.incrementAndGet(); sub.onSubscribe(new Subscription() { @Override public void request(long n) { req.addAndGet(n); } @Override public void cancel() { } public String toString(){ return "subscription " + id; } }); new Thread(()->{ int sent=0; while(sent<2){ if(req.get()>0){ sub.onNext( ++sent); req.decrementAndGet(); } } sub.onComplete(); // Flux.just(1,2).forEachAsync(sub); }).start(); return sub.reactiveStream(); } private Stream<Integer> nextAsync() {
private Stream<Integer> nextAsyncRS() { ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber(); AtomicLong req = new AtomicLong(0); int id = start.incrementAndGet(); sub.onSubscribe(new Subscription() { @Override public void request(long n) { req.addAndGet(n); } @Override public void cancel() { } public String toString(){ return "subscription " + id; } }); new Thread(()->{ int sent=0; while(sent<2){ if(req.get()>0){ sub.onNext( ++sent); req.decrementAndGet(); } } sub.onComplete(); // Flux.just(1,2).forEachAsync(sub); }).start(); return sub.reactiveStream(); } private Stream<Integer> nextAsync() {
private ReactiveSeq<Integer> nextAsyncRS() { ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber(); AtomicLong req = new AtomicLong(0); int id = start.incrementAndGet(); sub.onSubscribe(new Subscription() { @Override public void request(long n) { req.addAndGet(n); } @Override public void cancel() { } public String toString(){ return "subscription " + id; } }); new Thread(()->{ int sent=0; while(sent<2){ if(req.get()>0){ sub.onNext( ++sent); req.decrementAndGet(); } } sub.onComplete(); // Flux.just(1,2).forEachAsync(sub); }).start(); return sub.reactiveStream(); } private ReactiveSeq<Integer> nextAsync() {
@Test @Ignore public void push(){ ReactiveSubscriber<String> pushable = Spouts.reactiveSubscriber(); ReactiveSeq<String> stream = pushable.reactiveStream(); Executor ex= Executors.newFixedThreadPool(1); Future<List<String>> list = stream.peek(System.err::println) .foldFuture(ex,s->s.collect(Collectors.toList())); pushable.onNext("hello"); pushable.onComplete(); assertThat(list.orElse(new ArrayList<>()).size(),equalTo(1)); }
@Test @Ignore public void lazy(){ ReactiveSubscriber<String> pushable = Spouts.reactiveSubscriber(); ReactiveSeq<String> stream = pushable.reactiveStream(); Eval<List<String>> list = stream.peek(System.err::println) .foldLazy(s->s.collect(Collectors.toList())); pushable.onNext("hello"); pushable.onComplete(); assertThat(list.get().size(),equalTo(1)); } @Test @Ignore
/** @Test @Ignore public void limitPushTest(){ ReactiveSubscriber<String> pushable = ReactiveSeq.pushable(); ReactiveSeq<String> stream = pushable.stream(); ReactiveSeq<List<String>> res = stream.map(i->i+"-hello").limit(2) .collectSeq(CyclopsCollectors.toList()); pushable.onNext("hello1"); pushable.onNext("hello2"); pushable.onNext("hello3"); //LimitSpliterator only supports iteration assertThat(res.single().size(),equalTo(3)); } **/ @Test public void forEachWithErrorPush(){ ReactiveSubscriber<String> pushable = Spouts.reactiveSubscriber(); ReactiveSeq<String> stream = pushable.reactiveStream(); // pushable.onComplete(); stream.map(i->i+"-hello").limit(2) .forEach(System.out::println, s->System.out.println("Error" + s)); pushable.onNext("hello1"); pushable.onError(new RuntimeException()); } @Test @Ignore
@Test public void replay(){ ReactiveSubscriber<String> pushable = Spouts.reactiveSubscriber(); ReactiveSeq<String> stream = pushable.reactiveStream(); ReactiveSeq<String> stream1 = stream.map(str->"hello world " + str); Spliterator<String> sp = stream1.spliterator(); pushable.onNext("hello"); pushable.onComplete(); ReactiveSeq.fromSpliterator(sp).forEach(System.out::println); pushable.onNext("world"); pushable.onComplete(); ReactiveSeq.fromSpliterator(sp).forEach(System.err::println); } @Test
static ReactiveSeq<Integer> interval(String cron,ScheduledExecutorService exec) { ReactiveSubscriber<Integer> sub = reactiveSubscriber(); AtomicBoolean isOpen = new AtomicBoolean(true); Subscription[] s= {null}; sub.onSubscribe(new Subscription() { @Override public void request(long n) { s[0].request(n); } @Override public void cancel() { isOpen.set(false); } }); s[0] = ReactiveSeq.iterate(1, a -> a + 1) .takeWhile(e -> isOpen.get()) .schedule(cron, exec) .connect() .forEach(1, e -> sub.onNext(e)); return sub.reactiveStream(); } static ReactiveSeq<Integer> interval(final long millis,ScheduledExecutorService exec) {
static ReactiveSeq<Integer> interval(final long millis,ScheduledExecutorService exec) { ReactiveSubscriber<Integer> sub = reactiveSubscriber(); AtomicBoolean isOpen = new AtomicBoolean(true); Subscription[] s= {null}; sub.onSubscribe(new Subscription() { @Override public void request(long n) { s[0].request(n); } @Override public void cancel() { isOpen.set(false); } }); s[0] = ReactiveSeq.iterate(1, a -> a + 1) .takeWhile(e -> isOpen.get()) .scheduleFixedDelay(millis, exec) .connect() .forEach(1, e -> sub.onNext(e)); return sub.reactiveStream(); } static <T> ReactiveSeq<T> schedule(final Stream<T> stream,final String cron,final ScheduledExecutorService exec) {
static <T> ReactiveSeq<T> schedule(final Stream<T> stream,final String cron,final ScheduledExecutorService exec) { ReactiveSubscriber<T> sub = reactiveSubscriber(); AtomicBoolean isOpen = new AtomicBoolean(true); Subscription[] s= {null}; sub.onSubscribe(new Subscription() { @Override public void request(long n) { s[0].request(n); } @Override public void cancel() { isOpen.set(false); } }); s[0] = ReactiveSeq.fromStream(stream) .takeWhile(e -> isOpen.get()) .schedule(cron, exec) .connect() .forEach(0, e -> sub.onNext(e),t->sub.onError(t),()->sub.onComplete()); return sub.reactiveStream(); }