/** Timed offer reports drops if saturated */
 public void testDroppedTimedOffer() {
   SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>(basicExecutor, 4);
   TestSubscriber s1 = new TestSubscriber();
   s1.request = false;
   TestSubscriber s2 = new TestSubscriber();
   s2.request = false;
   p.subscribe(s1);
   p.subscribe(s2);
   s2.awaitSubscribe();
   s1.awaitSubscribe();
   long delay = timeoutMillis();
   for (int i = 1; i <= 4; ++i) assertTrue(p.offer(i, delay, MILLISECONDS, null) >= 0);
   long startTime = System.nanoTime();
   assertTrue(p.offer(5, delay, MILLISECONDS, null) < 0);
   s1.sn.request(64);
   assertTrue(p.offer(6, delay, MILLISECONDS, null) < 0);
   // 2 * delay should elapse but check only 1 * delay to allow timer slop
   assertTrue(millisElapsedSince(startTime) >= delay);
   s2.sn.request(64);
   p.close();
   s2.awaitComplete();
   assertTrue(s2.nexts >= 2);
   s1.awaitComplete();
   assertTrue(s1.nexts >= 2);
 }
 /** Timed offer invokes drop handler if saturated */
 public void testHandledDroppedTimedOffer() {
   AtomicInteger calls = new AtomicInteger();
   SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>(basicExecutor, 4);
   TestSubscriber s1 = new TestSubscriber();
   s1.request = false;
   TestSubscriber s2 = new TestSubscriber();
   s2.request = false;
   p.subscribe(s1);
   p.subscribe(s2);
   s2.awaitSubscribe();
   s1.awaitSubscribe();
   long delay = timeoutMillis();
   for (int i = 1; i <= 4; ++i)
     assertTrue(p.offer(i, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) >= 0);
   long startTime = System.nanoTime();
   assertTrue(p.offer(5, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0);
   s1.sn.request(64);
   assertTrue(p.offer(6, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0);
   assertTrue(millisElapsedSince(startTime) >= delay);
   s2.sn.request(64);
   p.close();
   s2.awaitComplete();
   s1.awaitComplete();
   assertTrue(calls.get() >= 2);
 }
 /** Timed offer with null item or TimeUnit throws NPE */
 public void testNullTimedOffer() {
   SubmissionPublisher<Integer> p = basicPublisher();
   long startTime = System.nanoTime();
   try {
     p.offer(null, LONG_DELAY_MS, MILLISECONDS, null);
     shouldThrow();
   } catch (NullPointerException success) {
   }
   try {
     p.offer(1, LONG_DELAY_MS, null, null);
     shouldThrow();
   } catch (NullPointerException success) {
   }
   assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
 }
 /** Timed offer succeeds if drop handler forces request */
 public void testRecoveredHandledDroppedTimedOffer() {
   AtomicInteger calls = new AtomicInteger();
   SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>(basicExecutor, 4);
   TestSubscriber s1 = new TestSubscriber();
   s1.request = false;
   TestSubscriber s2 = new TestSubscriber();
   s2.request = false;
   p.subscribe(s1);
   p.subscribe(s2);
   s2.awaitSubscribe();
   s1.awaitSubscribe();
   int n = 0;
   long delay = timeoutMillis();
   long startTime = System.nanoTime();
   for (int i = 1; i <= 6; ++i) {
     int d = p.offer(i, delay, MILLISECONDS, (s, x) -> reqHandle(calls, s));
     n = n + 2 + (d < 0 ? d : 0);
   }
   assertTrue(millisElapsedSince(startTime) >= delay);
   p.close();
   s2.awaitComplete();
   s1.awaitComplete();
   assertEquals(n, s1.nexts + s2.nexts);
   assertTrue(calls.get() >= 2);
 }
 /** offer(null) throws NPE */
 public void testNullOffer() {
   SubmissionPublisher<Integer> p = basicPublisher();
   try {
     p.offer(null, null);
     shouldThrow();
   } catch (NullPointerException success) {
   }
 }
 /** offer invokes drop handler if saturated */
 public void testHandledDroppedOffer() {
   AtomicInteger calls = new AtomicInteger();
   SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>(basicExecutor, 4);
   TestSubscriber s1 = new TestSubscriber();
   s1.request = false;
   TestSubscriber s2 = new TestSubscriber();
   s2.request = false;
   p.subscribe(s1);
   p.subscribe(s2);
   s2.awaitSubscribe();
   s1.awaitSubscribe();
   for (int i = 1; i <= 4; ++i) assertTrue(p.offer(i, (s, x) -> noopHandle(calls)) >= 0);
   p.offer(4, (s, x) -> noopHandle(calls));
   assertTrue(p.offer(6, (s, x) -> noopHandle(calls)) < 0);
   s1.sn.request(64);
   assertTrue(p.offer(7, (s, x) -> noopHandle(calls)) < 0);
   s2.sn.request(64);
   p.close();
   s2.awaitComplete();
   s1.awaitComplete();
   assertTrue(calls.get() >= 4);
 }
 /** offer reports drops if saturated */
 public void testDroppedOffer() {
   SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>(basicExecutor, 4);
   TestSubscriber s1 = new TestSubscriber();
   s1.request = false;
   TestSubscriber s2 = new TestSubscriber();
   s2.request = false;
   p.subscribe(s1);
   p.subscribe(s2);
   s2.awaitSubscribe();
   s1.awaitSubscribe();
   for (int i = 1; i <= 4; ++i) assertTrue(p.offer(i, null) >= 0);
   p.offer(5, null);
   assertTrue(p.offer(6, null) < 0);
   s1.sn.request(64);
   assertTrue(p.offer(7, null) < 0);
   s2.sn.request(64);
   p.close();
   s2.awaitComplete();
   assertTrue(s2.nexts >= 4);
   s1.awaitComplete();
   assertTrue(s1.nexts >= 4);
 }
 /** offer returns number of lagged items if not saturated */
 public void testLaggedOffer() {
   SubmissionPublisher<Integer> p = basicPublisher();
   TestSubscriber s1 = new TestSubscriber();
   s1.request = false;
   TestSubscriber s2 = new TestSubscriber();
   s2.request = false;
   p.subscribe(s1);
   p.subscribe(s2);
   s2.awaitSubscribe();
   s1.awaitSubscribe();
   assertTrue(p.offer(1, null) >= 1);
   assertTrue(p.offer(2, null) >= 2);
   s1.sn.request(4);
   assertTrue(p.offer(3, null) >= 3);
   s2.sn.request(4);
   p.offer(4, null);
   p.close();
   s2.awaitComplete();
   assertEquals(4, s2.nexts);
   s1.awaitComplete();
   assertEquals(4, s2.nexts);
 }
 /** Timed offer returns number of lagged items if not saturated */
 public void testLaggedTimedOffer() {
   SubmissionPublisher<Integer> p = basicPublisher();
   TestSubscriber s1 = new TestSubscriber();
   s1.request = false;
   TestSubscriber s2 = new TestSubscriber();
   s2.request = false;
   p.subscribe(s1);
   p.subscribe(s2);
   s2.awaitSubscribe();
   s1.awaitSubscribe();
   long startTime = System.nanoTime();
   assertTrue(p.offer(1, LONG_DELAY_MS, MILLISECONDS, null) >= 1);
   assertTrue(p.offer(2, LONG_DELAY_MS, MILLISECONDS, null) >= 2);
   s1.sn.request(4);
   assertTrue(p.offer(3, LONG_DELAY_MS, MILLISECONDS, null) >= 3);
   s2.sn.request(4);
   p.offer(4, LONG_DELAY_MS, MILLISECONDS, null);
   p.close();
   s2.awaitComplete();
   assertEquals(4, s2.nexts);
   s1.awaitComplete();
   assertEquals(4, s2.nexts);
   assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
 }
 /** offer succeeds if drop handler forces request */
 public void testRecoveredHandledDroppedOffer() {
   AtomicInteger calls = new AtomicInteger();
   SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>(basicExecutor, 4);
   TestSubscriber s1 = new TestSubscriber();
   s1.request = false;
   TestSubscriber s2 = new TestSubscriber();
   s2.request = false;
   p.subscribe(s1);
   p.subscribe(s2);
   s2.awaitSubscribe();
   s1.awaitSubscribe();
   int n = 0;
   for (int i = 1; i <= 8; ++i) {
     int d = p.offer(i, (s, x) -> reqHandle(calls, s));
     n = n + 2 + (d < 0 ? d : 0);
   }
   p.close();
   s2.awaitComplete();
   s1.awaitComplete();
   assertEquals(n, s1.nexts + s2.nexts);
   assertTrue(calls.get() >= 2);
 }
 /** Timed offer to a publisher with no subscribers returns lag 0 */
 public void testEmptyTimedOffer() {
   SubmissionPublisher<Integer> p = basicPublisher();
   long startTime = System.nanoTime();
   assertEquals(0, p.offer(1, LONG_DELAY_MS, MILLISECONDS, null));
   assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
 }
 /** offer to a publisher with no subscribers returns lag 0 */
 public void testEmptyOffer() {
   SubmissionPublisher<Integer> p = basicPublisher();
   assertEquals(0, p.offer(1, null));
 }