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

Comparing jsr166/src/test/tck/CountedCompleterTest.java (file contents):
Revision 1.29 by jsr166, Mon Sep 12 17:49:12 2016 UTC vs.
Revision 1.37 by jsr166, Sun Jul 22 20:23:28 2018 UTC

# Line 5 | Line 5
5   */
6  
7   import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 import static java.util.concurrent.TimeUnit.SECONDS;
8  
9   import java.util.HashSet;
10   import java.util.concurrent.CancellationException;
# Line 13 | Line 12 | import java.util.concurrent.CountedCompl
12   import java.util.concurrent.ExecutionException;
13   import java.util.concurrent.ForkJoinPool;
14   import java.util.concurrent.ForkJoinTask;
16 import java.util.concurrent.ThreadLocalRandom;
15   import java.util.concurrent.TimeoutException;
16   import java.util.concurrent.atomic.AtomicInteger;
17   import java.util.concurrent.atomic.AtomicReference;
20 import java.util.function.BiConsumer;
21 import java.util.function.Consumer;
18  
19   import junit.framework.Test;
20   import junit.framework.TestSuite;
# Line 80 | Line 76 | public class CountedCompleterTest extend
76          assertNull(a.getRawResult());
77  
78          try {
79 <            a.get(0L, SECONDS);
79 >            a.get(randomExpiredTimeout(), randomTimeUnit());
80              shouldThrow();
81          } catch (TimeoutException success) {
82          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 98 | Line 94 | public class CountedCompleterTest extend
94              Thread.currentThread().interrupt();
95              long startTime = System.nanoTime();
96              assertNull(a.join());
97 <            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
97 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
98              Thread.interrupted();
99          }
100  
# Line 106 | Line 102 | public class CountedCompleterTest extend
102              Thread.currentThread().interrupt();
103              long startTime = System.nanoTime();
104              a.quietlyJoin();        // should be no-op
105 <            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
105 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
106              Thread.interrupted();
107          }
108  
109          assertFalse(a.cancel(false));
110          assertFalse(a.cancel(true));
111 +
112 +        Object v1 = null, v2 = null;
113          try {
114 <            assertNull(a.get());
115 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
118 <        try {
119 <            assertNull(a.get(5L, SECONDS));
114 >            v1 = a.get();
115 >            v2 = a.get(randomTimeout(), randomTimeUnit());
116          } catch (Throwable fail) { threadUnexpectedException(fail); }
117 +        assertNull(v1);
118 +        assertNull(v2);
119      }
120  
121      void checkCancelled(CountedCompleter a) {
# Line 141 | Line 139 | public class CountedCompleterTest extend
139          {
140              long startTime = System.nanoTime();
141              a.quietlyJoin();        // should be no-op
142 <            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
142 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
143          }
144  
145          try {
# Line 151 | Line 149 | public class CountedCompleterTest extend
149          } catch (Throwable fail) { threadUnexpectedException(fail); }
150  
151          try {
152 <            a.get(5L, SECONDS);
152 >            a.get(randomTimeout(), randomTimeUnit());
153              shouldThrow();
154          } catch (CancellationException success) {
155          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 179 | Line 177 | public class CountedCompleterTest extend
177          {
178              long startTime = System.nanoTime();
179              a.quietlyJoin();        // should be no-op
180 <            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
180 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
181          }
182  
183          try {
# Line 190 | Line 188 | public class CountedCompleterTest extend
188          } catch (Throwable fail) { threadUnexpectedException(fail); }
189  
190          try {
191 <            a.get(5L, SECONDS);
191 >            a.get(randomTimeout(), randomTimeUnit());
192              shouldThrow();
193          } catch (ExecutionException success) {
194              assertSame(t.getClass(), success.getCause().getClass());
# Line 213 | Line 211 | public class CountedCompleterTest extend
211          final AtomicInteger onCompletionN = new AtomicInteger(0);
212          final AtomicInteger onExceptionalCompletionN = new AtomicInteger(0);
213          final AtomicInteger setRawResultN = new AtomicInteger(0);
214 <        final AtomicReference<Object> rawResult = new AtomicReference<Object>(null);
214 >        final AtomicReference<Object> rawResult = new AtomicReference<>(null);
215          int computeN() { return computeN.get(); }
216          int onCompletionN() { return onCompletionN.get(); }
217          int onExceptionalCompletionN() { return onExceptionalCompletionN.get(); }
# Line 705 | Line 703 | public class CountedCompleterTest extend
703                  CCF f = new LCCF(8);
704                  assertSame(f, f.fork());
705                  try {
706 <                    f.get(5L, null);
706 >                    f.get(randomTimeout(), null);
707                      shouldThrow();
708                  } catch (NullPointerException success) {}
709              }};
# Line 737 | Line 735 | public class CountedCompleterTest extend
735                  CCF f = new LCCF(8);
736                  assertSame(f, f.fork());
737                  helpQuiesce();
738 +                while (!f.isDone()) // wait out race
739 +                    ;
740                  assertEquals(21, f.number);
741                  assertEquals(0, getQueuedTaskCount());
742                  checkCompletedNormally(f);
# Line 1427 | Line 1427 | public class CountedCompleterTest extend
1427                  CCF f = new LCCF(8);
1428                  assertSame(f, f.fork());
1429                  try {
1430 <                    f.get(5L, null);
1430 >                    f.get(randomTimeout(), null);
1431                      shouldThrow();
1432                  } catch (NullPointerException success) {}
1433              }};
# Line 1845 | Line 1845 | public class CountedCompleterTest extend
1845          testInvokeOnPool(singletonPool(), a);
1846      }
1847  
1848    /** CountedCompleter class javadoc code sample, version 1. */
1849    public static <E> void forEach1(E[] array, Consumer<E> action) {
1850        class Task extends CountedCompleter<Void> {
1851            final int lo, hi;
1852            Task(Task parent, int lo, int hi) {
1853                super(parent); this.lo = lo; this.hi = hi;
1854            }
1855
1856            public void compute() {
1857                if (hi - lo >= 2) {
1858                    int mid = (lo + hi) >>> 1;
1859                    // must set pending count before fork
1860                    setPendingCount(2);
1861                    new Task(this, mid, hi).fork(); // right child
1862                    new Task(this, lo, mid).fork(); // left child
1863                }
1864                else if (hi > lo)
1865                    action.accept(array[lo]);
1866                tryComplete();
1867            }
1868        }
1869        new Task(null, 0, array.length).invoke();
1870    }
1871
1872    /** CountedCompleter class javadoc code sample, version 2. */
1873    public static <E> void forEach2(E[] array, Consumer<E> action) {
1874        class Task extends CountedCompleter<Void> {
1875            final int lo, hi;
1876            Task(Task parent, int lo, int hi) {
1877                super(parent); this.lo = lo; this.hi = hi;
1878            }
1879
1880            public void compute() {
1881                if (hi - lo >= 2) {
1882                    int mid = (lo + hi) >>> 1;
1883                    setPendingCount(1); // looks off by one, but correct!
1884                    new Task(this, mid, hi).fork(); // right child
1885                    new Task(this, lo, mid).compute(); // direct invoke
1886                } else {
1887                    if (hi > lo)
1888                        action.accept(array[lo]);
1889                    tryComplete();
1890                }
1891            }
1892        }
1893        new Task(null, 0, array.length).invoke();
1894    }
1895
1896    /** CountedCompleter class javadoc code sample, version 3. */
1897    public static <E> void forEach3(E[] array, Consumer<E> action) {
1898        class Task extends CountedCompleter<Void> {
1899            final int lo, hi;
1900            Task(Task parent, int lo, int hi) {
1901                super(parent); this.lo = lo; this.hi = hi;
1902            }
1903
1904            public void compute() {
1905                int n = hi - lo;
1906                for (; n >= 2; n /= 2) {
1907                    addToPendingCount(1);
1908                    new Task(this, lo + n/2, lo + n).fork();
1909                }
1910                if (n > 0)
1911                    action.accept(array[lo]);
1912                propagateCompletion();
1913            }
1914        }
1915        new Task(null, 0, array.length).invoke();
1916    }
1917
1918    /** CountedCompleter class javadoc code sample, version 4. */
1919    public static <E> void forEach4(E[] array, Consumer<E> action) {
1920        class Task extends CountedCompleter<Void> {
1921            final int lo, hi;
1922            Task(Task parent, int lo, int hi) {
1923                super(parent, 31 - Integer.numberOfLeadingZeros(hi - lo));
1924                this.lo = lo; this.hi = hi;
1925            }
1926
1927            public void compute() {
1928                for (int n = hi - lo; n >= 2; n /= 2)
1929                    new Task(this, lo + n/2, lo + n).fork();
1930                action.accept(array[lo]);
1931                propagateCompletion();
1932            }
1933        }
1934        if (array.length > 0)
1935            new Task(null, 0, array.length).invoke();
1936    }
1937
1938    void testRecursiveDecomposition(
1939        BiConsumer<Integer[], Consumer<Integer>> action) {
1940        int n = ThreadLocalRandom.current().nextInt(8);
1941        Integer[] a = new Integer[n];
1942        for (int i = 0; i < n; i++) a[i] = i + 1;
1943        AtomicInteger ai = new AtomicInteger(0);
1944        action.accept(a, (x) -> ai.addAndGet(x));
1945        assertEquals(n * (n + 1) / 2, ai.get());
1946    }
1947
1948    /**
1949     * Variants of divide-by-two recursive decomposition into leaf tasks,
1950     * as described in the CountedCompleter class javadoc code samples
1951     */
1952    public void testRecursiveDecomposition() {
1953        testRecursiveDecomposition(CountedCompleterTest::forEach1);
1954        testRecursiveDecomposition(CountedCompleterTest::forEach2);
1955        testRecursiveDecomposition(CountedCompleterTest::forEach3);
1956        testRecursiveDecomposition(CountedCompleterTest::forEach4);
1957    }
1958
1848   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines