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.36 by jsr166, Wed Nov 8 02:26:14 2017 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  
# Line 114 | Line 110 | public class CountedCompleterTest extend
110          assertFalse(a.cancel(true));
111          try {
112              assertNull(a.get());
113 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
118 <        try {
119 <            assertNull(a.get(5L, SECONDS));
113 >            assertNull(a.get(randomTimeout(), randomTimeUnit()));
114          } catch (Throwable fail) { threadUnexpectedException(fail); }
115      }
116  
# Line 141 | Line 135 | public class CountedCompleterTest extend
135          {
136              long startTime = System.nanoTime();
137              a.quietlyJoin();        // should be no-op
138 <            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
138 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
139          }
140  
141          try {
# Line 151 | Line 145 | public class CountedCompleterTest extend
145          } catch (Throwable fail) { threadUnexpectedException(fail); }
146  
147          try {
148 <            a.get(5L, SECONDS);
148 >            a.get(randomTimeout(), randomTimeUnit());
149              shouldThrow();
150          } catch (CancellationException success) {
151          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 179 | Line 173 | public class CountedCompleterTest extend
173          {
174              long startTime = System.nanoTime();
175              a.quietlyJoin();        // should be no-op
176 <            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
176 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
177          }
178  
179          try {
# Line 190 | Line 184 | public class CountedCompleterTest extend
184          } catch (Throwable fail) { threadUnexpectedException(fail); }
185  
186          try {
187 <            a.get(5L, SECONDS);
187 >            a.get(randomTimeout(), randomTimeUnit());
188              shouldThrow();
189          } catch (ExecutionException success) {
190              assertSame(t.getClass(), success.getCause().getClass());
# Line 213 | Line 207 | public class CountedCompleterTest extend
207          final AtomicInteger onCompletionN = new AtomicInteger(0);
208          final AtomicInteger onExceptionalCompletionN = new AtomicInteger(0);
209          final AtomicInteger setRawResultN = new AtomicInteger(0);
210 <        final AtomicReference<Object> rawResult = new AtomicReference<Object>(null);
210 >        final AtomicReference<Object> rawResult = new AtomicReference<>(null);
211          int computeN() { return computeN.get(); }
212          int onCompletionN() { return onCompletionN.get(); }
213          int onExceptionalCompletionN() { return onExceptionalCompletionN.get(); }
# Line 705 | Line 699 | public class CountedCompleterTest extend
699                  CCF f = new LCCF(8);
700                  assertSame(f, f.fork());
701                  try {
702 <                    f.get(5L, null);
702 >                    f.get(randomTimeout(), null);
703                      shouldThrow();
704                  } catch (NullPointerException success) {}
705              }};
# Line 737 | Line 731 | public class CountedCompleterTest extend
731                  CCF f = new LCCF(8);
732                  assertSame(f, f.fork());
733                  helpQuiesce();
734 +                while (!f.isDone()) // wait out race
735 +                    ;
736                  assertEquals(21, f.number);
737                  assertEquals(0, getQueuedTaskCount());
738                  checkCompletedNormally(f);
# Line 1427 | Line 1423 | public class CountedCompleterTest extend
1423                  CCF f = new LCCF(8);
1424                  assertSame(f, f.fork());
1425                  try {
1426 <                    f.get(5L, null);
1426 >                    f.get(randomTimeout(), null);
1427                      shouldThrow();
1428                  } catch (NullPointerException success) {}
1429              }};
# Line 1845 | Line 1841 | public class CountedCompleterTest extend
1841          testInvokeOnPool(singletonPool(), a);
1842      }
1843  
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
1844   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines