StrictSubscriber extends AtomicInteger implements FlowableSubscriber<T>, Subscription { @Override public void request(long n) { if (n <= 0) { cancel(); onError(new IllegalArgumentException("§3.9 violated: positive request amount required but it was " + n)); } else { SubscriptionHelper.deferredRequest(s, requested, n); } } StrictSubscriber(Subscriber<? super T> actual); @Override void request(long n); @Override void cancel(); @Override void onSubscribe(Subscription s); @Override void onNext(T t); @Override void onError(Throwable t); @Override void onComplete();  }
@Test public void deferredRequest() { final List<Object> list = new ArrayList<Object>(); Subscriber<Object> sub = new Subscriber<Object>() { @Override public void onSubscribe(Subscription s) { s.request(5); list.add(0); } @Override public void onNext(Object t) { list.add(t); } @Override public void onError(Throwable t) { list.add(t); } @Override public void onComplete() { list.add("Done"); } }; Flowable.range(1, 5).subscribe(sub); assertEquals(Arrays.<Object>asList(0, 1, 2, 3, 4, 5, "Done"), list); }
@Test public void requestZero() { final List<Object> list = new ArrayList<Object>(); Subscriber<Object> sub = new Subscriber<Object>() { @Override public void onSubscribe(Subscription s) { s.request(0); } @Override public void onNext(Object t) { list.add(t); } @Override public void onError(Throwable t) { list.add(t); } @Override public void onComplete() { list.add("Done"); } }; Flowable.range(1, 5).subscribe(sub); assertTrue(list.toString(), list.get(0) instanceof IllegalArgumentException); assertTrue(list.toString(), list.get(0).toString().contains("3.9")); }
@Test public void requestNegative() { final List<Object> list = new ArrayList<Object>(); Subscriber<Object> sub = new Subscriber<Object>() { @Override public void onSubscribe(Subscription s) { s.request(-99); } @Override public void onNext(Object t) { list.add(t); } @Override public void onError(Throwable t) { list.add(t); } @Override public void onComplete() { list.add("Done"); } }; Flowable.range(1, 5).subscribe(sub); assertTrue(list.toString(), list.get(0) instanceof IllegalArgumentException); assertTrue(list.toString(), list.get(0).toString().contains("3.9")); }