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