BackpressureHelper { public static long producedCancel(AtomicLong requested, long n) { for (;;) { long current = requested.get(); if (current == Long.MIN_VALUE) { return Long.MIN_VALUE; } if (current == Long.MAX_VALUE) { return Long.MAX_VALUE; } long update = current - n; if (update < 0L) { RxJavaPlugins.onError(new IllegalStateException("More produced than requested: " + update)); update = 0L; } if (requested.compareAndSet(current, update)) { return update; } } } private  BackpressureHelper(); static long addCap(long a, long b); static long multiplyCap(long a, long b); static long add(AtomicLong requested, long n); static long addCancel(AtomicLong requested, long n); static long produced(AtomicLong requested, long n); static long producedCancel(AtomicLong requested, long n);  }
@Test public void producedMoreCancel() { List<Throwable> list = TestHelper.trackPluginErrors(); try { AtomicLong requested = new AtomicLong(1); assertEquals(0, BackpressureHelper.producedCancel(requested, 2)); TestHelper.assertError(list, 0, IllegalStateException.class, "More produced than requested: -1"); } finally { RxJavaPlugins.reset(); } }