コード例 #1
0
  @Test
  public void testNoBackpressureWithInitialValue() {
    final AtomicInteger count = new AtomicInteger();
    Observable.range(1, 100)
        .scan(
            0,
            new BiFunction<Integer, Integer, Integer>() {

              @Override
              public Integer apply(Integer t1, Integer t2) {
                return t1 + t2;
              }
            })
        .subscribe(
            new Observer<Integer>() {

              @Override
              public void onComplete() {}

              @Override
              public void onError(Throwable e) {
                fail(e.getMessage());
                e.printStackTrace();
              }

              @Override
              public void onNext(Integer t) {
                count.incrementAndGet();
              }
            });

    // we only expect to receive 101 as we'll receive all 100 + the initial value
    assertEquals(101, count.get());
  }
コード例 #2
0
  @Test
  public void testOnStartCalledOnceViaUnsafeSubscribe() {
    final AtomicInteger c = new AtomicInteger();
    Observable.just(1, 2, 3, 4)
        .take(2)
        .unsafeSubscribe(
            new Observer<Integer>() {

              @Override
              public void onStart() {
                c.incrementAndGet();
                request(1);
              }

              @Override
              public void onComplete() {}

              @Override
              public void onError(Throwable e) {}

              @Override
              public void onNext(Integer t) {
                request(1);
              }
            });

    assertEquals(1, c.get());
  }
コード例 #3
0
  @Test
  public void testOnStartCalledOnceViaLift() {
    final AtomicInteger c = new AtomicInteger();
    Observable.just(1, 2, 3, 4)
        .lift(
            new Operator<Integer, Integer>() {

              @Override
              public Subscriber<? super Integer> apply(final Subscriber<? super Integer> child) {
                return new Observer<Integer>() {

                  @Override
                  public void onStart() {
                    c.incrementAndGet();
                    request(1);
                  }

                  @Override
                  public void onComplete() {
                    child.onComplete();
                  }

                  @Override
                  public void onError(Throwable e) {
                    child.onError(e);
                  }

                  @Override
                  public void onNext(Integer t) {
                    child.onNext(t);
                    request(1);
                  }
                };
              }
            })
        .subscribe();

    assertEquals(1, c.get());
  }