static <T> ReactiveSeq<T> reactive(Consumer<? super Subscriber<T>> sub){ ReactiveSubscriber<T> reactive = new ReactiveSubscriber<T>(); sub.accept(reactive); return reactive.reactiveStream(); } static <T> ReactiveSeq<T> reactiveStream(Operator<T> s){
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) {
@Test @Ignore public void block(){ ReactiveSubscriber<String> pushable = Spouts.reactiveSubscriber(); ReactiveSeq<String> stream = pushable.reactiveStream(); new Thread(()->{active.set(false); pushable.onComplete();}).run(); stream.forEach(System.out::println); assertFalse(active.get()); } @Test @Ignore
@Test @Ignore public void blockToList(){ ReactiveSubscriber<String> pushable = Spouts.reactiveSubscriber(); ReactiveSeq<String> stream = pushable.reactiveStream(); new Thread(()->{active.set(false); pushable.onComplete();}).run(); stream.toList(); assertFalse(active.get()); }
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
@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()); }
@Test public void flatMapAsyncRS2(){ for(int k=0;k<1000;k++) { System.out.println("********0---------------------K " + k); ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber(); Spouts.of(1, 2, 3).peek(System.out::println) .concatMap(i -> nextAsyncRS()) // .flatMapP(i->Spouts.of(1,2)) .subscribe(sub); List<Integer> res = sub.reactiveStream().collect(Collectors.toList()); System.out.println(res); assertThat(res.size(), equalTo(Arrays.asList(1, 2, 1, 2, 1, 2).size())); assertThat(res, hasItems(1, 2)); int one = 0; int two = 0; for (Integer next : res) { if (next == 1) { one++; } if (next == 2) { two++; } } assertThat(one, equalTo(3)); assertThat(two, equalTo(3)); } } @Test
@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
@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 public void collect(){ ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber(); ReactiveSeq.of(1,2,3).peek(System.out::println).subscribe(sub); System.out.println(sub.reactiveStream().peek(System.out::println).collect(Collectors.toList())); } @Test
@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 public void forEach(){ ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber(); ReactiveSeq.of(1,2,3).subscribe(sub); sub.reactiveStream().forEach(System.out::println); }
@Test public void reactiveStreams(){ ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber(); Flux.just(1,2,3).subscribe(sub); sub.reactiveStream().forEach(System.out::println); }
/** @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 init(){ ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber(); Flux.just(1,2,3).subscribe(sub); sub.reactiveStream().forEach(System.out::println); assertTrue(sub.isInitialized()); } }
@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