5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
10 |
< |
|
8 |
> |
import java.util.concurrent.CompletableFuture; |
9 |
|
import java.util.concurrent.Executor; |
10 |
|
import java.util.concurrent.Executors; |
11 |
|
import java.util.concurrent.Flow; |
14 |
– |
import static java.util.concurrent.Flow.Publisher; |
15 |
– |
import static java.util.concurrent.Flow.Subscriber; |
16 |
– |
import static java.util.concurrent.Flow.Subscription; |
17 |
– |
import java.util.concurrent.LinkedBlockingQueue; |
12 |
|
import java.util.concurrent.ForkJoinPool; |
13 |
|
import java.util.concurrent.SubmissionPublisher; |
20 |
– |
import java.util.concurrent.ThreadFactory; |
21 |
– |
import java.util.concurrent.ThreadPoolExecutor; |
22 |
– |
import java.util.concurrent.TimeUnit; |
14 |
|
import java.util.concurrent.atomic.AtomicInteger; |
24 |
– |
import java.util.function.BiConsumer; |
25 |
– |
import java.util.function.BiPredicate; |
26 |
– |
import java.util.function.BiFunction; |
27 |
– |
|
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) { |
28 |
|
return new TestSuite(SubmissionPublisherTest.class); |
29 |
|
} |
30 |
|
|
31 |
< |
// Factory for single thread pool in case commonPool parallelism is zero |
41 |
< |
static final class DaemonThreadFactory implements ThreadFactory { |
42 |
< |
public Thread newThread(Runnable r) { |
43 |
< |
Thread t = new Thread(r); |
44 |
< |
t.setDaemon(true); |
45 |
< |
return t; |
46 |
< |
} |
47 |
< |
} |
48 |
< |
|
49 |
< |
static final Executor basicExecutor = |
50 |
< |
(ForkJoinPool.getCommonPoolParallelism() > 0) ? |
51 |
< |
ForkJoinPool.commonPool() : |
52 |
< |
new ThreadPoolExecutor(1, 1, 60, SECONDS, |
53 |
< |
new LinkedBlockingQueue<Runnable>(), |
54 |
< |
new DaemonThreadFactory()); |
31 |
> |
final Executor basicExecutor = basicPublisher().getExecutor(); |
32 |
|
|
33 |
|
static SubmissionPublisher<Integer> basicPublisher() { |
34 |
< |
return new SubmissionPublisher<Integer>(basicExecutor, |
58 |
< |
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>(); |
150 |
|
checkInitialState(p); |
175 |
– |
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 |
|
/** |
265 |
|
assertEquals(0, s2.nexts); |
266 |
|
assertEquals(0, s2.errors); |
267 |
|
assertEquals(0, s2.completes); |
268 |
+ |
p.close(); |
269 |
|
} |
270 |
|
|
271 |
|
/** |
341 |
|
} |
342 |
|
|
343 |
|
/** |
344 |
< |
* subscribe(null) thows NPE |
344 |
> |
* subscribe(null) throws NPE |
345 |
|
*/ |
346 |
|
public void testSubscribe6() { |
347 |
|
SubmissionPublisher<Integer> p = basicPublisher(); |
436 |
|
} |
437 |
|
|
438 |
|
/** |
439 |
< |
* If a handler is supplied in conctructor, it is invoked when |
439 |
> |
* If a handler is supplied in constructor, it is invoked when |
440 |
|
* subscriber throws an exception in onNext |
441 |
|
*/ |
442 |
|
public void testThrowOnNextHandler() { |
787 |
|
*/ |
788 |
|
public void testEmptyTimedOffer() { |
789 |
|
SubmissionPublisher<Integer> p = basicPublisher(); |
790 |
+ |
long startTime = System.nanoTime(); |
791 |
|
assertEquals(0, p.offer(1, LONG_DELAY_MS, MILLISECONDS, null)); |
792 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
793 |
|
} |
794 |
|
|
795 |
|
/** |
797 |
|
*/ |
798 |
|
public void testNullTimedOffer() { |
799 |
|
SubmissionPublisher<Integer> p = basicPublisher(); |
800 |
+ |
long startTime = System.nanoTime(); |
801 |
|
try { |
802 |
< |
p.offer(null, SHORT_DELAY_MS, MILLISECONDS, null); |
802 |
> |
p.offer(null, LONG_DELAY_MS, MILLISECONDS, null); |
803 |
|
shouldThrow(); |
804 |
|
} catch (NullPointerException success) {} |
805 |
|
try { |
806 |
< |
p.offer(1, SHORT_DELAY_MS, null, null); |
806 |
> |
p.offer(1, LONG_DELAY_MS, null, null); |
807 |
|
shouldThrow(); |
808 |
|
} catch (NullPointerException success) {} |
809 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
810 |
|
} |
811 |
|
|
812 |
|
/** |
822 |
|
p.subscribe(s2); |
823 |
|
s2.awaitSubscribe(); |
824 |
|
s1.awaitSubscribe(); |
825 |
< |
assertTrue(p.offer(1, SHORT_DELAY_MS, MILLISECONDS, null) >= 1); |
826 |
< |
assertTrue(p.offer(2, SHORT_DELAY_MS, MILLISECONDS, null) >= 2); |
825 |
> |
long startTime = System.nanoTime(); |
826 |
> |
assertTrue(p.offer(1, LONG_DELAY_MS, MILLISECONDS, null) >= 1); |
827 |
> |
assertTrue(p.offer(2, LONG_DELAY_MS, MILLISECONDS, null) >= 2); |
828 |
|
s1.sn.request(4); |
829 |
< |
assertTrue(p.offer(3, SHORT_DELAY_MS, MILLISECONDS, null) >= 3); |
829 |
> |
assertTrue(p.offer(3, LONG_DELAY_MS, MILLISECONDS, null) >= 3); |
830 |
|
s2.sn.request(4); |
831 |
< |
p.offer(4, SHORT_DELAY_MS, MILLISECONDS, null); |
831 |
> |
p.offer(4, LONG_DELAY_MS, MILLISECONDS, null); |
832 |
|
p.close(); |
833 |
|
s2.awaitComplete(); |
834 |
|
assertEquals(4, s2.nexts); |
835 |
|
s1.awaitComplete(); |
836 |
|
assertEquals(4, s2.nexts); |
837 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
838 |
|
} |
839 |
|
|
840 |
|
/** |
851 |
|
p.subscribe(s2); |
852 |
|
s2.awaitSubscribe(); |
853 |
|
s1.awaitSubscribe(); |
854 |
+ |
long delay = timeoutMillis(); |
855 |
|
for (int i = 1; i <= 4; ++i) |
856 |
< |
assertTrue(p.offer(i, SHORT_DELAY_MS, MILLISECONDS, null) >= 0); |
857 |
< |
p.offer(5, SHORT_DELAY_MS, MILLISECONDS, null); |
858 |
< |
assertTrue(p.offer(6, SHORT_DELAY_MS, MILLISECONDS, null) < 0); |
856 |
> |
assertTrue(p.offer(i, delay, MILLISECONDS, null) >= 0); |
857 |
> |
long startTime = System.nanoTime(); |
858 |
> |
assertTrue(p.offer(5, delay, MILLISECONDS, null) < 0); |
859 |
|
s1.sn.request(64); |
860 |
< |
assertTrue(p.offer(7, SHORT_DELAY_MS, MILLISECONDS, null) < 0); |
860 |
> |
assertTrue(p.offer(6, delay, MILLISECONDS, null) < 0); |
861 |
> |
// 2 * delay should elapse but check only 1 * delay to allow timer slop |
862 |
> |
assertTrue(millisElapsedSince(startTime) >= delay); |
863 |
|
s2.sn.request(64); |
864 |
|
p.close(); |
865 |
|
s2.awaitComplete(); |
883 |
|
p.subscribe(s2); |
884 |
|
s2.awaitSubscribe(); |
885 |
|
s1.awaitSubscribe(); |
886 |
+ |
long delay = timeoutMillis(); |
887 |
|
for (int i = 1; i <= 4; ++i) |
888 |
< |
assertTrue(p.offer(i, SHORT_DELAY_MS, MILLISECONDS, (s, x) -> noopHandle(calls)) >= 0); |
889 |
< |
p.offer(5, (s, x) -> noopHandle(calls)); |
890 |
< |
assertTrue(p.offer(6, SHORT_DELAY_MS, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0); |
888 |
> |
assertTrue(p.offer(i, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) >= 0); |
889 |
> |
long startTime = System.nanoTime(); |
890 |
> |
assertTrue(p.offer(5, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0); |
891 |
|
s1.sn.request(64); |
892 |
< |
assertTrue(p.offer(7, SHORT_DELAY_MS, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0); |
892 |
> |
assertTrue(p.offer(6, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0); |
893 |
> |
assertTrue(millisElapsedSince(startTime) >= delay); |
894 |
|
s2.sn.request(64); |
895 |
|
p.close(); |
896 |
|
s2.awaitComplete(); |
914 |
|
s2.awaitSubscribe(); |
915 |
|
s1.awaitSubscribe(); |
916 |
|
int n = 0; |
917 |
< |
for (int i = 1; i <= 8; ++i) { |
918 |
< |
int d = p.offer(i, SHORT_DELAY_MS, MILLISECONDS, (s, x) -> reqHandle(calls, s)); |
917 |
> |
long delay = timeoutMillis(); |
918 |
> |
long startTime = System.nanoTime(); |
919 |
> |
for (int i = 1; i <= 6; ++i) { |
920 |
> |
int d = p.offer(i, delay, MILLISECONDS, (s, x) -> reqHandle(calls, s)); |
921 |
|
n = n + 2 + (d < 0 ? d : 0); |
922 |
|
} |
923 |
+ |
assertTrue(millisElapsedSince(startTime) >= delay); |
924 |
|
p.close(); |
925 |
|
s2.awaitComplete(); |
926 |
|
s1.awaitComplete(); |
928 |
|
assertTrue(calls.get() >= 2); |
929 |
|
} |
930 |
|
|
931 |
+ |
/** |
932 |
+ |
* consume returns a CompletableFuture that is done when |
933 |
+ |
* publisher completes |
934 |
+ |
*/ |
935 |
+ |
public void testConsume() { |
936 |
+ |
AtomicInteger sum = new AtomicInteger(); |
937 |
+ |
SubmissionPublisher<Integer> p = basicPublisher(); |
938 |
+ |
CompletableFuture<Void> f = |
939 |
+ |
p.consume((Integer x) -> { sum.getAndAdd(x.intValue()); }); |
940 |
+ |
int n = 20; |
941 |
+ |
for (int i = 1; i <= n; ++i) |
942 |
+ |
p.submit(i); |
943 |
+ |
p.close(); |
944 |
+ |
f.join(); |
945 |
+ |
assertEquals((n * (n + 1)) / 2, sum.get()); |
946 |
+ |
} |
947 |
+ |
|
948 |
+ |
/** |
949 |
+ |
* consume(null) throws NPE |
950 |
+ |
*/ |
951 |
+ |
public void testConsumeNPE() { |
952 |
+ |
SubmissionPublisher<Integer> p = basicPublisher(); |
953 |
+ |
try { |
954 |
+ |
CompletableFuture<Void> f = p.consume(null); |
955 |
+ |
shouldThrow(); |
956 |
+ |
} catch (NullPointerException success) {} |
957 |
+ |
} |
958 |
+ |
|
959 |
+ |
/** |
960 |
+ |
* consume eventually stops processing published items if cancelled |
961 |
+ |
*/ |
962 |
+ |
public void testCancelledConsume() { |
963 |
+ |
AtomicInteger count = new AtomicInteger(); |
964 |
+ |
SubmissionPublisher<Integer> p = basicPublisher(); |
965 |
+ |
CompletableFuture<Void> f = p.consume(x -> count.getAndIncrement()); |
966 |
+ |
f.cancel(true); |
967 |
+ |
int n = 1000000; // arbitrary limit |
968 |
+ |
for (int i = 1; i <= n; ++i) |
969 |
+ |
p.submit(i); |
970 |
+ |
assertTrue(count.get() < n); |
971 |
+ |
} |
972 |
+ |
|
973 |
|
} |