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.6 by jsr166, Mon Sep 7 20:53:10 2015 UTC vs.
Revision 1.16 by dl, Sun Dec 11 22:11:45 2016 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 < import static java.util.concurrent.TimeUnit.SECONDS;
10 <
8 > import java.util.concurrent.CompletableFuture;
9   import java.util.concurrent.Executor;
10   import java.util.concurrent.Executors;
11   import java.util.concurrent.Flow;
14 import static java.util.concurrent.Flow.Publisher;
15 import static java.util.concurrent.Flow.Subscriber;
16 import static java.util.concurrent.Flow.Subscription;
17 import java.util.concurrent.LinkedBlockingQueue;
12   import java.util.concurrent.ForkJoinPool;
13   import java.util.concurrent.SubmissionPublisher;
20 import java.util.concurrent.ThreadFactory;
21 import java.util.concurrent.ThreadPoolExecutor;
22 import java.util.concurrent.TimeUnit;
14   import java.util.concurrent.atomic.AtomicInteger;
24 import java.util.function.BiConsumer;
25 import java.util.function.BiPredicate;
26 import java.util.function.BiFunction;
27
15   import junit.framework.Test;
16   import junit.framework.TestSuite;
17  
18 + 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   public class SubmissionPublisherTest extends JSR166TestCase {
23  
24      public static void main(String[] args) {
# Line 37 | Line 28 | public class SubmissionPublisherTest ext
28          return new TestSuite(SubmissionPublisherTest.class);
29      }
30  
31 <    // Factory for single thread pool in case commonPool parallelism is zero
41 <    static final class DaemonThreadFactory implements ThreadFactory {
42 <        public Thread newThread(Runnable r) {
43 <            Thread t = new Thread(r);
44 <            t.setDaemon(true);
45 <            return t;
46 <        }
47 <    }
48 <
49 <    static final Executor basicExecutor =
50 <        (ForkJoinPool.getCommonPoolParallelism() > 0) ?
51 <        ForkJoinPool.commonPool() :
52 <        new ThreadPoolExecutor(1, 1, 60, SECONDS,
53 <                               new LinkedBlockingQueue<Runnable>(),
54 <                               new DaemonThreadFactory());
31 >    final Executor basicExecutor = basicPublisher().getExecutor();
32  
33      static SubmissionPublisher<Integer> basicPublisher() {
34 <        return new SubmissionPublisher<Integer>(basicExecutor,
58 <                                                Flow.defaultBufferSize());
34 >        return new SubmissionPublisher<Integer>();
35      }
36  
37      static class SPException extends RuntimeException {}
# Line 167 | Line 143 | public class SubmissionPublisherTest ext
143      /**
144       * A default-constructed SubmissionPublisher has no subscribers,
145       * is not closed, has default buffer size, and uses the
146 <     * ForkJoinPool.commonPool executor
146 >     * defaultExecutor
147       */
148      public void testConstructor1() {
149          SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>();
150          checkInitialState(p);
175        assertSame(p.getExecutor(), ForkJoinPool.commonPool());
151          assertEquals(p.getMaxBufferCapacity(), Flow.defaultBufferSize());
152 +        Executor e = p.getExecutor(), c = ForkJoinPool.commonPool();
153 +        if (ForkJoinPool.getCommonPoolParallelism() > 1)
154 +            assertSame(e, c);
155 +        else
156 +            assertNotSame(e, c);
157      }
158  
159      /**
# Line 285 | Line 265 | public class SubmissionPublisherTest ext
265          assertEquals(0, s2.nexts);
266          assertEquals(0, s2.errors);
267          assertEquals(0, s2.completes);
268 +        p.close();
269      }
270  
271      /**
# Line 360 | Line 341 | public class SubmissionPublisherTest ext
341      }
342  
343      /**
344 <     * subscribe(null) thows NPE
344 >     * subscribe(null) throws NPE
345       */
346      public void testSubscribe6() {
347          SubmissionPublisher<Integer> p = basicPublisher();
# Line 394 | Line 375 | public class SubmissionPublisherTest ext
375  
376      /**
377       * Closing a publisher exceptionally causes onError to subscribers
378 +     * after they are subscribed
379       */
380      public void testCloseExceptionallyError() {
381          SubmissionPublisher<Integer> p = basicPublisher();
# Line 404 | Line 386 | public class SubmissionPublisherTest ext
386          p.submit(1);
387          p.closeExceptionally(new SPException());
388          assertTrue(p.isClosed());
389 +        s1.awaitSubscribe();
390          s1.awaitError();
391          assertTrue(s1.nexts <= 1);
392          assertEquals(1, s1.errors);
393 +        s1.awaitSubscribe();
394          s2.awaitError();
395          assertTrue(s2.nexts <= 1);
396          assertEquals(1, s2.errors);
# Line 455 | Line 439 | public class SubmissionPublisherTest ext
439      }
440  
441      /**
442 <     * If a handler is supplied in conctructor, it is invoked when
442 >     * If a handler is supplied in constructor, it is invoked when
443       * subscriber throws an exception in onNext
444       */
445      public void testThrowOnNextHandler() {
# Line 806 | Line 790 | public class SubmissionPublisherTest ext
790       */
791      public void testEmptyTimedOffer() {
792          SubmissionPublisher<Integer> p = basicPublisher();
793 +        long startTime = System.nanoTime();
794          assertEquals(0, p.offer(1, LONG_DELAY_MS, MILLISECONDS, null));
795 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
796      }
797  
798      /**
# Line 814 | Line 800 | public class SubmissionPublisherTest ext
800       */
801      public void testNullTimedOffer() {
802          SubmissionPublisher<Integer> p = basicPublisher();
803 +        long startTime = System.nanoTime();
804          try {
805 <            p.offer(null, SHORT_DELAY_MS, MILLISECONDS, null);
805 >            p.offer(null, LONG_DELAY_MS, MILLISECONDS, null);
806              shouldThrow();
807          } catch (NullPointerException success) {}
808          try {
809 <            p.offer(1, SHORT_DELAY_MS, null, null);
809 >            p.offer(1, LONG_DELAY_MS, null, null);
810              shouldThrow();
811          } catch (NullPointerException success) {}
812 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
813      }
814  
815      /**
# Line 837 | Line 825 | public class SubmissionPublisherTest ext
825          p.subscribe(s2);
826          s2.awaitSubscribe();
827          s1.awaitSubscribe();
828 <        assertTrue(p.offer(1, SHORT_DELAY_MS, MILLISECONDS, null) >= 1);
829 <        assertTrue(p.offer(2, SHORT_DELAY_MS, MILLISECONDS, null) >= 2);
828 >        long startTime = System.nanoTime();
829 >        assertTrue(p.offer(1, LONG_DELAY_MS, MILLISECONDS, null) >= 1);
830 >        assertTrue(p.offer(2, LONG_DELAY_MS, MILLISECONDS, null) >= 2);
831          s1.sn.request(4);
832 <        assertTrue(p.offer(3, SHORT_DELAY_MS, MILLISECONDS, null) >= 3);
832 >        assertTrue(p.offer(3, LONG_DELAY_MS, MILLISECONDS, null) >= 3);
833          s2.sn.request(4);
834 <        p.offer(4, SHORT_DELAY_MS, MILLISECONDS, null);
834 >        p.offer(4, LONG_DELAY_MS, MILLISECONDS, null);
835          p.close();
836          s2.awaitComplete();
837          assertEquals(4, s2.nexts);
838          s1.awaitComplete();
839          assertEquals(4, s2.nexts);
840 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
841      }
842  
843      /**
# Line 864 | Line 854 | public class SubmissionPublisherTest ext
854          p.subscribe(s2);
855          s2.awaitSubscribe();
856          s1.awaitSubscribe();
857 +        long delay = timeoutMillis();
858          for (int i = 1; i <= 4; ++i)
859 <            assertTrue(p.offer(i, SHORT_DELAY_MS, MILLISECONDS, null) >= 0);
860 <        p.offer(5, SHORT_DELAY_MS, MILLISECONDS, null);
861 <        assertTrue(p.offer(6, SHORT_DELAY_MS, MILLISECONDS, null) < 0);
859 >            assertTrue(p.offer(i, delay, MILLISECONDS, null) >= 0);
860 >        long startTime = System.nanoTime();
861 >        assertTrue(p.offer(5, delay, MILLISECONDS, null) < 0);
862          s1.sn.request(64);
863 <        assertTrue(p.offer(7, SHORT_DELAY_MS, MILLISECONDS, null) < 0);
863 >        assertTrue(p.offer(6, delay, MILLISECONDS, null) < 0);
864 >        // 2 * delay should elapse but check only 1 * delay to allow timer slop
865 >        assertTrue(millisElapsedSince(startTime) >= delay);
866          s2.sn.request(64);
867          p.close();
868          s2.awaitComplete();
# Line 893 | Line 886 | public class SubmissionPublisherTest ext
886          p.subscribe(s2);
887          s2.awaitSubscribe();
888          s1.awaitSubscribe();
889 +        long delay = timeoutMillis();
890          for (int i = 1; i <= 4; ++i)
891 <            assertTrue(p.offer(i, SHORT_DELAY_MS, MILLISECONDS, (s, x) -> noopHandle(calls)) >= 0);
892 <        p.offer(5, (s, x) -> noopHandle(calls));
893 <        assertTrue(p.offer(6, SHORT_DELAY_MS, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0);
891 >            assertTrue(p.offer(i, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) >= 0);
892 >        long startTime = System.nanoTime();
893 >        assertTrue(p.offer(5, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0);
894          s1.sn.request(64);
895 <        assertTrue(p.offer(7, SHORT_DELAY_MS, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0);
895 >        assertTrue(p.offer(6, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0);
896 >        assertTrue(millisElapsedSince(startTime) >= delay);
897          s2.sn.request(64);
898          p.close();
899          s2.awaitComplete();
# Line 922 | Line 917 | public class SubmissionPublisherTest ext
917          s2.awaitSubscribe();
918          s1.awaitSubscribe();
919          int n = 0;
920 <        for (int i = 1; i <= 8; ++i) {
921 <            int d = p.offer(i, SHORT_DELAY_MS, MILLISECONDS, (s, x) -> reqHandle(calls, s));
920 >        long delay = timeoutMillis();
921 >        long startTime = System.nanoTime();
922 >        for (int i = 1; i <= 6; ++i) {
923 >            int d = p.offer(i, delay, MILLISECONDS, (s, x) -> reqHandle(calls, s));
924              n = n + 2 + (d < 0 ? d : 0);
925          }
926 +        assertTrue(millisElapsedSince(startTime) >= delay);
927          p.close();
928          s2.awaitComplete();
929          s1.awaitComplete();
# Line 933 | Line 931 | public class SubmissionPublisherTest ext
931          assertTrue(calls.get() >= 2);
932      }
933  
934 +    /**
935 +     * consume returns a CompletableFuture that is done when
936 +     * publisher completes
937 +     */
938 +    public void testConsume() {
939 +        AtomicInteger sum = new AtomicInteger();
940 +        SubmissionPublisher<Integer> p = basicPublisher();
941 +        CompletableFuture<Void> f =
942 +            p.consume((Integer x) -> sum.getAndAdd(x.intValue()));
943 +        int n = 20;
944 +        for (int i = 1; i <= n; ++i)
945 +            p.submit(i);
946 +        p.close();
947 +        f.join();
948 +        assertEquals((n * (n + 1)) / 2, sum.get());
949 +    }
950 +
951 +    /**
952 +     * consume(null) throws NPE
953 +     */
954 +    public void testConsumeNPE() {
955 +        SubmissionPublisher<Integer> p = basicPublisher();
956 +        try {
957 +            CompletableFuture<Void> f = p.consume(null);
958 +            shouldThrow();
959 +        } catch (NullPointerException success) {}
960 +    }
961 +
962 +    /**
963 +     * consume eventually stops processing published items if cancelled
964 +     */
965 +    public void testCancelledConsume() {
966 +        AtomicInteger count = new AtomicInteger();
967 +        SubmissionPublisher<Integer> p = basicPublisher();
968 +        CompletableFuture<Void> f = p.consume(x -> count.getAndIncrement());
969 +        f.cancel(true);
970 +        int n = 1000000; // arbitrary limit
971 +        for (int i = 1; i <= n; ++i)
972 +            p.submit(i);
973 +        assertTrue(count.get() < n);
974 +    }
975 +
976   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines