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>(); |
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 |
|
/** |
935 |
|
public void testConsume() { |
936 |
|
AtomicInteger sum = new AtomicInteger(); |
937 |
|
SubmissionPublisher<Integer> p = basicPublisher(); |
938 |
< |
CompletableFuture<Void> f = |
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) |
953 |
|
try { |
954 |
|
CompletableFuture<Void> f = p.consume(null); |
955 |
|
shouldThrow(); |
956 |
< |
} catch(NullPointerException success) { |
978 |
< |
} |
956 |
> |
} catch (NullPointerException success) {} |
957 |
|
} |
958 |
|
|
959 |
|
/** |
969 |
|
p.submit(i); |
970 |
|
assertTrue(count.get() < n); |
971 |
|
} |
972 |
< |
|
972 |
> |
|
973 |
|
} |