ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SubmissionPublisherTest.java
Revision: 1.23
Committed: Sun Nov 26 21:37:56 2017 UTC (6 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.22: +2 -2 lines
Log Message:
Avoid capacity assumptions

File Contents

# Content
1 /*
2 * Written by Doug Lea and Martin Buchholz with assistance from
3 * members of JCP JSR-166 Expert Group and released to the public
4 * domain, as explained at
5 * http://creativecommons.org/publicdomain/zero/1.0/
6 */
7
8 import java.util.concurrent.CompletableFuture;
9 import java.util.concurrent.Executor;
10 import java.util.concurrent.Executors;
11 import java.util.concurrent.Flow;
12 import java.util.concurrent.ForkJoinPool;
13 import java.util.concurrent.SubmissionPublisher;
14 import java.util.concurrent.atomic.AtomicInteger;
15 import junit.framework.Test;
16 import junit.framework.TestSuite;
17
18 import static java.util.concurrent.Flow.Subscriber;
19 import static java.util.concurrent.Flow.Subscription;
20 import static java.util.concurrent.TimeUnit.MILLISECONDS;
21
22 public class SubmissionPublisherTest extends JSR166TestCase {
23
24 public static void main(String[] args) {
25 main(suite(), args);
26 }
27 public static Test suite() {
28 return new TestSuite(SubmissionPublisherTest.class);
29 }
30
31 final Executor basicExecutor = basicPublisher().getExecutor();
32
33 static SubmissionPublisher<Integer> basicPublisher() {
34 return new SubmissionPublisher<Integer>();
35 }
36
37 static class SPException extends RuntimeException {}
38
39 class TestSubscriber implements Subscriber<Integer> {
40 volatile Subscription sn;
41 int last; // Requires that onNexts are in numeric order
42 volatile int nexts;
43 volatile int errors;
44 volatile int completes;
45 volatile boolean throwOnCall = false;
46 volatile boolean request = true;
47 volatile Throwable lastError;
48
49 public synchronized void onSubscribe(Subscription s) {
50 threadAssertTrue(sn == null);
51 sn = s;
52 notifyAll();
53 if (throwOnCall)
54 throw new SPException();
55 if (request)
56 sn.request(1L);
57 }
58 public synchronized void onNext(Integer t) {
59 ++nexts;
60 notifyAll();
61 int current = t.intValue();
62 threadAssertTrue(current >= last);
63 last = current;
64 if (request)
65 sn.request(1L);
66 if (throwOnCall)
67 throw new SPException();
68 }
69 public synchronized void onError(Throwable t) {
70 threadAssertTrue(completes == 0);
71 threadAssertTrue(errors == 0);
72 lastError = t;
73 ++errors;
74 notifyAll();
75 }
76 public synchronized void onComplete() {
77 threadAssertTrue(completes == 0);
78 ++completes;
79 notifyAll();
80 }
81
82 synchronized void awaitSubscribe() {
83 while (sn == null) {
84 try {
85 wait();
86 } catch (Exception ex) {
87 threadUnexpectedException(ex);
88 break;
89 }
90 }
91 }
92 synchronized void awaitNext(int n) {
93 while (nexts < n) {
94 try {
95 wait();
96 } catch (Exception ex) {
97 threadUnexpectedException(ex);
98 break;
99 }
100 }
101 }
102 synchronized void awaitComplete() {
103 while (completes == 0 && errors == 0) {
104 try {
105 wait();
106 } catch (Exception ex) {
107 threadUnexpectedException(ex);
108 break;
109 }
110 }
111 }
112 synchronized void awaitError() {
113 while (errors == 0) {
114 try {
115 wait();
116 } catch (Exception ex) {
117 threadUnexpectedException(ex);
118 break;
119 }
120 }
121 }
122
123 }
124
125 /**
126 * A new SubmissionPublisher has no subscribers, a non-null
127 * executor, a power-of-two capacity, is not closed, and reports
128 * zero demand and lag
129 */
130 void checkInitialState(SubmissionPublisher<?> p) {
131 assertFalse(p.hasSubscribers());
132 assertEquals(0, p.getNumberOfSubscribers());
133 assertTrue(p.getSubscribers().isEmpty());
134 assertFalse(p.isClosed());
135 assertNull(p.getClosedException());
136 int n = p.getMaxBufferCapacity();
137 assertTrue((n & (n - 1)) == 0); // power of two
138 assertNotNull(p.getExecutor());
139 assertEquals(0, p.estimateMinimumDemand());
140 assertEquals(0, p.estimateMaximumLag());
141 }
142
143 /**
144 * A default-constructed SubmissionPublisher has no subscribers,
145 * is not closed, has default buffer size, and uses the
146 * defaultExecutor
147 */
148 public void testConstructor1() {
149 SubmissionPublisher<Integer> p = new SubmissionPublisher<>();
150 checkInitialState(p);
151 assertEquals(p.getMaxBufferCapacity(), Flow.defaultBufferSize());
152 Executor e = p.getExecutor(), c = ForkJoinPool.commonPool();
153 if (ForkJoinPool.getCommonPoolParallelism() > 1)
154 assertSame(e, c);
155 else
156 assertNotSame(e, c);
157 }
158
159 /**
160 * A new SubmissionPublisher has no subscribers, is not closed,
161 * has the given buffer size, and uses the given executor
162 */
163 public void testConstructor2() {
164 Executor e = Executors.newFixedThreadPool(1);
165 SubmissionPublisher<Integer> p = new SubmissionPublisher<>(e, 8);
166 checkInitialState(p);
167 assertSame(p.getExecutor(), e);
168 assertEquals(8, p.getMaxBufferCapacity());
169 }
170
171 /**
172 * A null Executor argument to SubmissionPublisher constructor
173 * throws NullPointerException
174 */
175 public void testConstructor3() {
176 try {
177 new SubmissionPublisher<Integer>(null, 8);
178 shouldThrow();
179 } catch (NullPointerException success) {}
180 }
181
182 /**
183 * A negative capacity argument to SubmissionPublisher constructor
184 * throws IllegalArgumentException
185 */
186 public void testConstructor4() {
187 Executor e = Executors.newFixedThreadPool(1);
188 try {
189 new SubmissionPublisher<Integer>(e, -1);
190 shouldThrow();
191 } catch (IllegalArgumentException success) {}
192 }
193
194 /**
195 * A closed publisher reports isClosed with no closedException and
196 * throws IllegalStateException upon attempted submission; a
197 * subsequent close or closeExceptionally has no additional
198 * effect.
199 */
200 public void testClose() {
201 SubmissionPublisher<Integer> p = basicPublisher();
202 checkInitialState(p);
203 p.close();
204 assertTrue(p.isClosed());
205 assertNull(p.getClosedException());
206 try {
207 p.submit(1);
208 shouldThrow();
209 } catch (IllegalStateException success) {}
210 Throwable ex = new SPException();
211 p.closeExceptionally(ex);
212 assertTrue(p.isClosed());
213 assertNull(p.getClosedException());
214 }
215
216 /**
217 * A publisher closedExceptionally reports isClosed with the
218 * closedException and throws IllegalStateException upon attempted
219 * submission; a subsequent close or closeExceptionally has no
220 * additional effect.
221 */
222 public void testCloseExceptionally() {
223 SubmissionPublisher<Integer> p = basicPublisher();
224 checkInitialState(p);
225 Throwable ex = new SPException();
226 p.closeExceptionally(ex);
227 assertTrue(p.isClosed());
228 assertSame(p.getClosedException(), ex);
229 try {
230 p.submit(1);
231 shouldThrow();
232 } catch (IllegalStateException success) {}
233 p.close();
234 assertTrue(p.isClosed());
235 assertSame(p.getClosedException(), ex);
236 }
237
238 /**
239 * Upon subscription, the subscriber's onSubscribe is called, no
240 * other Subscriber methods are invoked, the publisher
241 * hasSubscribers, isSubscribed is true, and existing
242 * subscriptions are unaffected.
243 */
244 public void testSubscribe1() {
245 TestSubscriber s = new TestSubscriber();
246 SubmissionPublisher<Integer> p = basicPublisher();
247 p.subscribe(s);
248 assertTrue(p.hasSubscribers());
249 assertEquals(1, p.getNumberOfSubscribers());
250 assertTrue(p.getSubscribers().contains(s));
251 assertTrue(p.isSubscribed(s));
252 s.awaitSubscribe();
253 assertNotNull(s.sn);
254 assertEquals(0, s.nexts);
255 assertEquals(0, s.errors);
256 assertEquals(0, s.completes);
257 TestSubscriber s2 = new TestSubscriber();
258 p.subscribe(s2);
259 assertTrue(p.hasSubscribers());
260 assertEquals(2, p.getNumberOfSubscribers());
261 assertTrue(p.getSubscribers().contains(s));
262 assertTrue(p.getSubscribers().contains(s2));
263 assertTrue(p.isSubscribed(s));
264 assertTrue(p.isSubscribed(s2));
265 s2.awaitSubscribe();
266 assertNotNull(s2.sn);
267 assertEquals(0, s2.nexts);
268 assertEquals(0, s2.errors);
269 assertEquals(0, s2.completes);
270 p.close();
271 }
272
273 /**
274 * If closed, upon subscription, the subscriber's onComplete
275 * method is invoked
276 */
277 public void testSubscribe2() {
278 TestSubscriber s = new TestSubscriber();
279 SubmissionPublisher<Integer> p = basicPublisher();
280 p.close();
281 p.subscribe(s);
282 s.awaitComplete();
283 assertEquals(0, s.nexts);
284 assertEquals(0, s.errors);
285 assertEquals(1, s.completes, 1);
286 }
287
288 /**
289 * If closedExceptionally, upon subscription, the subscriber's
290 * onError method is invoked
291 */
292 public void testSubscribe3() {
293 TestSubscriber s = new TestSubscriber();
294 SubmissionPublisher<Integer> p = basicPublisher();
295 Throwable ex = new SPException();
296 p.closeExceptionally(ex);
297 assertTrue(p.isClosed());
298 assertSame(p.getClosedException(), ex);
299 p.subscribe(s);
300 s.awaitError();
301 assertEquals(0, s.nexts);
302 assertEquals(1, s.errors);
303 }
304
305 /**
306 * Upon attempted resubscription, the subscriber's onError is
307 * called and the subscription is cancelled.
308 */
309 public void testSubscribe4() {
310 TestSubscriber s = new TestSubscriber();
311 SubmissionPublisher<Integer> p = basicPublisher();
312 p.subscribe(s);
313 assertTrue(p.hasSubscribers());
314 assertEquals(1, p.getNumberOfSubscribers());
315 assertTrue(p.getSubscribers().contains(s));
316 assertTrue(p.isSubscribed(s));
317 s.awaitSubscribe();
318 assertNotNull(s.sn);
319 assertEquals(0, s.nexts);
320 assertEquals(0, s.errors);
321 assertEquals(0, s.completes);
322 p.subscribe(s);
323 s.awaitError();
324 assertEquals(0, s.nexts);
325 assertEquals(1, s.errors);
326 assertFalse(p.isSubscribed(s));
327 }
328
329 /**
330 * An exception thrown in onSubscribe causes onError
331 */
332 public void testSubscribe5() {
333 TestSubscriber s = new TestSubscriber();
334 SubmissionPublisher<Integer> p = basicPublisher();
335 s.throwOnCall = true;
336 try {
337 p.subscribe(s);
338 } catch (Exception ok) {}
339 s.awaitError();
340 assertEquals(0, s.nexts);
341 assertEquals(1, s.errors);
342 assertEquals(0, s.completes);
343 }
344
345 /**
346 * subscribe(null) throws NPE
347 */
348 public void testSubscribe6() {
349 SubmissionPublisher<Integer> p = basicPublisher();
350 try {
351 p.subscribe(null);
352 shouldThrow();
353 } catch (NullPointerException success) {}
354 checkInitialState(p);
355 }
356
357 /**
358 * Closing a publisher causes onComplete to subscribers
359 */
360 public void testCloseCompletes() {
361 SubmissionPublisher<Integer> p = basicPublisher();
362 TestSubscriber s1 = new TestSubscriber();
363 TestSubscriber s2 = new TestSubscriber();
364 p.subscribe(s1);
365 p.subscribe(s2);
366 p.submit(1);
367 p.close();
368 assertTrue(p.isClosed());
369 assertNull(p.getClosedException());
370 s1.awaitComplete();
371 assertEquals(1, s1.nexts);
372 assertEquals(1, s1.completes);
373 s2.awaitComplete();
374 assertEquals(1, s2.nexts);
375 assertEquals(1, s2.completes);
376 }
377
378 /**
379 * Closing a publisher exceptionally causes onError to subscribers
380 * after they are subscribed
381 */
382 public void testCloseExceptionallyError() {
383 SubmissionPublisher<Integer> p = basicPublisher();
384 TestSubscriber s1 = new TestSubscriber();
385 TestSubscriber s2 = new TestSubscriber();
386 p.subscribe(s1);
387 p.subscribe(s2);
388 p.submit(1);
389 p.closeExceptionally(new SPException());
390 assertTrue(p.isClosed());
391 s1.awaitSubscribe();
392 s1.awaitError();
393 assertTrue(s1.nexts <= 1);
394 assertEquals(1, s1.errors);
395 s2.awaitSubscribe();
396 s2.awaitError();
397 assertTrue(s2.nexts <= 1);
398 assertEquals(1, s2.errors);
399 }
400
401 /**
402 * Cancelling a subscription eventually causes no more onNexts to be issued
403 */
404 public void testCancel() {
405 SubmissionPublisher<Integer> p =
406 new SubmissionPublisher<Integer>(basicExecutor, 4); // must be < 20
407 TestSubscriber s1 = new TestSubscriber();
408 TestSubscriber s2 = new TestSubscriber();
409 p.subscribe(s1);
410 p.subscribe(s2);
411 s1.awaitSubscribe();
412 p.submit(1);
413 s1.sn.cancel();
414 for (int i = 2; i <= 20; ++i)
415 p.submit(i);
416 p.close();
417 s2.awaitComplete();
418 assertEquals(20, s2.nexts);
419 assertEquals(1, s2.completes);
420 assertTrue(s1.nexts < 20);
421 assertFalse(p.isSubscribed(s1));
422 }
423
424 /**
425 * Throwing an exception in onNext causes onError
426 */
427 public void testThrowOnNext() {
428 SubmissionPublisher<Integer> p = basicPublisher();
429 TestSubscriber s1 = new TestSubscriber();
430 TestSubscriber s2 = new TestSubscriber();
431 p.subscribe(s1);
432 p.subscribe(s2);
433 s1.awaitSubscribe();
434 p.submit(1);
435 s1.throwOnCall = true;
436 p.submit(2);
437 p.close();
438 s2.awaitComplete();
439 assertEquals(2, s2.nexts);
440 s1.awaitComplete();
441 assertEquals(1, s1.errors);
442 }
443
444 /**
445 * If a handler is supplied in constructor, it is invoked when
446 * subscriber throws an exception in onNext
447 */
448 public void testThrowOnNextHandler() {
449 AtomicInteger calls = new AtomicInteger();
450 SubmissionPublisher<Integer> p = new SubmissionPublisher<>(
451 basicExecutor, 8, (s, e) -> calls.getAndIncrement());
452 TestSubscriber s1 = new TestSubscriber();
453 TestSubscriber s2 = new TestSubscriber();
454 p.subscribe(s1);
455 p.subscribe(s2);
456 s1.awaitSubscribe();
457 p.submit(1);
458 s1.throwOnCall = true;
459 p.submit(2);
460 p.close();
461 s2.awaitComplete();
462 assertEquals(2, s2.nexts);
463 assertEquals(1, s2.completes);
464 s1.awaitError();
465 assertEquals(1, s1.errors);
466 assertEquals(1, calls.get());
467 }
468
469 /**
470 * onNext items are issued in the same order to each subscriber
471 */
472 public void testOrder() {
473 SubmissionPublisher<Integer> p = basicPublisher();
474 TestSubscriber s1 = new TestSubscriber();
475 TestSubscriber s2 = new TestSubscriber();
476 p.subscribe(s1);
477 p.subscribe(s2);
478 for (int i = 1; i <= 20; ++i)
479 p.submit(i);
480 p.close();
481 s2.awaitComplete();
482 s1.awaitComplete();
483 assertEquals(20, s2.nexts);
484 assertEquals(1, s2.completes);
485 assertEquals(20, s1.nexts);
486 assertEquals(1, s1.completes);
487 }
488
489 /**
490 * onNext is issued only if requested
491 */
492 public void testRequest1() {
493 SubmissionPublisher<Integer> p = basicPublisher();
494 TestSubscriber s1 = new TestSubscriber();
495 s1.request = false;
496 p.subscribe(s1);
497 s1.awaitSubscribe();
498 assertEquals(0, p.estimateMinimumDemand());
499 TestSubscriber s2 = new TestSubscriber();
500 p.subscribe(s2);
501 p.submit(1);
502 p.submit(2);
503 s2.awaitNext(1);
504 assertEquals(0, s1.nexts);
505 s1.sn.request(3);
506 p.submit(3);
507 p.close();
508 s2.awaitComplete();
509 assertEquals(3, s2.nexts);
510 assertEquals(1, s2.completes);
511 s1.awaitComplete();
512 assertTrue(s1.nexts > 0);
513 assertEquals(1, s1.completes);
514 }
515
516 /**
517 * onNext is not issued when requests become zero
518 */
519 public void testRequest2() {
520 SubmissionPublisher<Integer> p = basicPublisher();
521 TestSubscriber s1 = new TestSubscriber();
522 TestSubscriber s2 = new TestSubscriber();
523 p.subscribe(s1);
524 p.subscribe(s2);
525 s2.awaitSubscribe();
526 s1.awaitSubscribe();
527 s1.request = false;
528 p.submit(1);
529 p.submit(2);
530 p.close();
531 s2.awaitComplete();
532 assertEquals(2, s2.nexts);
533 assertEquals(1, s2.completes);
534 s1.awaitNext(1);
535 assertEquals(1, s1.nexts);
536 }
537
538 /**
539 * Non-positive request causes error
540 */
541 public void testRequest3() {
542 SubmissionPublisher<Integer> p = basicPublisher();
543 TestSubscriber s1 = new TestSubscriber();
544 TestSubscriber s2 = new TestSubscriber();
545 TestSubscriber s3 = new TestSubscriber();
546 p.subscribe(s1);
547 p.subscribe(s2);
548 p.subscribe(s3);
549 s3.awaitSubscribe();
550 s2.awaitSubscribe();
551 s1.awaitSubscribe();
552 s1.sn.request(-1L);
553 s3.sn.request(0L);
554 p.submit(1);
555 p.submit(2);
556 p.close();
557 s2.awaitComplete();
558 assertEquals(2, s2.nexts);
559 assertEquals(1, s2.completes);
560 s1.awaitError();
561 assertEquals(1, s1.errors);
562 assertTrue(s1.lastError instanceof IllegalArgumentException);
563 s3.awaitError();
564 assertEquals(1, s3.errors);
565 assertTrue(s3.lastError instanceof IllegalArgumentException);
566 }
567
568 /**
569 * estimateMinimumDemand reports 0 until request, nonzero after
570 * request
571 */
572 public void testEstimateMinimumDemand() {
573 TestSubscriber s = new TestSubscriber();
574 SubmissionPublisher<Integer> p = basicPublisher();
575 s.request = false;
576 p.subscribe(s);
577 s.awaitSubscribe();
578 assertEquals(0, p.estimateMinimumDemand());
579 s.sn.request(1);
580 assertEquals(1, p.estimateMinimumDemand());
581 }
582
583 /**
584 * submit to a publisher with no subscribers returns lag 0
585 */
586 public void testEmptySubmit() {
587 SubmissionPublisher<Integer> p = basicPublisher();
588 assertEquals(0, p.submit(1));
589 }
590
591 /**
592 * submit(null) throws NPE
593 */
594 public void testNullSubmit() {
595 SubmissionPublisher<Integer> p = basicPublisher();
596 try {
597 p.submit(null);
598 shouldThrow();
599 } catch (NullPointerException success) {}
600 }
601
602 /**
603 * submit returns number of lagged items, compatible with result
604 * of estimateMaximumLag.
605 */
606 public void testLaggedSubmit() {
607 SubmissionPublisher<Integer> p = basicPublisher();
608 TestSubscriber s1 = new TestSubscriber();
609 s1.request = false;
610 TestSubscriber s2 = new TestSubscriber();
611 s2.request = false;
612 p.subscribe(s1);
613 p.subscribe(s2);
614 s2.awaitSubscribe();
615 s1.awaitSubscribe();
616 assertEquals(1, p.submit(1));
617 assertTrue(p.estimateMaximumLag() >= 1);
618 assertTrue(p.submit(2) >= 2);
619 assertTrue(p.estimateMaximumLag() >= 2);
620 s1.sn.request(4);
621 assertTrue(p.submit(3) >= 3);
622 assertTrue(p.estimateMaximumLag() >= 3);
623 s2.sn.request(4);
624 p.submit(4);
625 p.close();
626 s2.awaitComplete();
627 assertEquals(4, s2.nexts);
628 s1.awaitComplete();
629 assertEquals(4, s2.nexts);
630 }
631
632 /**
633 * submit eventually issues requested items when buffer capacity is 1
634 */
635 public void testCap1Submit() {
636 SubmissionPublisher<Integer> p
637 = new SubmissionPublisher<>(basicExecutor, 1);
638 TestSubscriber s1 = new TestSubscriber();
639 TestSubscriber s2 = new TestSubscriber();
640 p.subscribe(s1);
641 p.subscribe(s2);
642 for (int i = 1; i <= 20; ++i) {
643 assertTrue(p.submit(i) >= 0);
644 }
645 p.close();
646 s2.awaitComplete();
647 s1.awaitComplete();
648 assertEquals(20, s2.nexts);
649 assertEquals(1, s2.completes);
650 assertEquals(20, s1.nexts);
651 assertEquals(1, s1.completes);
652 }
653
654 static boolean noopHandle(AtomicInteger count) {
655 count.getAndIncrement();
656 return false;
657 }
658
659 static boolean reqHandle(AtomicInteger count, Subscriber s) {
660 count.getAndIncrement();
661 ((TestSubscriber)s).sn.request(Long.MAX_VALUE);
662 return true;
663 }
664
665 /**
666 * offer to a publisher with no subscribers returns lag 0
667 */
668 public void testEmptyOffer() {
669 SubmissionPublisher<Integer> p = basicPublisher();
670 assertEquals(0, p.offer(1, null));
671 }
672
673 /**
674 * offer(null) throws NPE
675 */
676 public void testNullOffer() {
677 SubmissionPublisher<Integer> p = basicPublisher();
678 try {
679 p.offer(null, null);
680 shouldThrow();
681 } catch (NullPointerException success) {}
682 }
683
684 /**
685 * offer returns number of lagged items if not saturated
686 */
687 public void testLaggedOffer() {
688 SubmissionPublisher<Integer> p = basicPublisher();
689 TestSubscriber s1 = new TestSubscriber();
690 s1.request = false;
691 TestSubscriber s2 = new TestSubscriber();
692 s2.request = false;
693 p.subscribe(s1);
694 p.subscribe(s2);
695 s2.awaitSubscribe();
696 s1.awaitSubscribe();
697 assertTrue(p.offer(1, null) >= 1);
698 assertTrue(p.offer(2, null) >= 2);
699 s1.sn.request(4);
700 assertTrue(p.offer(3, null) >= 3);
701 s2.sn.request(4);
702 p.offer(4, null);
703 p.close();
704 s2.awaitComplete();
705 assertEquals(4, s2.nexts);
706 s1.awaitComplete();
707 assertEquals(4, s2.nexts);
708 }
709
710 /**
711 * offer reports drops if saturated
712 */
713 public void testDroppedOffer() {
714 SubmissionPublisher<Integer> p
715 = new SubmissionPublisher<>(basicExecutor, 4);
716 TestSubscriber s1 = new TestSubscriber();
717 s1.request = false;
718 TestSubscriber s2 = new TestSubscriber();
719 s2.request = false;
720 p.subscribe(s1);
721 p.subscribe(s2);
722 s2.awaitSubscribe();
723 s1.awaitSubscribe();
724 for (int i = 1; i <= 4; ++i)
725 assertTrue(p.offer(i, null) >= 0);
726 p.offer(5, null);
727 assertTrue(p.offer(6, null) < 0);
728 s1.sn.request(64);
729 assertTrue(p.offer(7, null) < 0);
730 s2.sn.request(64);
731 p.close();
732 s2.awaitComplete();
733 assertTrue(s2.nexts >= 4);
734 s1.awaitComplete();
735 assertTrue(s1.nexts >= 4);
736 }
737
738 /**
739 * offer invokes drop handler if saturated
740 */
741 public void testHandledDroppedOffer() {
742 AtomicInteger calls = new AtomicInteger();
743 SubmissionPublisher<Integer> p
744 = new SubmissionPublisher<>(basicExecutor, 4);
745 TestSubscriber s1 = new TestSubscriber();
746 s1.request = false;
747 TestSubscriber s2 = new TestSubscriber();
748 s2.request = false;
749 p.subscribe(s1);
750 p.subscribe(s2);
751 s2.awaitSubscribe();
752 s1.awaitSubscribe();
753 for (int i = 1; i <= 4; ++i)
754 assertTrue(p.offer(i, (s, x) -> noopHandle(calls)) >= 0);
755 p.offer(4, (s, x) -> noopHandle(calls));
756 assertTrue(p.offer(6, (s, x) -> noopHandle(calls)) < 0);
757 s1.sn.request(64);
758 assertTrue(p.offer(7, (s, x) -> noopHandle(calls)) < 0);
759 s2.sn.request(64);
760 p.close();
761 s2.awaitComplete();
762 s1.awaitComplete();
763 assertTrue(calls.get() >= 4);
764 }
765
766 /**
767 * offer succeeds if drop handler forces request
768 */
769 public void testRecoveredHandledDroppedOffer() {
770 AtomicInteger calls = new AtomicInteger();
771 SubmissionPublisher<Integer> p
772 = new SubmissionPublisher<>(basicExecutor, 4);
773 TestSubscriber s1 = new TestSubscriber();
774 s1.request = false;
775 TestSubscriber s2 = new TestSubscriber();
776 s2.request = false;
777 p.subscribe(s1);
778 p.subscribe(s2);
779 s2.awaitSubscribe();
780 s1.awaitSubscribe();
781 int n = 0;
782 for (int i = 1; i <= 8; ++i) {
783 int d = p.offer(i, (s, x) -> reqHandle(calls, s));
784 n = n + 2 + (d < 0 ? d : 0);
785 }
786 p.close();
787 s2.awaitComplete();
788 s1.awaitComplete();
789 assertEquals(n, s1.nexts + s2.nexts);
790 assertTrue(calls.get() >= 2);
791 }
792
793 /**
794 * Timed offer to a publisher with no subscribers returns lag 0
795 */
796 public void testEmptyTimedOffer() {
797 SubmissionPublisher<Integer> p = basicPublisher();
798 long startTime = System.nanoTime();
799 assertEquals(0, p.offer(1, LONG_DELAY_MS, MILLISECONDS, null));
800 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
801 }
802
803 /**
804 * Timed offer with null item or TimeUnit throws NPE
805 */
806 public void testNullTimedOffer() {
807 SubmissionPublisher<Integer> p = basicPublisher();
808 long startTime = System.nanoTime();
809 try {
810 p.offer(null, LONG_DELAY_MS, MILLISECONDS, null);
811 shouldThrow();
812 } catch (NullPointerException success) {}
813 try {
814 p.offer(1, LONG_DELAY_MS, null, null);
815 shouldThrow();
816 } catch (NullPointerException success) {}
817 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
818 }
819
820 /**
821 * Timed offer returns number of lagged items if not saturated
822 */
823 public void testLaggedTimedOffer() {
824 SubmissionPublisher<Integer> p = basicPublisher();
825 TestSubscriber s1 = new TestSubscriber();
826 s1.request = false;
827 TestSubscriber s2 = new TestSubscriber();
828 s2.request = false;
829 p.subscribe(s1);
830 p.subscribe(s2);
831 s2.awaitSubscribe();
832 s1.awaitSubscribe();
833 long startTime = System.nanoTime();
834 assertTrue(p.offer(1, LONG_DELAY_MS, MILLISECONDS, null) >= 1);
835 assertTrue(p.offer(2, LONG_DELAY_MS, MILLISECONDS, null) >= 2);
836 s1.sn.request(4);
837 assertTrue(p.offer(3, LONG_DELAY_MS, MILLISECONDS, null) >= 3);
838 s2.sn.request(4);
839 p.offer(4, LONG_DELAY_MS, MILLISECONDS, null);
840 p.close();
841 s2.awaitComplete();
842 assertEquals(4, s2.nexts);
843 s1.awaitComplete();
844 assertEquals(4, s2.nexts);
845 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
846 }
847
848 /**
849 * Timed offer reports drops if saturated
850 */
851 public void testDroppedTimedOffer() {
852 SubmissionPublisher<Integer> p
853 = new SubmissionPublisher<>(basicExecutor, 4);
854 TestSubscriber s1 = new TestSubscriber();
855 s1.request = false;
856 TestSubscriber s2 = new TestSubscriber();
857 s2.request = false;
858 p.subscribe(s1);
859 p.subscribe(s2);
860 s2.awaitSubscribe();
861 s1.awaitSubscribe();
862 long delay = timeoutMillis();
863 for (int i = 1; i <= 4; ++i)
864 assertTrue(p.offer(i, delay, MILLISECONDS, null) >= 0);
865 long startTime = System.nanoTime();
866 assertTrue(p.offer(5, delay, MILLISECONDS, null) < 0);
867 s1.sn.request(64);
868 assertTrue(p.offer(6, delay, MILLISECONDS, null) < 0);
869 // 2 * delay should elapse but check only 1 * delay to allow timer slop
870 assertTrue(millisElapsedSince(startTime) >= delay);
871 s2.sn.request(64);
872 p.close();
873 s2.awaitComplete();
874 assertTrue(s2.nexts >= 2);
875 s1.awaitComplete();
876 assertTrue(s1.nexts >= 2);
877 }
878
879 /**
880 * Timed offer invokes drop handler if saturated
881 */
882 public void testHandledDroppedTimedOffer() {
883 AtomicInteger calls = new AtomicInteger();
884 SubmissionPublisher<Integer> p
885 = new SubmissionPublisher<>(basicExecutor, 4);
886 TestSubscriber s1 = new TestSubscriber();
887 s1.request = false;
888 TestSubscriber s2 = new TestSubscriber();
889 s2.request = false;
890 p.subscribe(s1);
891 p.subscribe(s2);
892 s2.awaitSubscribe();
893 s1.awaitSubscribe();
894 long delay = timeoutMillis();
895 for (int i = 1; i <= 4; ++i)
896 assertTrue(p.offer(i, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) >= 0);
897 long startTime = System.nanoTime();
898 assertTrue(p.offer(5, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0);
899 s1.sn.request(64);
900 assertTrue(p.offer(6, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0);
901 assertTrue(millisElapsedSince(startTime) >= delay);
902 s2.sn.request(64);
903 p.close();
904 s2.awaitComplete();
905 s1.awaitComplete();
906 assertTrue(calls.get() >= 2);
907 }
908
909 /**
910 * Timed offer succeeds if drop handler forces request
911 */
912 public void testRecoveredHandledDroppedTimedOffer() {
913 AtomicInteger calls = new AtomicInteger();
914 SubmissionPublisher<Integer> p
915 = new SubmissionPublisher<>(basicExecutor, 4);
916 TestSubscriber s1 = new TestSubscriber();
917 s1.request = false;
918 TestSubscriber s2 = new TestSubscriber();
919 s2.request = false;
920 p.subscribe(s1);
921 p.subscribe(s2);
922 s2.awaitSubscribe();
923 s1.awaitSubscribe();
924 int n = 0;
925 long delay = timeoutMillis();
926 long startTime = System.nanoTime();
927 for (int i = 1; i <= 6; ++i) {
928 int d = p.offer(i, delay, MILLISECONDS, (s, x) -> reqHandle(calls, s));
929 n = n + 2 + (d < 0 ? d : 0);
930 }
931 assertTrue(millisElapsedSince(startTime) >= delay);
932 p.close();
933 s2.awaitComplete();
934 s1.awaitComplete();
935 assertEquals(n, s1.nexts + s2.nexts);
936 assertTrue(calls.get() >= 2);
937 }
938
939 /**
940 * consume returns a CompletableFuture that is done when
941 * publisher completes
942 */
943 public void testConsume() {
944 AtomicInteger sum = new AtomicInteger();
945 SubmissionPublisher<Integer> p = basicPublisher();
946 CompletableFuture<Void> f =
947 p.consume((Integer x) -> sum.getAndAdd(x.intValue()));
948 int n = 20;
949 for (int i = 1; i <= n; ++i)
950 p.submit(i);
951 p.close();
952 f.join();
953 assertEquals((n * (n + 1)) / 2, sum.get());
954 }
955
956 /**
957 * consume(null) throws NPE
958 */
959 public void testConsumeNPE() {
960 SubmissionPublisher<Integer> p = basicPublisher();
961 try {
962 CompletableFuture<Void> f = p.consume(null);
963 shouldThrow();
964 } catch (NullPointerException success) {}
965 }
966
967 /**
968 * consume eventually stops processing published items if cancelled
969 */
970 public void testCancelledConsume() {
971 AtomicInteger count = new AtomicInteger();
972 SubmissionPublisher<Integer> p = basicPublisher();
973 CompletableFuture<Void> f = p.consume(x -> count.getAndIncrement());
974 f.cancel(true);
975 int n = 1000000; // arbitrary limit
976 for (int i = 1; i <= n; ++i)
977 p.submit(i);
978 assertTrue(count.get() < n);
979 }
980
981 }