[cvs] / jsr166 / src / test / tck / FutureTaskTest.java Repository:
ViewVC logotype

Diff of /jsr166/src/test/tck/FutureTaskTest.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.31, Sat Dec 15 21:50:30 2012 UTC revision 1.32, Sun Dec 16 18:52:27 2012 UTC
# Line 7  Line 7 
7   */   */
8    
9  import junit.framework.*;  import junit.framework.*;
10    import java.security.Permission;
11  import java.util.concurrent.Callable;  import java.util.concurrent.Callable;
12  import java.util.concurrent.CancellationException;  import java.util.concurrent.CancellationException;
13  import java.util.concurrent.CountDownLatch;  import java.util.concurrent.CountDownLatch;
# Line 319  Line 320 
320          PublicFutureTask task = new PublicFutureTask(new NoOpCallable());          PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
321          assertTrue(task.cancel(false));          assertTrue(task.cancel(false));
322          task.run();          task.run();
323            assertEquals(0, task.runCount());
324          assertEquals(0, task.setCount());          assertEquals(0, task.setCount());
325          assertEquals(0, task.setExceptionCount());          assertEquals(0, task.setExceptionCount());
326            assertTrue(task.isCancelled());
327            assertTrue(task.isDone());
328          tryToConfuseDoneTask(task);          tryToConfuseDoneTask(task);
         checkCancelled(task);  
329          assertEquals(0, task.runCount());          assertEquals(0, task.runCount());
330            checkCancelled(task);
331      }      }
332    
333      /**      /**
# Line 333  Line 337 
337          PublicFutureTask task = new PublicFutureTask(new NoOpCallable());          PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
338          assertTrue(task.cancel(true));          assertTrue(task.cancel(true));
339          task.run();          task.run();
340            assertEquals(0, task.runCount());
341          assertEquals(0, task.setCount());          assertEquals(0, task.setCount());
342          assertEquals(0, task.setExceptionCount());          assertEquals(0, task.setExceptionCount());
343            assertTrue(task.isCancelled());
344            assertTrue(task.isDone());
345          tryToConfuseDoneTask(task);          tryToConfuseDoneTask(task);
         checkCancelled(task);  
346          assertEquals(0, task.runCount());          assertEquals(0, task.runCount());
347            checkCancelled(task);
348      }      }
349    
350      /**      /**
# Line 347  Line 354 
354          PublicFutureTask task = new PublicFutureTask(new NoOpCallable());          PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
355          task.run();          task.run();
356          assertFalse(task.cancel(false));          assertFalse(task.cancel(false));
357            assertEquals(1, task.runCount());
358          assertEquals(1, task.setCount());          assertEquals(1, task.setCount());
359          assertEquals(0, task.setExceptionCount());          assertEquals(0, task.setExceptionCount());
360          tryToConfuseDoneTask(task);          tryToConfuseDoneTask(task);
# Line 361  Line 369 
369          PublicFutureTask task = new PublicFutureTask(new NoOpCallable());          PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
370          task.run();          task.run();
371          assertFalse(task.cancel(true));          assertFalse(task.cancel(true));
372            assertEquals(1, task.runCount());
373          assertEquals(1, task.setCount());          assertEquals(1, task.setCount());
374          assertEquals(0, task.setExceptionCount());          assertEquals(0, task.setExceptionCount());
375          tryToConfuseDoneTask(task);          tryToConfuseDoneTask(task);
# Line 387  Line 396 
396          await(pleaseCancel);          await(pleaseCancel);
397          assertTrue(task.cancel(true));          assertTrue(task.cancel(true));
398          assertTrue(task.isCancelled());          assertTrue(task.isCancelled());
399            assertTrue(task.isDone());
400          awaitTermination(t);          awaitTermination(t);
401          assertEquals(1, task.runCount());          assertEquals(1, task.runCount());
402          assertEquals(1, task.setCount());          assertEquals(1, task.setCount());
# Line 396  Line 406 
406      }      }
407    
408      /**      /**
409         * cancel(true) interrupts a running task that subsequently
410         * succeeds, with a security manager that does not permit
411         * Thread.interrupt
412         */
413        public void testCancelInterrupt_ThrowsSecurityException() {
414            if (System.getSecurityManager() != null)
415                return;
416    
417            final CountDownLatch pleaseCancel = new CountDownLatch(1);
418            final CountDownLatch cancelled = new CountDownLatch(1);
419            final PublicFutureTask task =
420                new PublicFutureTask(new CheckedRunnable() {
421                    public void realRun() {
422                        pleaseCancel.countDown();
423                        await(cancelled);
424                        assertFalse(Thread.interrupted());
425                    }});
426    
427            final Thread t = newStartedThread(task);
428            await(pleaseCancel);
429            System.setSecurityManager(new SecurityManager() {
430                public void checkAccess(Thread t) { throw new SecurityException(); }
431                public void checkPermission(Permission p) {}});
432            try {
433                try {
434                    task.cancel(true);
435                    shouldThrow();
436                }
437                catch (SecurityException expected) {}
438            } finally {
439                System.setSecurityManager(null);
440            }
441            assertTrue(task.isCancelled());
442            assertTrue(task.isDone());
443            assertEquals(1, task.runCount());
444            assertEquals(1, task.doneCount());
445            assertEquals(0, task.setCount());
446            assertEquals(0, task.setExceptionCount());
447            cancelled.countDown();
448            awaitTermination(t);
449            assertEquals(1, task.setCount());
450            assertEquals(0, task.setExceptionCount());
451            tryToConfuseDoneTask(task);
452            checkCancelled(task);
453        }
454    
455        /**
456       * cancel(true) interrupts a running task that subsequently throws       * cancel(true) interrupts a running task that subsequently throws
457       */       */
458      public void testCancelInterrupt_taskFails() {      public void testCancelInterrupt_taskFails() {

Legend:
Removed from v.1.31  
changed lines
  Added in v.1.32

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8