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

Comparing jsr166/src/test/tck/ExecutorCompletionServiceTest.java (file contents):
Revision 1.12 by jsr166, Wed Aug 25 01:44:48 2010 UTC vs.
Revision 1.20 by jsr166, Sun Feb 22 04:34:44 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9
10 import junit.framework.*;
11 import java.util.*;
12 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.util.concurrent.atomic.*;
11 < import java.math.BigInteger;
12 < import java.security.*;
10 >
11 > import java.util.concurrent.ArrayBlockingQueue;
12 > import java.util.concurrent.Callable;
13 > import java.util.concurrent.ExecutorCompletionService;
14 > import java.util.concurrent.Executors;
15 > import java.util.concurrent.ExecutorService;
16 > import java.util.concurrent.Future;
17 > import java.util.concurrent.FutureTask;
18 > import java.util.concurrent.RunnableFuture;
19 > import java.util.concurrent.ThreadPoolExecutor;
20 > import java.util.concurrent.TimeUnit;
21 > import java.util.concurrent.atomic.AtomicBoolean;
22 >
23 > import junit.framework.Test;
24 > import junit.framework.TestSuite;
25  
26   public class ExecutorCompletionServiceTest extends JSR166TestCase {
27      public static void main(String[] args) {
# Line 23 | Line 31 | public class ExecutorCompletionServiceTe
31          return new TestSuite(ExecutorCompletionServiceTest.class);
32      }
33  
26
34      /**
35       * Creating a new ECS with null Executor throw NPE
36       */
37      public void testConstructorNPE() {
38          try {
39 <            ExecutorCompletionService ecs = new ExecutorCompletionService(null);
39 >            new ExecutorCompletionService(null);
40              shouldThrow();
41          } catch (NullPointerException success) {}
42      }
# Line 40 | Line 47 | public class ExecutorCompletionServiceTe
47      public void testConstructorNPE2() {
48          try {
49              ExecutorService e = Executors.newCachedThreadPool();
50 <            ExecutorCompletionService ecs = new ExecutorCompletionService(e, null);
50 >            new ExecutorCompletionService(e, null);
51              shouldThrow();
52          } catch (NullPointerException success) {}
53      }
# Line 112 | Line 119 | public class ExecutorCompletionServiceTe
119      /**
120       * If poll returns non-null, the returned task is completed
121       */
122 <    public void testPoll1() throws InterruptedException {
122 >    public void testPoll1() throws Exception {
123          ExecutorService e = Executors.newCachedThreadPool();
124          ExecutorCompletionService ecs = new ExecutorCompletionService(e);
125          try {
126              assertNull(ecs.poll());
127              Callable c = new StringTask();
128              ecs.submit(c);
129 <            Thread.sleep(SHORT_DELAY_MS);
130 <            for (;;) {
131 <                Future f = ecs.poll();
132 <                if (f != null) {
133 <                    assertTrue(f.isDone());
134 <                    break;
135 <                }
129 >
130 >            long startTime = System.nanoTime();
131 >            Future f;
132 >            while ((f = ecs.poll()) == null) {
133 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
134 >                    fail("timed out");
135 >                Thread.yield();
136              }
137 +            assertTrue(f.isDone());
138 +            assertSame(TEST_STRING, f.get());
139          } finally {
140              joinPool(e);
141          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines