/** Should request n for whatever the final Subscriber asks for */
  @Test
  public void testRequestFromFinalSubscribeWithRequestValue() {
    TestSubscriber<String> s = new TestSubscriber<>((Long) null);
    s.request(10);
    final AtomicLong r = new AtomicLong();
    s.onSubscribe(
        new Subscription() {

          @Override
          public void request(long n) {
            r.set(n);
          }

          @Override
          public void cancel() {}
        });
    assertEquals(10, r.get());
  }
  @Test
  public void testRequestFromChainedOperator() {
    TestSubscriber<String> s = new TestSubscriber<>();
    Operator<String, String> o =
        s1 ->
            new Subscriber<String>() {

              @Override
              public void onSubscribe(Subscription a) {
                s1.onSubscribe(a);
              }

              @Override
              public void onComplete() {}

              @Override
              public void onError(Throwable e) {}

              @Override
              public void onNext(String t) {}
            };
    s.request(10);
    Subscriber<? super String> ns = o.apply(s);

    final AtomicLong r = new AtomicLong();
    // set set the producer at the top of the chain (ns) and it should flow through the operator to
    // the (s) subscriber
    // and then it should request up with the value set on the final Subscriber (s)
    ns.onSubscribe(
        new Subscription() {

          @Override
          public void request(long n) {
            r.set(n);
          }

          @Override
          public void cancel() {}
        });
    assertEquals(10, r.get());
  }
  @Test
  public void testRequestToObservable() {
    TestSubscriber<Integer> ts = new TestSubscriber<>();
    ts.request(3);
    final AtomicLong requested = new AtomicLong();
    Observable.<Integer>create(
            s ->
                s.onSubscribe(
                    new Subscription() {

                      @Override
                      public void request(long n) {
                        requested.set(n);
                      }

                      @Override
                      public void cancel() {}
                    }))
        .subscribe(ts);
    assertEquals(3, requested.get());
  }
  @Test
  public void testRequestThroughTakeWhereRequestIsSmallerThanTake() {
    TestSubscriber<Integer> ts = new TestSubscriber<>((Long) null);
    ts.request(3);
    final AtomicLong requested = new AtomicLong();
    Observable.<Integer>create(
            s ->
                s.onSubscribe(
                    new Subscription() {

                      @Override
                      public void request(long n) {
                        requested.set(n);
                      }

                      @Override
                      public void cancel() {}
                    }))
        .take(10)
        .subscribe(ts);
    assertEquals(3, requested.get());
  }
  @Test
  public void testRequestThroughTakeThatReducesRequest() {
    TestSubscriber<Integer> ts = new TestSubscriber<>((Long) null);
    ts.request(3);
    final AtomicLong requested = new AtomicLong();
    Observable.<Integer>create(
            s ->
                s.onSubscribe(
                    new Subscription() {

                      @Override
                      public void request(long n) {
                        requested.set(n);
                      }

                      @Override
                      public void cancel() {}
                    }))
        .take(2)
        .subscribe(ts);

    // FIXME the take now requests Long.MAX_PATH if downstream requests at least the limit
    assertEquals(Long.MAX_VALUE, requested.get());
  }
  @Test
  public void testRequestFromDecoupledOperatorThatRequestsN() {
    TestSubscriber<String> s = new TestSubscriber<>();
    final AtomicLong innerR = new AtomicLong();
    Operator<String, String> o =
        child -> {
          // we want to decouple the chain so set our own Producer on the child instead of it coming
          // from the parent
          child.onSubscribe(
              new Subscription() {

                @Override
                public void request(long n) {
                  innerR.set(n);
                }

                @Override
                public void cancel() {}
              });

          AsyncObserver<String> as =
              new AsyncObserver<String>() {

                @Override
                protected void onStart() {
                  // we request 99 up to the parent
                  request(99);
                }

                @Override
                public void onComplete() {}

                @Override
                public void onError(Throwable e) {}

                @Override
                public void onNext(String t) {}
              };
          return as;
        };
    s.request(10);
    Subscriber<? super String> ns = o.apply(s);

    final AtomicLong r = new AtomicLong();
    // set set the producer at the top of the chain (ns) and it should flow through the operator to
    // the (s) subscriber
    // and then it should request up with the value set on the final Subscriber (s)
    ns.onSubscribe(
        new Subscription() {

          @Override
          public void request(long n) {
            r.set(n);
          }

          @Override
          public void cancel() {}
        });
    assertEquals(99, r.get());
    assertEquals(10, innerR.get());
  }