10 |
|
import java.util.concurrent.Executors; |
11 |
|
import java.util.concurrent.Flow; |
12 |
|
import java.util.concurrent.ForkJoinPool; |
13 |
– |
import java.util.concurrent.LinkedBlockingQueue; |
13 |
|
import java.util.concurrent.SubmissionPublisher; |
15 |
– |
import java.util.concurrent.ThreadFactory; |
16 |
– |
import java.util.concurrent.ThreadPoolExecutor; |
17 |
– |
import java.util.concurrent.TimeUnit; |
14 |
|
import java.util.concurrent.atomic.AtomicInteger; |
19 |
– |
import java.util.function.BiConsumer; |
20 |
– |
import java.util.function.BiFunction; |
21 |
– |
import java.util.function.BiPredicate; |
22 |
– |
import java.util.stream.Stream; |
15 |
|
import junit.framework.Test; |
16 |
|
import junit.framework.TestSuite; |
17 |
|
|
26 |
– |
import static java.util.concurrent.Flow.Publisher; |
18 |
|
import static java.util.concurrent.Flow.Subscriber; |
19 |
|
import static java.util.concurrent.Flow.Subscription; |
20 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
30 |
– |
import static java.util.concurrent.TimeUnit.SECONDS; |
21 |
|
|
22 |
|
public class SubmissionPublisherTest extends JSR166TestCase { |
23 |
|
|
28 |
|
return new TestSuite(SubmissionPublisherTest.class); |
29 |
|
} |
30 |
|
|
31 |
< |
// Factory for single thread pool in case commonPool parallelism is zero |
42 |
< |
static final class DaemonThreadFactory implements ThreadFactory { |
43 |
< |
public Thread newThread(Runnable r) { |
44 |
< |
Thread t = new Thread(r); |
45 |
< |
t.setDaemon(true); |
46 |
< |
return t; |
47 |
< |
} |
48 |
< |
} |
49 |
< |
|
50 |
< |
static final Executor basicExecutor = |
51 |
< |
(ForkJoinPool.getCommonPoolParallelism() > 1) ? |
52 |
< |
ForkJoinPool.commonPool() : |
53 |
< |
new ThreadPoolExecutor(1, 1, 60, SECONDS, |
54 |
< |
new LinkedBlockingQueue<Runnable>(), |
55 |
< |
new DaemonThreadFactory()); |
31 |
> |
final Executor basicExecutor = basicPublisher().getExecutor(); |
32 |
|
|
33 |
|
static SubmissionPublisher<Integer> basicPublisher() { |
34 |
< |
return new SubmissionPublisher<Integer>(basicExecutor, |
59 |
< |
Flow.defaultBufferSize()); |
34 |
> |
return new SubmissionPublisher<Integer>(); |
35 |
|
} |
36 |
|
|
37 |
|
static class SPException extends RuntimeException {} |
143 |
|
/** |
144 |
|
* A default-constructed SubmissionPublisher has no subscribers, |
145 |
|
* is not closed, has default buffer size, and uses the |
146 |
< |
* ForkJoinPool.commonPool executor |
146 |
> |
* defaultExecutor |
147 |
|
*/ |
148 |
|
public void testConstructor1() { |
149 |
< |
SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>(); |
149 |
> |
SubmissionPublisher<Integer> p = new SubmissionPublisher<>(); |
150 |
|
checkInitialState(p); |
176 |
– |
assertSame(p.getExecutor(), ForkJoinPool.commonPool()); |
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 |
|
/** |
162 |
|
*/ |
163 |
|
public void testConstructor2() { |
164 |
|
Executor e = Executors.newFixedThreadPool(1); |
165 |
< |
SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>(e, 8); |
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 throws NPE |
172 |
> |
* A null Executor argument to SubmissionPublisher constructor |
173 |
> |
* throws NullPointerException |
174 |
|
*/ |
175 |
|
public void testConstructor3() { |
176 |
|
try { |
181 |
|
|
182 |
|
/** |
183 |
|
* A negative capacity argument to SubmissionPublisher constructor |
184 |
< |
* throws IAE |
184 |
> |
* throws IllegalArgumentException |
185 |
|
*/ |
186 |
|
public void testConstructor4() { |
187 |
|
Executor e = Executors.newFixedThreadPool(1); |
193 |
|
|
194 |
|
/** |
195 |
|
* A closed publisher reports isClosed with no closedException and |
196 |
< |
* throws ISE upon attempted submission; a subsequent close or |
197 |
< |
* closeExceptionally has no additional effect. |
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(); |
215 |
|
|
216 |
|
/** |
217 |
|
* A publisher closedExceptionally reports isClosed with the |
218 |
< |
* closedException and throws ISE upon attempted submission; a |
219 |
< |
* subsequent close or closeExceptionally has no additional |
220 |
< |
* effect. |
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(); |
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(); |
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); |
446 |
|
*/ |
447 |
|
public void testThrowOnNextHandler() { |
448 |
|
AtomicInteger calls = new AtomicInteger(); |
449 |
< |
SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer> |
450 |
< |
(basicExecutor, 8, |
467 |
< |
(s, e) -> calls.getAndIncrement()); |
449 |
> |
SubmissionPublisher<Integer> p = new SubmissionPublisher<>( |
450 |
> |
basicExecutor, 8, (s, e) -> calls.getAndIncrement()); |
451 |
|
TestSubscriber s1 = new TestSubscriber(); |
452 |
|
TestSubscriber s2 = new TestSubscriber(); |
453 |
|
p.subscribe(s1); |
494 |
|
s1.request = false; |
495 |
|
p.subscribe(s1); |
496 |
|
s1.awaitSubscribe(); |
497 |
< |
assertTrue(p.estimateMinimumDemand() == 0); |
497 |
> |
assertEquals(0, p.estimateMinimumDemand()); |
498 |
|
TestSubscriber s2 = new TestSubscriber(); |
499 |
|
p.subscribe(s2); |
500 |
|
p.submit(1); |
535 |
|
} |
536 |
|
|
537 |
|
/** |
538 |
< |
* Negative request causes error |
538 |
> |
* Non-positive request causes error |
539 |
|
*/ |
540 |
|
public void testRequest3() { |
541 |
|
SubmissionPublisher<Integer> p = basicPublisher(); |
542 |
|
TestSubscriber s1 = new TestSubscriber(); |
543 |
|
TestSubscriber s2 = new TestSubscriber(); |
544 |
+ |
TestSubscriber s3 = new TestSubscriber(); |
545 |
|
p.subscribe(s1); |
546 |
|
p.subscribe(s2); |
547 |
+ |
p.subscribe(s3); |
548 |
+ |
s3.awaitSubscribe(); |
549 |
|
s2.awaitSubscribe(); |
550 |
|
s1.awaitSubscribe(); |
551 |
|
s1.sn.request(-1L); |
552 |
+ |
s3.sn.request(0L); |
553 |
|
p.submit(1); |
554 |
|
p.submit(2); |
555 |
|
p.close(); |
559 |
|
s1.awaitError(); |
560 |
|
assertEquals(1, s1.errors); |
561 |
|
assertTrue(s1.lastError instanceof IllegalArgumentException); |
562 |
+ |
s3.awaitError(); |
563 |
+ |
assertEquals(1, s3.errors); |
564 |
+ |
assertTrue(s3.lastError instanceof IllegalArgumentException); |
565 |
|
} |
566 |
|
|
567 |
|
/** |
568 |
|
* estimateMinimumDemand reports 0 until request, nonzero after |
569 |
< |
* request, and zero again after delivery |
569 |
> |
* request |
570 |
|
*/ |
571 |
|
public void testEstimateMinimumDemand() { |
572 |
|
TestSubscriber s = new TestSubscriber(); |
577 |
|
assertEquals(0, p.estimateMinimumDemand()); |
578 |
|
s.sn.request(1); |
579 |
|
assertEquals(1, p.estimateMinimumDemand()); |
590 |
– |
p.submit(1); |
591 |
– |
s.awaitNext(1); |
592 |
– |
assertEquals(0, p.estimateMinimumDemand()); |
580 |
|
} |
581 |
|
|
582 |
|
/** |
632 |
|
* submit eventually issues requested items when buffer capacity is 1 |
633 |
|
*/ |
634 |
|
public void testCap1Submit() { |
635 |
< |
SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>( |
636 |
< |
basicExecutor, 1); |
635 |
> |
SubmissionPublisher<Integer> p |
636 |
> |
= new SubmissionPublisher<>(basicExecutor, 1); |
637 |
|
TestSubscriber s1 = new TestSubscriber(); |
638 |
|
TestSubscriber s2 = new TestSubscriber(); |
639 |
|
p.subscribe(s1); |
711 |
|
* offer reports drops if saturated |
712 |
|
*/ |
713 |
|
public void testDroppedOffer() { |
714 |
< |
SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>( |
715 |
< |
basicExecutor, 4); |
714 |
> |
SubmissionPublisher<Integer> p |
715 |
> |
= new SubmissionPublisher<>(basicExecutor, 4); |
716 |
|
TestSubscriber s1 = new TestSubscriber(); |
717 |
|
s1.request = false; |
718 |
|
TestSubscriber s2 = new TestSubscriber(); |
740 |
|
*/ |
741 |
|
public void testHandledDroppedOffer() { |
742 |
|
AtomicInteger calls = new AtomicInteger(); |
743 |
< |
SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>( |
744 |
< |
basicExecutor, 4); |
743 |
> |
SubmissionPublisher<Integer> p |
744 |
> |
= new SubmissionPublisher<>(basicExecutor, 4); |
745 |
|
TestSubscriber s1 = new TestSubscriber(); |
746 |
|
s1.request = false; |
747 |
|
TestSubscriber s2 = new TestSubscriber(); |
768 |
|
*/ |
769 |
|
public void testRecoveredHandledDroppedOffer() { |
770 |
|
AtomicInteger calls = new AtomicInteger(); |
771 |
< |
SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>( |
772 |
< |
basicExecutor, 4); |
771 |
> |
SubmissionPublisher<Integer> p |
772 |
> |
= new SubmissionPublisher<>(basicExecutor, 4); |
773 |
|
TestSubscriber s1 = new TestSubscriber(); |
774 |
|
s1.request = false; |
775 |
|
TestSubscriber s2 = new TestSubscriber(); |
849 |
|
* Timed offer reports drops if saturated |
850 |
|
*/ |
851 |
|
public void testDroppedTimedOffer() { |
852 |
< |
SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>( |
853 |
< |
basicExecutor, 4); |
852 |
> |
SubmissionPublisher<Integer> p |
853 |
> |
= new SubmissionPublisher<>(basicExecutor, 4); |
854 |
|
TestSubscriber s1 = new TestSubscriber(); |
855 |
|
s1.request = false; |
856 |
|
TestSubscriber s2 = new TestSubscriber(); |
881 |
|
*/ |
882 |
|
public void testHandledDroppedTimedOffer() { |
883 |
|
AtomicInteger calls = new AtomicInteger(); |
884 |
< |
SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>( |
885 |
< |
basicExecutor, 4); |
884 |
> |
SubmissionPublisher<Integer> p |
885 |
> |
= new SubmissionPublisher<>(basicExecutor, 4); |
886 |
|
TestSubscriber s1 = new TestSubscriber(); |
887 |
|
s1.request = false; |
888 |
|
TestSubscriber s2 = new TestSubscriber(); |
911 |
|
*/ |
912 |
|
public void testRecoveredHandledDroppedTimedOffer() { |
913 |
|
AtomicInteger calls = new AtomicInteger(); |
914 |
< |
SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>( |
915 |
< |
basicExecutor, 4); |
914 |
> |
SubmissionPublisher<Integer> p |
915 |
> |
= new SubmissionPublisher<>(basicExecutor, 4); |
916 |
|
TestSubscriber s1 = new TestSubscriber(); |
917 |
|
s1.request = false; |
918 |
|
TestSubscriber s2 = new TestSubscriber(); |
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()); }); |
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); |
961 |
|
try { |
962 |
|
CompletableFuture<Void> f = p.consume(null); |
963 |
|
shouldThrow(); |
964 |
< |
} catch(NullPointerException success) { |
978 |
< |
} |
964 |
> |
} catch (NullPointerException success) {} |
965 |
|
} |
966 |
|
|
967 |
|
/** |
977 |
|
p.submit(i); |
978 |
|
assertTrue(count.get() < n); |
979 |
|
} |
980 |
< |
|
980 |
> |
|
981 |
|
} |