/**
  * estimateMinimumDemand reports 0 until request, nonzero after request, and zero again after
  * delivery
  */
 public void testEstimateMinimumDemand() {
   TestSubscriber s = new TestSubscriber();
   SubmissionPublisher<Integer> p = basicPublisher();
   s.request = false;
   p.subscribe(s);
   s.awaitSubscribe();
   assertEquals(0, p.estimateMinimumDemand());
   s.sn.request(1);
   assertEquals(1, p.estimateMinimumDemand());
   p.submit(1);
   s.awaitNext(1);
   assertEquals(0, p.estimateMinimumDemand());
 }
 /**
  * A new SubmissionPublisher has no subscribers, a non-null executor, a power-of-two capacity, is
  * not closed, and reports zero demand and lag
  */
 void checkInitialState(SubmissionPublisher<?> p) {
   assertFalse(p.hasSubscribers());
   assertEquals(0, p.getNumberOfSubscribers());
   assertTrue(p.getSubscribers().isEmpty());
   assertFalse(p.isClosed());
   assertNull(p.getClosedException());
   int n = p.getMaxBufferCapacity();
   assertTrue((n & (n - 1)) == 0); // power of two
   assertNotNull(p.getExecutor());
   assertEquals(0, p.estimateMinimumDemand());
   assertEquals(0, p.estimateMaximumLag());
 }
 /** submit eventually issues requested items when buffer capacity is 1 */
 public void testCap1Submit() {
   SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>(basicExecutor, 1);
   TestSubscriber s1 = new TestSubscriber();
   TestSubscriber s2 = new TestSubscriber();
   p.subscribe(s1);
   p.subscribe(s2);
   for (int i = 1; i <= 20; ++i) {
     assertTrue(p.estimateMinimumDemand() <= 1);
     assertTrue(p.submit(i) >= 0);
   }
   p.close();
   s2.awaitComplete();
   s1.awaitComplete();
   assertEquals(20, s2.nexts);
   assertEquals(1, s2.completes);
   assertEquals(20, s1.nexts);
   assertEquals(1, s1.completes);
 }
 /** onNext is issued only if requested */
 public void testRequest1() {
   SubmissionPublisher<Integer> p = basicPublisher();
   TestSubscriber s1 = new TestSubscriber();
   s1.request = false;
   p.subscribe(s1);
   s1.awaitSubscribe();
   assertTrue(p.estimateMinimumDemand() == 0);
   TestSubscriber s2 = new TestSubscriber();
   p.subscribe(s2);
   p.submit(1);
   p.submit(2);
   s2.awaitNext(1);
   assertEquals(0, s1.nexts);
   s1.sn.request(3);
   p.submit(3);
   p.close();
   s2.awaitComplete();
   assertEquals(3, s2.nexts);
   assertEquals(1, s2.completes);
   s1.awaitComplete();
   assertTrue(s1.nexts > 0);
   assertEquals(1, s1.completes);
 }