Exemple #1
0
  @Test
  public void fizzTest() throws Throwable {
    int numOfItems = 1024;
    int batchSize = 8;
    final Timer timer = new Timer();
    AtomicLong globalCounter = new AtomicLong();
    CountDownLatch latch = new CountDownLatch(1);

    Control c =
        Streams.createWith(
                (demand, subscriber) -> {
                  System.out.println("demand is " + demand);
                  if (!subscriber.isCancelled()) {
                    for (int i = 0; i < demand; i++) {
                      long curr = globalCounter.incrementAndGet();
                      if (curr % 5 == 0 && curr % 3 == 0) subscriber.onNext("FizBuz \r\n");
                      else if (curr % 3 == 0) subscriber.onNext("Fiz ");
                      else if (curr % 5 == 0) subscriber.onNext("Buz ");
                      else subscriber.onNext(String.valueOf(curr) + " ");

                      if (globalCounter.get() > numOfItems) {
                        subscriber.onComplete();
                        return;
                      }
                    }
                  }
                })
            .flatMap(
                (s) ->
                    Streams.withOverflowSupport(
                        (sub) ->
                            timer.schedule(
                                new TimerTask() {
                                  @Override
                                  public void run() {
                                    sub.onNext(s);
                                    sub.onComplete();
                                  }
                                },
                                10)))
            .capacity(batchSize)
            .log()
            //                .observe(System.out::print)
            .consume(numOfItems);

    while (c.isPublishing()) ;
  }
 @Override
 public StreamUtils.StreamVisitor debug() {
   return controls.debug();
 }
 @Override
 public void requestMore(long n) {
   controls.requestMore(n);
 }
 @Override
 public boolean isPublishing() {
   return controls.isPublishing();
 }
 @Override
 public void cancel() {
   controls.cancel();
 }
 @Override
 public void start() {
   controls.start();
 }