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.22 by jsr166, Sun Oct 18 18:54:49 2015 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 > import static java.util.concurrent.TimeUnit.SECONDS;
9 >
10 > import java.util.HashSet;
11   import java.util.concurrent.CancellationException;
12 + import java.util.concurrent.CountedCompleter;
13 + import java.util.concurrent.ExecutionException;
14   import java.util.concurrent.ForkJoinPool;
15   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;
16   import java.util.concurrent.TimeoutException;
17   import java.util.concurrent.atomic.AtomicInteger;
16 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
18   import java.util.concurrent.atomic.AtomicReference;
19 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
20 < import static java.util.concurrent.TimeUnit.SECONDS;
21 < import java.util.HashSet;
21 < import junit.framework.*;
19 >
20 > import junit.framework.Test;
21 > import junit.framework.TestSuite;
22  
23   public class CountedCompleterTest extends JSR166TestCase {
24  
25      public static void main(String[] args) {
26 <        junit.textui.TestRunner.run(suite());
26 >        main(suite(), args);
27      }
28  
29      public static Test suite() {
# Line 49 | Line 49 | public class CountedCompleterTest extend
49      }
50  
51      private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) {
52 <        try {
52 >        try (PoolCleaner cleaner = cleaner(pool)) {
53              assertFalse(a.isDone());
54              assertFalse(a.isCompletedNormally());
55              assertFalse(a.isCompletedAbnormally());
# Line 65 | Line 65 | public class CountedCompleterTest extend
65              assertFalse(a.isCancelled());
66              assertNull(a.getException());
67              assertNull(a.getRawResult());
68        } finally {
69            joinPool(pool);
68          }
69      }
70  
# Line 95 | Line 93 | public class CountedCompleterTest extend
93  
94          {
95              Thread.currentThread().interrupt();
96 <            long t0 = System.nanoTime();
96 >            long startTime = System.nanoTime();
97              assertNull(a.join());
98 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
98 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
99              Thread.interrupted();
100          }
101  
102          {
103              Thread.currentThread().interrupt();
104 <            long t0 = System.nanoTime();
104 >            long startTime = System.nanoTime();
105              a.quietlyJoin();        // should be no-op
106 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
106 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
107              Thread.interrupted();
108          }
109  
# Line 138 | Line 136 | public class CountedCompleterTest extend
136          Thread.interrupted();
137  
138          {
139 <            long t0 = System.nanoTime();
139 >            long startTime = System.nanoTime();
140              a.quietlyJoin();        // should be no-op
141 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
141 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
142          }
143  
144          try {
# Line 176 | Line 174 | public class CountedCompleterTest extend
174          Thread.interrupted();
175  
176          {
177 <            long t0 = System.nanoTime();
177 >            long startTime = System.nanoTime();
178              a.quietlyJoin();        // should be no-op
179 <            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
179 >            assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS);
180          }
181  
182          try {
# Line 198 | Line 196 | public class CountedCompleterTest extend
196          try {
197              a.invoke();
198              shouldThrow();
199 <        } catch (Throwable ex) {
200 <            assertSame(t, ex);
199 >        } catch (Throwable success) {
200 >            assertSame(t, success);
201          }
202      }
203  
# Line 212 | Line 210 | public class CountedCompleterTest extend
210          final AtomicInteger onCompletionN = new AtomicInteger(0);
211          final AtomicInteger onExceptionalCompletionN = new AtomicInteger(0);
212          final AtomicInteger setRawResultN = new AtomicInteger(0);
213 <        final AtomicReference<Object> rawResult = new AtomicReference<>(null);
213 >        final AtomicReference<Object> rawResult = new AtomicReference<Object>(null);
214          int computeN() { return computeN.get(); }
215          int onCompletionN() { return onCompletionN.get(); }
216          int onExceptionalCompletionN() { return onExceptionalCompletionN.get(); }
# Line 252 | Line 250 | public class CountedCompleterTest extend
250          }
251          void checkCompletes(Object rawResult) {
252              checkIncomplete();
253 +            int pendingCount = getPendingCount();
254              complete(rawResult);
255 +            assertEquals(pendingCount, getPendingCount());
256              assertEquals(0, computeN());
257              assertEquals(1, onCompletionN());
258              assertEquals(0, onExceptionalCompletionN());
# Line 278 | Line 278 | public class CountedCompleterTest extend
278      final class NoopCC extends CheckedCC {
279          NoopCC() { super(); }
280          NoopCC(CountedCompleter p) { super(p); }
281 +        NoopCC(CountedCompleter p, int initialPendingCount) {
282 +            super(p, initialPendingCount);
283 +        }
284          protected void realCompute() {}
285      }
286  
287      /**
288       * A newly constructed CountedCompleter is not completed;
289 <     * complete() causes completion.
289 >     * complete() causes completion. pendingCount is ignored.
290       */
291      public void testComplete() {
292          for (Object x : new Object[] { Boolean.TRUE, null }) {
293 <            new NoopCC()
294 <                .checkCompletes(x);
295 <            new NoopCC(new NoopCC())
296 <                .checkCompletes(x);
293 >            for (int pendingCount : new int[] { 0, 42 }) {
294 >                testComplete(new NoopCC(), x, pendingCount);
295 >                testComplete(new NoopCC(new NoopCC()), x, pendingCount);
296 >            }
297          }
298      }
299 +    void testComplete(NoopCC cc, Object x, int pendingCount) {
300 +        cc.setPendingCount(pendingCount);
301 +        cc.checkCompletes(x);
302 +        assertEquals(pendingCount, cc.getPendingCount());
303 +    }
304  
305      /**
306       * completeExceptionally completes exceptionally
# Line 305 | Line 313 | public class CountedCompleterTest extend
313      }
314  
315      /**
316 <     * completeExceptionally(null) throws NullPointerException
316 >     * completeExceptionally(null) surprisingly has the same effect as
317 >     * completeExceptionally(new RuntimeException())
318       */
319      public void testCompleteExceptionally_null() {
320 +        NoopCC a = new NoopCC();
321 +        a.completeExceptionally(null);
322          try {
323 <            new NoopCC()
313 <                .checkCompletesExceptionally(null);
323 >            a.invoke();
324              shouldThrow();
325 <        } catch (NullPointerException success) {}
325 >        } catch (RuntimeException success) {
326 >            assertSame(success.getClass(), RuntimeException.class);
327 >            assertNull(success.getCause());
328 >            a.checkCompletedExceptionally(success);
329 >        }
330      }
331  
332      /**
# Line 343 | Line 357 | public class CountedCompleterTest extend
357       * decrementPendingCountUnlessZero decrements reported pending
358       * count unless zero
359       */
360 <    public void testDecrementPendingCount() {
361 <        NoopCC a = new NoopCC();
362 <        assertEquals(0, a.getPendingCount());
363 <        a.addToPendingCount(1);
360 >    public void testDecrementPendingCountUnlessZero() {
361 >        NoopCC a = new NoopCC(null, 2);
362 >        assertEquals(2, a.getPendingCount());
363 >        assertEquals(2, a.decrementPendingCountUnlessZero());
364          assertEquals(1, a.getPendingCount());
365 <        a.decrementPendingCountUnlessZero();
365 >        assertEquals(1, a.decrementPendingCountUnlessZero());
366          assertEquals(0, a.getPendingCount());
367 <        a.decrementPendingCountUnlessZero();
367 >        assertEquals(0, a.decrementPendingCountUnlessZero());
368          assertEquals(0, a.getPendingCount());
369      }
370  
# Line 493 | Line 507 | public class CountedCompleterTest extend
507      // Invocation tests use some interdependent task classes
508      // to better test propagation etc
509  
510 <
511 <    // Version of Fibonacci with different classes for left vs right forks
510 >    /**
511 >     * Version of Fibonacci with different classes for left vs right forks
512 >     */
513      abstract class CCF extends CheckedCC {
514          int number;
515          int rnumber;
# Line 1130 | Line 1145 | public class CountedCompleterTest extend
1145      }
1146  
1147      /**
1148 <     * invokeAll(collection)  throws exception if any task does
1148 >     * invokeAll(collection) throws exception if any task does
1149       */
1150      public void testAbnormalInvokeAllCollection() {
1151          ForkJoinTask a = new CheckedRecursiveAction() {
# Line 1795 | Line 1810 | public class CountedCompleterTest extend
1810      }
1811  
1812      /**
1813 <     * invokeAll(collection)  throws exception if any task does
1813 >     * invokeAll(collection) throws exception if any task does
1814       */
1815      public void testAbnormalInvokeAllCollectionSingleton() {
1816          ForkJoinTask a = new CheckedRecursiveAction() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines