/**
  * A publisher closedExceptionally reports isClosed with the closedException and throws ISE upon
  * attempted submission; a subsequent close or closeExceptionally has no additional effect.
  */
 public void testCloseExceptionally() {
   SubmissionPublisher<Integer> p = basicPublisher();
   checkInitialState(p);
   Throwable ex = new SPException();
   p.closeExceptionally(ex);
   assertTrue(p.isClosed());
   assertSame(p.getClosedException(), ex);
   try {
     p.submit(1);
     shouldThrow();
   } catch (IllegalStateException success) {
   }
   p.close();
   assertTrue(p.isClosed());
   assertSame(p.getClosedException(), ex);
 }
 /** If closedExceptionally, upon subscription, the subscriber's onError method is invoked */
 public void testSubscribe3() {
   TestSubscriber s = new TestSubscriber();
   SubmissionPublisher<Integer> p = basicPublisher();
   Throwable ex = new SPException();
   p.closeExceptionally(ex);
   assertTrue(p.isClosed());
   assertSame(p.getClosedException(), ex);
   p.subscribe(s);
   s.awaitError();
   assertEquals(0, s.nexts);
   assertEquals(1, s.errors);
 }
 /**
  * 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());
 }
 /** Closing a publisher causes onComplete to subscribers */
 public void testCloseCompletes() {
   SubmissionPublisher<Integer> p = basicPublisher();
   TestSubscriber s1 = new TestSubscriber();
   TestSubscriber s2 = new TestSubscriber();
   p.subscribe(s1);
   p.subscribe(s2);
   p.submit(1);
   p.close();
   assertTrue(p.isClosed());
   assertNull(p.getClosedException());
   s1.awaitComplete();
   assertEquals(1, s1.nexts);
   assertEquals(1, s1.completes);
   s2.awaitComplete();
   assertEquals(1, s2.nexts);
   assertEquals(1, s2.completes);
 }