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

Comparing jsr166/src/test/tck/AbstractExecutorServiceTest.java (file contents):
Revision 1.40 by jsr166, Sun Oct 4 18:18:48 2015 UTC vs.
Revision 1.48 by jsr166, Mon Jul 17 22:27:30 2017 UTC

# Line 11 | Line 11 | import static java.util.concurrent.TimeU
11   import java.security.PrivilegedAction;
12   import java.security.PrivilegedExceptionAction;
13   import java.util.ArrayList;
14 + import java.util.Collection;
15   import java.util.Collections;
16   import java.util.List;
17   import java.util.concurrent.AbstractExecutorService;
# Line 165 | Line 166 | public class AbstractExecutorServiceTest
166      }
167  
168      /**
169 <     * execute(null runnable) throws NPE
169 >     * Submitting null tasks throws NullPointerException
170       */
171 <    public void testExecuteNullRunnable() {
172 <        ExecutorService e = new DirectExecutorService();
173 <        try {
174 <            e.submit((Runnable) null);
175 <            shouldThrow();
175 <        } catch (NullPointerException success) {}
176 <    }
177 <
178 <    /**
179 <     * submit(null callable) throws NPE
180 <     */
181 <    public void testSubmitNullCallable() {
182 <        ExecutorService e = new DirectExecutorService();
183 <        try {
184 <            e.submit((Callable) null);
185 <            shouldThrow();
186 <        } catch (NullPointerException success) {}
171 >    public void testNullTaskSubmission() {
172 >        final ExecutorService e = new DirectExecutorService();
173 >        try (PoolCleaner cleaner = cleaner(e)) {
174 >            assertNullTaskSubmissionThrowsNullPointerException(e);
175 >        }
176      }
177  
178      /**
# Line 194 | Line 183 | public class AbstractExecutorServiceTest
183          final CountDownLatch quittingTime = new CountDownLatch(1);
184          final Callable<Void> awaiter = new CheckedCallable<Void>() {
185              public Void realCall() throws InterruptedException {
186 <                quittingTime.await();
186 >                assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS));
187                  return null;
188              }};
189          final ExecutorService p
190              = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
191                                       new ArrayBlockingQueue<Runnable>(10));
192 <        try (PoolCleaner cleaner = cleaner(p)) {
193 <            Thread t = new Thread(new CheckedInterruptedRunnable() {
192 >        try (PoolCleaner cleaner = cleaner(p, quittingTime)) {
193 >            Thread t = newStartedThread(new CheckedInterruptedRunnable() {
194                  public void realRun() throws Exception {
195                      Future<Void> future = p.submit(awaiter);
196                      submitted.countDown();
197                      future.get();
198                  }});
199 <            t.start();
200 <            submitted.await();
199 >
200 >            await(submitted);
201              t.interrupt();
202 <            t.join();
214 <            quittingTime.countDown();
202 >            awaitTermination(t);
203          }
204      }
205  
# Line 250 | Line 238 | public class AbstractExecutorServiceTest
238      }
239  
240      /**
241 <     * invokeAny(empty collection) throws IAE
241 >     * invokeAny(empty collection) throws IllegalArgumentException
242       */
243      public void testInvokeAny2() throws Exception {
244          final ExecutorService e = new DirectExecutorService();
245 +        final Collection<Callable<String>> emptyCollection
246 +            = Collections.emptyList();
247          try (PoolCleaner cleaner = cleaner(e)) {
248              try {
249 <                e.invokeAny(new ArrayList<Callable<String>>());
249 >                e.invokeAny(emptyCollection);
250                  shouldThrow();
251              } catch (IllegalArgumentException success) {}
252          }
# Line 268 | Line 258 | public class AbstractExecutorServiceTest
258      public void testInvokeAny3() throws Exception {
259          final ExecutorService e = new DirectExecutorService();
260          try (PoolCleaner cleaner = cleaner(e)) {
261 <            List<Callable<Long>> l = new ArrayList<Callable<Long>>();
261 >            List<Callable<Long>> l = new ArrayList<>();
262              l.add(new Callable<Long>() {
263                        public Long call() { throw new ArithmeticException(); }});
264              l.add(null);
# Line 285 | Line 275 | public class AbstractExecutorServiceTest
275      public void testInvokeAny4() throws InterruptedException {
276          final ExecutorService e = new DirectExecutorService();
277          try (PoolCleaner cleaner = cleaner(e)) {
278 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
278 >            List<Callable<String>> l = new ArrayList<>();
279              l.add(new NPETask());
280              try {
281                  e.invokeAny(l);
# Line 302 | Line 292 | public class AbstractExecutorServiceTest
292      public void testInvokeAny5() throws Exception {
293          final ExecutorService e = new DirectExecutorService();
294          try (PoolCleaner cleaner = cleaner(e)) {
295 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
295 >            List<Callable<String>> l = new ArrayList<>();
296              l.add(new StringTask());
297              l.add(new StringTask());
298              String result = e.invokeAny(l);
# Line 324 | Line 314 | public class AbstractExecutorServiceTest
314      }
315  
316      /**
317 <     * invokeAll(empty collection) returns empty collection
317 >     * invokeAll(empty collection) returns empty list
318       */
319      public void testInvokeAll2() throws InterruptedException {
320          final ExecutorService e = new DirectExecutorService();
321 +        final Collection<Callable<String>> emptyCollection
322 +            = Collections.emptyList();
323          try (PoolCleaner cleaner = cleaner(e)) {
324 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
324 >            List<Future<String>> r = e.invokeAll(emptyCollection);
325              assertTrue(r.isEmpty());
326          }
327      }
# Line 340 | Line 332 | public class AbstractExecutorServiceTest
332      public void testInvokeAll3() throws InterruptedException {
333          final ExecutorService e = new DirectExecutorService();
334          try (PoolCleaner cleaner = cleaner(e)) {
335 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
335 >            List<Callable<String>> l = new ArrayList<>();
336              l.add(new StringTask());
337              l.add(null);
338              try {
# Line 356 | Line 348 | public class AbstractExecutorServiceTest
348      public void testInvokeAll4() throws Exception {
349          final ExecutorService e = new DirectExecutorService();
350          try (PoolCleaner cleaner = cleaner(e)) {
351 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
351 >            List<Callable<String>> l = new ArrayList<>();
352              l.add(new NPETask());
353              List<Future<String>> futures = e.invokeAll(l);
354              assertEquals(1, futures.size());
# Line 375 | Line 367 | public class AbstractExecutorServiceTest
367      public void testInvokeAll5() throws Exception {
368          final ExecutorService e = new DirectExecutorService();
369          try (PoolCleaner cleaner = cleaner(e)) {
370 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
370 >            List<Callable<String>> l = new ArrayList<>();
371              l.add(new StringTask());
372              l.add(new StringTask());
373              List<Future<String>> futures = e.invokeAll(l);
# Line 392 | Line 384 | public class AbstractExecutorServiceTest
384          final ExecutorService e = new DirectExecutorService();
385          try (PoolCleaner cleaner = cleaner(e)) {
386              try {
387 <                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
387 >                e.invokeAny(null, randomTimeout(), randomTimeUnit());
388                  shouldThrow();
389              } catch (NullPointerException success) {}
390          }
391      }
392  
393      /**
394 <     * timed invokeAny(null time unit) throws NPE
394 >     * timed invokeAny(null time unit) throws NullPointerException
395       */
396      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
397          final ExecutorService e = new DirectExecutorService();
398          try (PoolCleaner cleaner = cleaner(e)) {
399 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
399 >            List<Callable<String>> l = new ArrayList<>();
400              l.add(new StringTask());
401              try {
402 <                e.invokeAny(l, MEDIUM_DELAY_MS, null);
402 >                e.invokeAny(l, randomTimeout(), null);
403                  shouldThrow();
404              } catch (NullPointerException success) {}
405          }
406      }
407  
408      /**
409 <     * timed invokeAny(empty collection) throws IAE
409 >     * timed invokeAny(empty collection) throws IllegalArgumentException
410       */
411      public void testTimedInvokeAny2() throws Exception {
412          final ExecutorService e = new DirectExecutorService();
413 +        final Collection<Callable<String>> emptyCollection
414 +            = Collections.emptyList();
415          try (PoolCleaner cleaner = cleaner(e)) {
416              try {
417 <                e.invokeAny(new ArrayList<Callable<String>>(),
424 <                            MEDIUM_DELAY_MS, MILLISECONDS);
417 >                e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit());
418                  shouldThrow();
419              } catch (IllegalArgumentException success) {}
420          }
# Line 433 | Line 426 | public class AbstractExecutorServiceTest
426      public void testTimedInvokeAny3() throws Exception {
427          final ExecutorService e = new DirectExecutorService();
428          try (PoolCleaner cleaner = cleaner(e)) {
429 <            List<Callable<Long>> l = new ArrayList<Callable<Long>>();
429 >            List<Callable<Long>> l = new ArrayList<>();
430              l.add(new Callable<Long>() {
431                        public Long call() { throw new ArithmeticException(); }});
432              l.add(null);
433              try {
434 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
434 >                e.invokeAny(l, randomTimeout(), randomTimeUnit());
435                  shouldThrow();
436              } catch (NullPointerException success) {}
437          }
# Line 450 | Line 443 | public class AbstractExecutorServiceTest
443      public void testTimedInvokeAny4() throws Exception {
444          final ExecutorService e = new DirectExecutorService();
445          try (PoolCleaner cleaner = cleaner(e)) {
446 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
446 >            long startTime = System.nanoTime();
447 >            List<Callable<String>> l = new ArrayList<>();
448              l.add(new NPETask());
449              try {
450 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
450 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
451                  shouldThrow();
452              } catch (ExecutionException success) {
453                  assertTrue(success.getCause() instanceof NullPointerException);
454              }
455 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
456          }
457      }
458  
# Line 467 | Line 462 | public class AbstractExecutorServiceTest
462      public void testTimedInvokeAny5() throws Exception {
463          final ExecutorService e = new DirectExecutorService();
464          try (PoolCleaner cleaner = cleaner(e)) {
465 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
465 >            long startTime = System.nanoTime();
466 >            List<Callable<String>> l = new ArrayList<>();
467              l.add(new StringTask());
468              l.add(new StringTask());
469 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
469 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
470              assertSame(TEST_STRING, result);
471 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
472          }
473      }
474  
475      /**
476 <     * timed invokeAll(null) throws NPE
476 >     * timed invokeAll(null) throws NullPointerException
477       */
478      public void testTimedInvokeAll1() throws InterruptedException {
479          final ExecutorService e = new DirectExecutorService();
480          try (PoolCleaner cleaner = cleaner(e)) {
481              try {
482 <                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
482 >                e.invokeAll(null, randomTimeout(), randomTimeUnit());
483                  shouldThrow();
484              } catch (NullPointerException success) {}
485          }
# Line 494 | Line 491 | public class AbstractExecutorServiceTest
491      public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
492          final ExecutorService e = new DirectExecutorService();
493          try (PoolCleaner cleaner = cleaner(e)) {
494 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
494 >            List<Callable<String>> l = new ArrayList<>();
495              l.add(new StringTask());
496              try {
497 <                e.invokeAll(l, MEDIUM_DELAY_MS, null);
497 >                e.invokeAll(l, randomTimeout(), null);
498                  shouldThrow();
499              } catch (NullPointerException success) {}
500          }
501      }
502  
503      /**
504 <     * timed invokeAll(empty collection) returns empty collection
504 >     * timed invokeAll(empty collection) returns empty list
505       */
506      public void testTimedInvokeAll2() throws InterruptedException {
507          final ExecutorService e = new DirectExecutorService();
508 +        final Collection<Callable<String>> emptyCollection
509 +            = Collections.emptyList();
510          try (PoolCleaner cleaner = cleaner(e)) {
511 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
511 >            List<Future<String>> r =
512 >                e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit());
513              assertTrue(r.isEmpty());
514          }
515      }
516  
517      /**
518 <     * timed invokeAll(c) throws NPE if c has null elements
518 >     * timed invokeAll(c) throws NullPointerException if c has null elements
519       */
520      public void testTimedInvokeAll3() throws InterruptedException {
521          final ExecutorService e = new DirectExecutorService();
522          try (PoolCleaner cleaner = cleaner(e)) {
523 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
523 >            List<Callable<String>> l = new ArrayList<>();
524              l.add(new StringTask());
525              l.add(null);
526              try {
527 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
527 >                e.invokeAll(l, randomTimeout(), randomTimeUnit());
528                  shouldThrow();
529              } catch (NullPointerException success) {}
530          }
# Line 536 | Line 536 | public class AbstractExecutorServiceTest
536      public void testTimedInvokeAll4() throws Exception {
537          final ExecutorService e = new DirectExecutorService();
538          try (PoolCleaner cleaner = cleaner(e)) {
539 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
539 >            List<Callable<String>> l = new ArrayList<>();
540              l.add(new NPETask());
541              List<Future<String>> futures =
542 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
542 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
543              assertEquals(1, futures.size());
544              try {
545                  futures.get(0).get();
# Line 556 | Line 556 | public class AbstractExecutorServiceTest
556      public void testTimedInvokeAll5() throws Exception {
557          final ExecutorService e = new DirectExecutorService();
558          try (PoolCleaner cleaner = cleaner(e)) {
559 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
559 >            List<Callable<String>> l = new ArrayList<>();
560              l.add(new StringTask());
561              l.add(new StringTask());
562              List<Future<String>> futures =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines