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