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