6 |
|
*/ |
7 |
|
|
8 |
|
import java.util.concurrent.CompletableFuture; |
9 |
+ |
import java.util.concurrent.CountDownLatch; |
10 |
|
import java.util.concurrent.Executor; |
11 |
|
import java.util.concurrent.Executors; |
12 |
|
import java.util.concurrent.Flow; |
170 |
|
} |
171 |
|
|
172 |
|
/** |
173 |
< |
* A null Executor argument to SubmissionPublisher constructor throws NPE |
173 |
> |
* A null Executor argument to SubmissionPublisher constructor |
174 |
> |
* throws NullPointerException |
175 |
|
*/ |
176 |
|
public void testConstructor3() { |
177 |
|
try { |
182 |
|
|
183 |
|
/** |
184 |
|
* A negative capacity argument to SubmissionPublisher constructor |
185 |
< |
* throws IAE |
185 |
> |
* throws IllegalArgumentException |
186 |
|
*/ |
187 |
|
public void testConstructor4() { |
188 |
|
Executor e = Executors.newFixedThreadPool(1); |
194 |
|
|
195 |
|
/** |
196 |
|
* A closed publisher reports isClosed with no closedException and |
197 |
< |
* throws ISE upon attempted submission; a subsequent close or |
198 |
< |
* closeExceptionally has no additional effect. |
197 |
> |
* throws IllegalStateException upon attempted submission; a |
198 |
> |
* subsequent close or closeExceptionally has no additional |
199 |
> |
* effect. |
200 |
|
*/ |
201 |
|
public void testClose() { |
202 |
|
SubmissionPublisher<Integer> p = basicPublisher(); |
216 |
|
|
217 |
|
/** |
218 |
|
* A publisher closedExceptionally reports isClosed with the |
219 |
< |
* closedException and throws ISE upon attempted submission; a |
220 |
< |
* subsequent close or closeExceptionally has no additional |
221 |
< |
* effect. |
219 |
> |
* closedException and throws IllegalStateException upon attempted |
220 |
> |
* submission; a subsequent close or closeExceptionally has no |
221 |
> |
* additional effect. |
222 |
|
*/ |
223 |
|
public void testCloseExceptionally() { |
224 |
|
SubmissionPublisher<Integer> p = basicPublisher(); |
403 |
|
* Cancelling a subscription eventually causes no more onNexts to be issued |
404 |
|
*/ |
405 |
|
public void testCancel() { |
406 |
< |
SubmissionPublisher<Integer> p = basicPublisher(); |
406 |
> |
SubmissionPublisher<Integer> p = |
407 |
> |
new SubmissionPublisher<Integer>(basicExecutor, 4); // must be < 20 |
408 |
|
TestSubscriber s1 = new TestSubscriber(); |
409 |
|
TestSubscriber s2 = new TestSubscriber(); |
410 |
|
p.subscribe(s1); |
496 |
|
s1.request = false; |
497 |
|
p.subscribe(s1); |
498 |
|
s1.awaitSubscribe(); |
499 |
< |
assertTrue(p.estimateMinimumDemand() == 0); |
499 |
> |
assertEquals(0, p.estimateMinimumDemand()); |
500 |
|
TestSubscriber s2 = new TestSubscriber(); |
501 |
|
p.subscribe(s2); |
502 |
|
p.submit(1); |
537 |
|
} |
538 |
|
|
539 |
|
/** |
540 |
< |
* Negative request causes error |
540 |
> |
* Non-positive request causes error |
541 |
|
*/ |
542 |
|
public void testRequest3() { |
543 |
|
SubmissionPublisher<Integer> p = basicPublisher(); |
544 |
|
TestSubscriber s1 = new TestSubscriber(); |
545 |
|
TestSubscriber s2 = new TestSubscriber(); |
546 |
+ |
TestSubscriber s3 = new TestSubscriber(); |
547 |
|
p.subscribe(s1); |
548 |
|
p.subscribe(s2); |
549 |
+ |
p.subscribe(s3); |
550 |
+ |
s3.awaitSubscribe(); |
551 |
|
s2.awaitSubscribe(); |
552 |
|
s1.awaitSubscribe(); |
553 |
|
s1.sn.request(-1L); |
554 |
+ |
s3.sn.request(0L); |
555 |
|
p.submit(1); |
556 |
|
p.submit(2); |
557 |
|
p.close(); |
561 |
|
s1.awaitError(); |
562 |
|
assertEquals(1, s1.errors); |
563 |
|
assertTrue(s1.lastError instanceof IllegalArgumentException); |
564 |
+ |
s3.awaitError(); |
565 |
+ |
assertEquals(1, s3.errors); |
566 |
+ |
assertTrue(s3.lastError instanceof IllegalArgumentException); |
567 |
|
} |
568 |
|
|
569 |
|
/** |
570 |
|
* estimateMinimumDemand reports 0 until request, nonzero after |
571 |
< |
* request, and zero again after delivery |
571 |
> |
* request |
572 |
|
*/ |
573 |
|
public void testEstimateMinimumDemand() { |
574 |
|
TestSubscriber s = new TestSubscriber(); |
579 |
|
assertEquals(0, p.estimateMinimumDemand()); |
580 |
|
s.sn.request(1); |
581 |
|
assertEquals(1, p.estimateMinimumDemand()); |
571 |
– |
p.submit(1); |
572 |
– |
s.awaitNext(1); |
573 |
– |
assertEquals(0, p.estimateMinimumDemand()); |
582 |
|
} |
583 |
|
|
584 |
|
/** |
641 |
|
p.subscribe(s1); |
642 |
|
p.subscribe(s2); |
643 |
|
for (int i = 1; i <= 20; ++i) { |
636 |
– |
assertTrue(p.estimateMinimumDemand() <= 1); |
644 |
|
assertTrue(p.submit(i) >= 0); |
645 |
|
} |
646 |
|
p.close(); |
979 |
|
assertTrue(count.get() < n); |
980 |
|
} |
981 |
|
|
982 |
+ |
/** |
983 |
+ |
* Tests scenario for |
984 |
+ |
* JDK-8187947: A race condition in SubmissionPublisher |
985 |
+ |
* 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 |
986 |
+ |
*/ |
987 |
+ |
public void testMissedSignal_8187947() throws Exception { |
988 |
+ |
final int N = expensiveTests ? (1 << 20) : (1 << 10); |
989 |
+ |
final CountDownLatch finished = new CountDownLatch(1); |
990 |
+ |
final SubmissionPublisher<Boolean> pub = new SubmissionPublisher<>(); |
991 |
+ |
class Sub implements Subscriber<Boolean> { |
992 |
+ |
int received; |
993 |
+ |
public void onSubscribe(Subscription s) { |
994 |
+ |
s.request(N); |
995 |
+ |
} |
996 |
+ |
public void onNext(Boolean item) { |
997 |
+ |
if (++received == N) |
998 |
+ |
finished.countDown(); |
999 |
+ |
else |
1000 |
+ |
CompletableFuture.runAsync(() -> pub.submit(Boolean.TRUE)); |
1001 |
+ |
} |
1002 |
+ |
public void onError(Throwable t) { throw new AssertionError(t); } |
1003 |
+ |
public void onComplete() {} |
1004 |
+ |
} |
1005 |
+ |
pub.subscribe(new Sub()); |
1006 |
+ |
CompletableFuture.runAsync(() -> pub.submit(Boolean.TRUE)); |
1007 |
+ |
await(finished); |
1008 |
+ |
} |
1009 |
|
} |