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.9 by jsr166, Wed Jun 5 05:48:26 2013 UTC vs.
Revision 1.37 by jsr166, Sun Jul 22 20:23:28 2018 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6 < import java.util.concurrent.ExecutionException;
6 >
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 >
9 > import java.util.HashSet;
10   import java.util.concurrent.CancellationException;
11 + import java.util.concurrent.CountedCompleter;
12 + import java.util.concurrent.ExecutionException;
13   import java.util.concurrent.ForkJoinPool;
14   import java.util.concurrent.ForkJoinTask;
10 import java.util.concurrent.CountedCompleter;
11 import java.util.concurrent.ForkJoinWorkerThread;
12 import java.util.concurrent.RecursiveAction;
13 import java.util.concurrent.TimeUnit;
15   import java.util.concurrent.TimeoutException;
16   import java.util.concurrent.atomic.AtomicInteger;
16 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
17   import java.util.concurrent.atomic.AtomicReference;
18 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
19 < import static java.util.concurrent.TimeUnit.SECONDS;
20 < import java.util.HashSet;
21 < import junit.framework.*;
18 >
19 > import junit.framework.Test;
20 > import junit.framework.TestSuite;
21  
22   public class CountedCompleterTest extends JSR166TestCase {
23  
24      public static void main(String[] args) {
25 <        junit.textui.TestRunner.run(suite());
25 >        main(suite(), args);
26      }
27  
28      public static Test suite() {
# Line 49 | Line 48 | public class CountedCompleterTest extend
48      }
49  
50      private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) {
51 <        try {
51 >        try (PoolCleaner cleaner = cleaner(pool)) {
52              assertFalse(a.isDone());
53              assertFalse(a.isCompletedNormally());
54              assertFalse(a.isCompletedAbnormally());
# Line 65 | Line 64 | public class CountedCompleterTest extend
64              assertFalse(a.isCancelled());
65              assertNull(a.getException());
66              assertNull(a.getRawResult());
68        } finally {
69            joinPool(pool);
67          }
68      }
69  
# Line 79 | 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 95 | Line 92 | public class CountedCompleterTest extend
92  
93          {
94              Thread.currentThread().interrupt();
95 <            long t0 = System.nanoTime();
95 >            long startTime = System.nanoTime();
96              assertNull(a.join());
97 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
97 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
98              Thread.interrupted();
99          }
100  
101          {
102              Thread.currentThread().interrupt();
103 <            long t0 = System.nanoTime();
103 >            long startTime = System.nanoTime();
104              a.quietlyJoin();        // should be no-op
105 <            assertTrue(millisElapsedSince(t0) < 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); }
117 <        try {
118 <            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 138 | Line 137 | public class CountedCompleterTest extend
137          Thread.interrupted();
138  
139          {
140 <            long t0 = System.nanoTime();
140 >            long startTime = System.nanoTime();
141              a.quietlyJoin();        // should be no-op
142 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
142 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
143          }
144  
145          try {
# Line 150 | 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 176 | Line 175 | public class CountedCompleterTest extend
175          Thread.interrupted();
176  
177          {
178 <            long t0 = System.nanoTime();
178 >            long startTime = System.nanoTime();
179              a.quietlyJoin();        // should be no-op
180 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
180 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
181          }
182  
183          try {
# Line 189 | 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 198 | Line 197 | public class CountedCompleterTest extend
197          try {
198              a.invoke();
199              shouldThrow();
200 <        } catch (Throwable ex) {
201 <            assertSame(t, ex);
200 >        } catch (Throwable success) {
201 >            assertSame(t, success);
202          }
203      }
204  
# Line 252 | Line 251 | public class CountedCompleterTest extend
251          }
252          void checkCompletes(Object rawResult) {
253              checkIncomplete();
254 +            int pendingCount = getPendingCount();
255              complete(rawResult);
256 +            assertEquals(pendingCount, getPendingCount());
257              assertEquals(0, computeN());
258              assertEquals(1, onCompletionN());
259              assertEquals(0, onExceptionalCompletionN());
# Line 278 | Line 279 | public class CountedCompleterTest extend
279      final class NoopCC extends CheckedCC {
280          NoopCC() { super(); }
281          NoopCC(CountedCompleter p) { super(p); }
282 +        NoopCC(CountedCompleter p, int initialPendingCount) {
283 +            super(p, initialPendingCount);
284 +        }
285          protected void realCompute() {}
286      }
287  
288      /**
289       * A newly constructed CountedCompleter is not completed;
290 <     * complete() causes completion.
290 >     * complete() causes completion. pendingCount is ignored.
291       */
292      public void testComplete() {
293          for (Object x : new Object[] { Boolean.TRUE, null }) {
294 <            new NoopCC()
295 <                .checkCompletes(x);
296 <            new NoopCC(new NoopCC())
297 <                .checkCompletes(x);
294 >            for (int pendingCount : new int[] { 0, 42 }) {
295 >                testComplete(new NoopCC(), x, pendingCount);
296 >                testComplete(new NoopCC(new NoopCC()), x, pendingCount);
297 >            }
298          }
299      }
300 +    void testComplete(NoopCC cc, Object x, int pendingCount) {
301 +        cc.setPendingCount(pendingCount);
302 +        cc.checkCompletes(x);
303 +        assertEquals(pendingCount, cc.getPendingCount());
304 +    }
305  
306      /**
307       * completeExceptionally completes exceptionally
# Line 305 | Line 314 | public class CountedCompleterTest extend
314      }
315  
316      /**
317 <     * completeExceptionally(null) throws NullPointerException
317 >     * completeExceptionally(null) surprisingly has the same effect as
318 >     * completeExceptionally(new RuntimeException())
319       */
320      public void testCompleteExceptionally_null() {
321 +        NoopCC a = new NoopCC();
322 +        a.completeExceptionally(null);
323          try {
324 <            new NoopCC()
313 <                .checkCompletesExceptionally(null);
324 >            a.invoke();
325              shouldThrow();
326 <        } catch (NullPointerException success) {}
326 >        } catch (RuntimeException success) {
327 >            assertSame(success.getClass(), RuntimeException.class);
328 >            assertNull(success.getCause());
329 >            a.checkCompletedExceptionally(success);
330 >        }
331      }
332  
333      /**
# Line 321 | Line 336 | public class CountedCompleterTest extend
336      public void testSetPendingCount() {
337          NoopCC a = new NoopCC();
338          assertEquals(0, a.getPendingCount());
339 <        a.setPendingCount(1);
340 <        assertEquals(1, a.getPendingCount());
341 <        a.setPendingCount(27);
342 <        assertEquals(27, a.getPendingCount());
339 >        int[] vals = {
340 >             -1, 0, 1,
341 >             Integer.MIN_VALUE,
342 >             Integer.MAX_VALUE,
343 >        };
344 >        for (int val : vals) {
345 >            a.setPendingCount(val);
346 >            assertEquals(val, a.getPendingCount());
347 >        }
348      }
349  
350      /**
# Line 337 | Line 357 | public class CountedCompleterTest extend
357          assertEquals(1, a.getPendingCount());
358          a.addToPendingCount(27);
359          assertEquals(28, a.getPendingCount());
360 +        a.addToPendingCount(-28);
361 +        assertEquals(0, a.getPendingCount());
362      }
363  
364      /**
365       * decrementPendingCountUnlessZero decrements reported pending
366       * count unless zero
367       */
368 <    public void testDecrementPendingCount() {
369 <        NoopCC a = new NoopCC();
370 <        assertEquals(0, a.getPendingCount());
371 <        a.addToPendingCount(1);
368 >    public void testDecrementPendingCountUnlessZero() {
369 >        NoopCC a = new NoopCC(null, 2);
370 >        assertEquals(2, a.getPendingCount());
371 >        assertEquals(2, a.decrementPendingCountUnlessZero());
372          assertEquals(1, a.getPendingCount());
373 <        a.decrementPendingCountUnlessZero();
373 >        assertEquals(1, a.decrementPendingCountUnlessZero());
374          assertEquals(0, a.getPendingCount());
375 <        a.decrementPendingCountUnlessZero();
375 >        assertEquals(0, a.decrementPendingCountUnlessZero());
376          assertEquals(0, a.getPendingCount());
377 +        a.setPendingCount(-1);
378 +        assertEquals(-1, a.decrementPendingCountUnlessZero());
379 +        assertEquals(-2, a.getPendingCount());
380      }
381  
382      /**
# Line 475 | Line 500 | public class CountedCompleterTest extend
500      }
501  
502      /**
503 <     * quietlyCompleteRoot completes root task
503 >     * quietlyCompleteRoot completes root task and only root task
504       */
505      public void testQuietlyCompleteRoot() {
506          NoopCC a = new NoopCC();
# Line 493 | Line 518 | public class CountedCompleterTest extend
518      // Invocation tests use some interdependent task classes
519      // to better test propagation etc
520  
521 <
522 <    // Version of Fibonacci with different classes for left vs right forks
521 >    /**
522 >     * Version of Fibonacci with different classes for left vs right forks
523 >     */
524      abstract class CCF extends CheckedCC {
525          int number;
526          int rnumber;
# Line 677 | 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 709 | 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 1130 | Line 1158 | public class CountedCompleterTest extend
1158      }
1159  
1160      /**
1161 <     * invokeAll(collection)  throws exception if any task does
1161 >     * invokeAll(collection) throws exception if any task does
1162       */
1163      public void testAbnormalInvokeAllCollection() {
1164          ForkJoinTask a = new CheckedRecursiveAction() {
# Line 1399 | 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 1795 | Line 1823 | public class CountedCompleterTest extend
1823      }
1824  
1825      /**
1826 <     * invokeAll(collection)  throws exception if any task does
1826 >     * invokeAll(collection) throws exception if any task does
1827       */
1828      public void testAbnormalInvokeAllCollectionSingleton() {
1829          ForkJoinTask a = new CheckedRecursiveAction() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines