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.25 by jsr166, Mon May 23 18:19:48 2016 UTC vs.
Revision 1.30 by dl, Tue Jan 26 13:33:06 2021 UTC

# Line 13 | Line 13 | import java.util.concurrent.Callable;
13   import java.util.concurrent.CompletionService;
14   import java.util.concurrent.CountDownLatch;
15   import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Executor;
16   import java.util.concurrent.ExecutorCompletionService;
17   import java.util.concurrent.ExecutorService;
18   import java.util.concurrent.Future;
# Line 39 | Line 38 | public class ExecutorCompletionServiceTe
38       */
39      public void testConstructorNPE() {
40          try {
41 <            new ExecutorCompletionService(null);
41 >            new ExecutorCompletionService<Item>(null);
42              shouldThrow();
43          } catch (NullPointerException success) {}
44      }
# Line 49 | Line 48 | public class ExecutorCompletionServiceTe
48       */
49      public void testConstructorNPE2() {
50          try {
51 <            new ExecutorCompletionService(cachedThreadPool, null);
51 >            new ExecutorCompletionService<Item>(cachedThreadPool, null);
52              shouldThrow();
53          } catch (NullPointerException success) {}
54      }
# Line 58 | Line 57 | public class ExecutorCompletionServiceTe
57       * ecs.submit(null) throws NullPointerException
58       */
59      public void testSubmitNullCallable() {
60 <        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
60 >        CompletionService<Item> cs = new ExecutorCompletionService<Item>(cachedThreadPool);
61          try {
62 <            cs.submit((Callable) null);
62 >            cs.submit((Callable<Item>) null);
63              shouldThrow();
64          } catch (NullPointerException success) {}
65      }
# Line 69 | Line 68 | public class ExecutorCompletionServiceTe
68       * ecs.submit(null, val) throws NullPointerException
69       */
70      public void testSubmitNullRunnable() {
71 <        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
71 >        CompletionService<Boolean> cs = new ExecutorCompletionService<Boolean>(cachedThreadPool);
72          try {
73              cs.submit((Runnable) null, Boolean.TRUE);
74              shouldThrow();
# Line 79 | Line 78 | public class ExecutorCompletionServiceTe
78      /**
79       * A taken submitted task is completed
80       */
81 <    public void testTake()
82 <        throws InterruptedException, ExecutionException {
84 <        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
81 >    public void testTake() throws Exception {
82 >        CompletionService<String> cs = new ExecutorCompletionService<String>(cachedThreadPool);
83          cs.submit(new StringTask());
84 <        Future f = cs.take();
84 >        Future<?> f = cs.take();
85          assertTrue(f.isDone());
86          assertSame(TEST_STRING, f.get());
87      }
# Line 92 | Line 90 | public class ExecutorCompletionServiceTe
90       * Take returns the same future object returned by submit
91       */
92      public void testTake2() throws InterruptedException {
93 <        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
94 <        Future f1 = cs.submit(new StringTask());
95 <        Future f2 = cs.take();
93 >        CompletionService<String> cs = new ExecutorCompletionService<String>(cachedThreadPool);
94 >        Future<?> f1 = cs.submit(new StringTask());
95 >        Future<?> f2 = cs.take();
96          assertSame(f1, f2);
97      }
98  
99      /**
100       * poll returns non-null when the returned task is completed
101       */
102 <    public void testPoll1()
103 <        throws InterruptedException, ExecutionException {
106 <        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
102 >    public void testPoll1() throws Exception {
103 >        CompletionService<String> cs = new ExecutorCompletionService<String>(cachedThreadPool);
104          assertNull(cs.poll());
105          cs.submit(new StringTask());
106  
107          long startTime = System.nanoTime();
108 <        Future f;
108 >        Future<?> f;
109          while ((f = cs.poll()) == null) {
110              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
111                  fail("timed out");
# Line 121 | Line 118 | public class ExecutorCompletionServiceTe
118      /**
119       * timed poll returns non-null when the returned task is completed
120       */
121 <    public void testPoll2()
122 <        throws InterruptedException, ExecutionException {
126 <        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
121 >    public void testPoll2() throws Exception {
122 >        CompletionService<String> cs = new ExecutorCompletionService<String>(cachedThreadPool);
123          assertNull(cs.poll());
124          cs.submit(new StringTask());
125  
126          long startTime = System.nanoTime();
127 <        Future f;
128 <        while ((f = cs.poll(SHORT_DELAY_MS, MILLISECONDS)) == null) {
127 >        Future<?> f;
128 >        while ((f = cs.poll(timeoutMillis(), MILLISECONDS)) == null) {
129 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
130              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
131                  fail("timed out");
132              Thread.yield();
# Line 141 | Line 138 | public class ExecutorCompletionServiceTe
138      /**
139       * poll returns null before the returned task is completed
140       */
141 <    public void testPollReturnsNull()
142 <        throws InterruptedException, ExecutionException {
143 <        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
144 <        CountDownLatch proceed = new CountDownLatch(1);
145 <        cs.submit(new Callable() { public String call() throws Exception {
149 <            proceed.await();
141 >    public void testPollReturnsNullBeforeCompletion() throws Exception {
142 >        CompletionService<String> cs = new ExecutorCompletionService<String>(cachedThreadPool);
143 >        final CountDownLatch proceed = new CountDownLatch(1);
144 >        cs.submit(new Callable<String>() { public String call() throws Exception {
145 >            await(proceed);
146              return TEST_STRING;
147          }});
148          assertNull(cs.poll());
# Line 162 | Line 158 | public class ExecutorCompletionServiceTe
158      /**
159       * successful and failed tasks are both returned
160       */
161 <    public void testTaskAssortment()
162 <        throws InterruptedException, ExecutionException {
167 <        CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
161 >    public void testTaskAssortment() throws Exception {
162 >        CompletionService<String> cs = new ExecutorCompletionService<String>(cachedThreadPool);
163          ArithmeticException ex = new ArithmeticException();
164 <        for (int i = 0; i < 2; i++) {
164 >        final int rounds = 2;
165 >        for (int i = rounds; i--> 0; ) {
166              cs.submit(new StringTask());
167              cs.submit(callableThrowing(ex));
168              cs.submit(runnableThrowing(ex), null);
169          }
170          int normalCompletions = 0;
171          int exceptionalCompletions = 0;
172 <        for (int i = 0; i < 3 * 2; i++) {
172 >        for (int i = 3 * rounds; i--> 0; ) {
173              try {
174 <                if (cs.take().get() == TEST_STRING)
175 <                    normalCompletions++;
176 <            }
177 <            catch (ExecutionException expected) {
182 <                assertTrue(expected.getCause() instanceof ArithmeticException);
174 >                assertSame(TEST_STRING, cs.take().get());
175 >                normalCompletions++;
176 >            } catch (ExecutionException expected) {
177 >                assertSame(ex, expected.getCause());
178                  exceptionalCompletions++;
179              }
180          }
181 <        assertEquals(2 * 1, normalCompletions);
182 <        assertEquals(2 * 2, exceptionalCompletions);
181 >        assertEquals(1 * rounds, normalCompletions);
182 >        assertEquals(2 * rounds, exceptionalCompletions);
183          assertNull(cs.poll());
184      }
185  
# Line 209 | Line 204 | public class ExecutorCompletionServiceTe
204          try (PoolCleaner cleaner = cleaner(e)) {
205              assertNull(cs.poll());
206              Callable<String> c = new StringTask();
207 <            Future f1 = cs.submit(c);
207 >            Future<?> f1 = cs.submit(c);
208              assertTrue("submit must return MyCallableFuture",
209                         f1 instanceof MyCallableFuture);
210 <            Future f2 = cs.take();
210 >            Future<?> f2 = cs.take();
211              assertSame("submit and take must return same objects", f1, f2);
212              assertTrue("completed task must have set done", done.get());
213          }
# Line 239 | Line 234 | public class ExecutorCompletionServiceTe
234          try (PoolCleaner cleaner = cleaner(e)) {
235              assertNull(cs.poll());
236              Runnable r = new NoOpRunnable();
237 <            Future f1 = cs.submit(r, null);
237 >            Future<?> f1 = cs.submit(r, null);
238              assertTrue("submit must return MyRunnableFuture",
239                         f1 instanceof MyRunnableFuture);
240 <            Future f2 = cs.take();
240 >            Future<?> f2 = cs.take();
241              assertSame("submit and take must return same objects", f1, f2);
242              assertTrue("completed task must have set done", done.get());
243          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines