Пример #1
0
 /** 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);
 }
Пример #2
0
 /** 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);
 }
Пример #3
0
 /** 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);
 }
Пример #4
0
 /** 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);
 }
Пример #5
0
 /** offer(null) throws NPE */
 public void testNullOffer() {
   SubmissionPublisher<Integer> p = basicPublisher();
   try {
     p.offer(null, null);
     shouldThrow();
   } catch (NullPointerException success) {
   }
 }
Пример #6
0
 /** 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);
 }
Пример #7
0
 /** 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);
 }
Пример #8
0
 /** 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);
 }
Пример #9
0
 /** 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);
 }
Пример #10
0
 /** 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);
 }
Пример #11
0
 /** 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);
 }
Пример #12
0
 /** offer to a publisher with no subscribers returns lag 0 */
 public void testEmptyOffer() {
   SubmissionPublisher<Integer> p = basicPublisher();
   assertEquals(0, p.offer(1, null));
 }