ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SubmissionPublisherTest.java
(Generate patch)

Comparing jsr166/src/test/tck/SubmissionPublisherTest.java (file contents):
Revision 1.19 by dl, Thu Mar 9 00:11:16 2017 UTC vs.
Revision 1.31 by dl, Tue Jan 26 13:33:06 2021 UTC

# Line 6 | Line 6
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;
# Line 169 | Line 170 | public class SubmissionPublisherTest ext
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 {
# Line 180 | Line 182 | public class SubmissionPublisherTest ext
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);
# Line 192 | Line 194 | public class SubmissionPublisherTest ext
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();
# Line 213 | Line 216 | public class SubmissionPublisherTest ext
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();
# Line 331 | Line 334 | public class SubmissionPublisherTest ext
334          TestSubscriber s = new TestSubscriber();
335          SubmissionPublisher<Integer> p = basicPublisher();
336          s.throwOnCall = true;
337 <        try {
335 <            p.subscribe(s);
336 <        } catch (Exception ok) {}
337 >        p.subscribe(s);
338          s.awaitError();
339          assertEquals(0, s.nexts);
340          assertEquals(1, s.errors);
# Line 400 | Line 401 | public class SubmissionPublisherTest ext
401       * Cancelling a subscription eventually causes no more onNexts to be issued
402       */
403      public void testCancel() {
404 <        SubmissionPublisher<Integer> p = basicPublisher();
404 >        SubmissionPublisher<Integer> p =
405 >            new SubmissionPublisher<>(basicExecutor, 4); // must be < 20
406          TestSubscriber s1 = new TestSubscriber();
407          TestSubscriber s2 = new TestSubscriber();
408          p.subscribe(s1);
# Line 492 | Line 494 | public class SubmissionPublisherTest ext
494          s1.request = false;
495          p.subscribe(s1);
496          s1.awaitSubscribe();
497 <        assertTrue(p.estimateMinimumDemand() == 0);
497 >        assertEquals(0, p.estimateMinimumDemand());
498          TestSubscriber s2 = new TestSubscriber();
499          p.subscribe(s2);
500          p.submit(1);
# Line 564 | Line 566 | public class SubmissionPublisherTest ext
566  
567      /**
568       * estimateMinimumDemand reports 0 until request, nonzero after
569 <     * request, and zero again after delivery
569 >     * request
570       */
571      public void testEstimateMinimumDemand() {
572          TestSubscriber s = new TestSubscriber();
# Line 575 | Line 577 | public class SubmissionPublisherTest ext
577          assertEquals(0, p.estimateMinimumDemand());
578          s.sn.request(1);
579          assertEquals(1, p.estimateMinimumDemand());
578        p.submit(1);
579        s.awaitNext(1);
580        assertEquals(0, p.estimateMinimumDemand());
580      }
581  
582      /**
# Line 640 | Line 639 | public class SubmissionPublisherTest ext
639          p.subscribe(s1);
640          p.subscribe(s2);
641          for (int i = 1; i <= 20; ++i) {
643            assertTrue(p.estimateMinimumDemand() <= 1);
642              assertTrue(p.submit(i) >= 0);
643          }
644          p.close();
# Line 657 | Line 655 | public class SubmissionPublisherTest ext
655          return false;
656      }
657  
658 <    static boolean reqHandle(AtomicInteger count, Subscriber s) {
658 >    static boolean reqHandle(AtomicInteger count, Subscriber<?> s) {
659          count.getAndIncrement();
660          ((TestSubscriber)s).sn.request(Long.MAX_VALUE);
661          return true;
# Line 960 | Line 958 | public class SubmissionPublisherTest ext
958      public void testConsumeNPE() {
959          SubmissionPublisher<Integer> p = basicPublisher();
960          try {
961 <            CompletableFuture<Void> f = p.consume(null);
961 >            CompletableFuture<Void> unused = p.consume(null);
962              shouldThrow();
963          } catch (NullPointerException success) {}
964      }
# Line 979 | Line 977 | public class SubmissionPublisherTest ext
977          assertTrue(count.get() < n);
978      }
979  
980 +    /**
981 +     * Tests scenario for
982 +     * JDK-8187947: A race condition in SubmissionPublisher
983 +     * 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
984 +     */
985 +    public void testMissedSignal_8187947() throws Exception {
986 +        if (!atLeastJava9()) return; // backport to jdk8 too hard
987 +        final int N =
988 +            ((ForkJoinPool.getCommonPoolParallelism() < 2) // JDK-8212899
989 +             ? (1 << 5)
990 +             : (1 << 10))
991 +            * (expensiveTests ? (1 << 10) : 1);
992 +        final CountDownLatch finished = new CountDownLatch(1);
993 +        final SubmissionPublisher<Boolean> pub = new SubmissionPublisher<>();
994 +        class Sub implements Subscriber<Boolean> {
995 +            int received;
996 +            public void onSubscribe(Subscription s) {
997 +                s.request(N);
998 +            }
999 +            public void onNext(Boolean item) {
1000 +                if (++received == N)
1001 +                    finished.countDown();
1002 +                else
1003 +                    CompletableFuture.runAsync(() -> pub.submit(Boolean.TRUE));
1004 +            }
1005 +            public void onError(Throwable t) { throw new AssertionError(t); }
1006 +            public void onComplete() {}
1007 +        }
1008 +        pub.subscribe(new Sub());
1009 +        checkTimedGet(
1010 +            CompletableFuture.runAsync(() -> pub.submit(Boolean.TRUE)),
1011 +            null);
1012 +        await(finished);
1013 +    }
1014   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines