FileDocCategorySizeDatePackage
FutureTaskTest.javaAPI DocAndroid 1.5 API14410Wed May 06 22:41:02 BST 2009tests.api.java.util.concurrent

FutureTaskTest

public class FutureTaskTest extends JSR166TestCase

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        junit.textui.TestRunner.run (suite());        
    
public static junit.framework.Testsuite()

        return new TestSuite(FutureTaskTest.class);
    
public voidtestCancelAfterRun()
cancel of a completed task fails

        FutureTask task = new FutureTask( new NoOpCallable());
        task.run();
        assertFalse(task.cancel(false));
        assertTrue(task.isDone());
        assertFalse(task.isCancelled());
    
public voidtestCancelBeforeRun()
Cancelling before running succeeds

        FutureTask task = new FutureTask( new NoOpCallable());
        assertTrue(task.cancel(false));
        task.run();
        assertTrue(task.isDone());
        assertTrue(task.isCancelled());
    
public voidtestCancelBeforeRun2()
Cancel(true) before run succeeds

        FutureTask task = new FutureTask( new NoOpCallable());
        assertTrue(task.cancel(true));
        task.run();
        assertTrue(task.isDone());
        assertTrue(task.isCancelled());
    
public voidtestCancelInterrupt()
cancel(true) interrupts a running task

        FutureTask task = new FutureTask( new Callable() {
                public Object call() {
                    try {
                        Thread.sleep(MEDIUM_DELAY_MS);
                        threadShouldThrow();
                    }
                    catch (InterruptedException success) {}
                    return Boolean.TRUE;
                } });
        Thread t = new  Thread(task);
        t.start();
        
        try {
            Thread.sleep(SHORT_DELAY_MS);
            assertTrue(task.cancel(true));
            t.join();
            assertTrue(task.isDone());
            assertTrue(task.isCancelled());
        } catch(InterruptedException e){
            unexpectedException();
        }
    
public voidtestCancelNoInterrupt()
cancel(false) does not interrupt a running task

        FutureTask task = new FutureTask( new Callable() {
                public Object call() {
                    try {
                        Thread.sleep(MEDIUM_DELAY_MS);
                    }
                    catch (InterruptedException success) {
                        threadFail("should not interrupt");
                    }
                    return Boolean.TRUE;
                } });
        Thread t = new  Thread(task);
        t.start();
        
        try {
            Thread.sleep(SHORT_DELAY_MS);
            assertTrue(task.cancel(false));
            t.join();
            assertTrue(task.isDone());
            assertTrue(task.isCancelled());
        } catch(InterruptedException e){
            unexpectedException();
        }
    
public voidtestConstructor()
Creating a future with a null callable throws NPE

        try {
            FutureTask task = new FutureTask(null);
            shouldThrow();
        }
        catch(NullPointerException success) {
        }
    
public voidtestConstructor2()
creating a future with null runnable fails

        try {
            FutureTask task = new FutureTask(null, Boolean.TRUE);
            shouldThrow();
        }
        catch(NullPointerException success) {
        }
    
public voidtestGet1()
set in one thread causes get in another thread to retrieve value

        final FutureTask ft = new FutureTask(new Callable() {
                public Object call() {
                    try {
                        Thread.sleep(MEDIUM_DELAY_MS);
                    } catch(InterruptedException e){
                        threadUnexpectedException();
                    }
                    return Boolean.TRUE;
                }
        });
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        ft.get();
                    } catch(Exception e){
                        threadUnexpectedException();
                    }
                }
            });
        try {
            assertFalse(ft.isDone());
            assertFalse(ft.isCancelled());
            t.start();
            Thread.sleep(SHORT_DELAY_MS);
            ft.run();
            t.join();
            assertTrue(ft.isDone());
            assertFalse(ft.isCancelled());
        } catch(InterruptedException e){
            unexpectedException();

        }        
    
public voidtestGet_Cancellation()
Cancelling a task causes get in another thread to throw CancellationException

        final FutureTask ft = new FutureTask(new Callable() {
                public Object call() {
                    try {
                        Thread.sleep(MEDIUM_DELAY_MS);
                        threadShouldThrow();
                    } catch(InterruptedException e){
                    }
                    return Boolean.TRUE;
                }
            });
        try {
            Thread t1 = new Thread(new Runnable() {
                    public void run() {
                        try {
                            ft.get();
                            threadShouldThrow();
                        } catch(CancellationException success){
                        }
                        catch(Exception e){
                            threadUnexpectedException();
                        }
                    }
                });
            Thread t2 = new Thread(ft);
            t1.start(); 
            t2.start();
            Thread.sleep(SHORT_DELAY_MS);
            ft.cancel(true);
            t1.join();
            t2.join();
        } catch(InterruptedException success){
            unexpectedException();
        }
    
public voidtestGet_ExecutionException()
A runtime exception in task causes get to throw ExecutionException

        final FutureTask ft = new FutureTask(new Callable() {
                public Object call() {
                    int i = 5/0;
                    return Boolean.TRUE;
                }
            });
        try {
            ft.run();
            ft.get();
            shouldThrow();
        } catch(ExecutionException success){
        }
        catch(Exception e){
            unexpectedException();
        }
    
public voidtestGet_InterruptedException()
Interrupting a waiting get causes it to throw InterruptedException

        final FutureTask ft = new FutureTask(new NoOpCallable());
        Thread t = new Thread(new Runnable() {
                public void run() {                    
                    try {
                        ft.get();
                        threadShouldThrow();
                    } catch(InterruptedException success){
                    } catch(Exception e){
                        threadUnexpectedException();
                    }
                }
            });
        try {
            t.start();
            Thread.sleep(SHORT_DELAY_MS);
            t.interrupt();
            t.join();
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestGet_TimeoutException()
A timed out timed get throws TimeoutException

        try {
            FutureTask ft = new FutureTask(new NoOpCallable());
            ft.get(1,TimeUnit.MILLISECONDS);
            shouldThrow();
        } catch(TimeoutException success){}
        catch(Exception success){
            unexpectedException();
        }
    
public voidtestIsDone()
isDone is true when a task completes

        FutureTask task = new FutureTask( new NoOpCallable());
        task.run();
        assertTrue(task.isDone());
        assertFalse(task.isCancelled());
    
public voidtestResetAfterCancel()
runAndReset after cancellation fails

        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
        assertTrue(task.cancel(false));
        assertFalse(task.runAndReset());
        assertTrue(task.isDone());
        assertTrue(task.isCancelled());
    
public voidtestRunAndReset()
runAndReset of a non-cancelled task succeeds

        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
        assertTrue(task.runAndReset());
        assertFalse(task.isDone());
    
public voidtestSet()
setting value causes get to return it

        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
        task.set(one);
        try {
            assertEquals(task.get(), one);
        }
        catch(Exception e) {
            unexpectedException();
        }
    
public voidtestSetException()
setException causes get to throw ExecutionException

        Exception nse = new NoSuchElementException();
        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
        task.setException(nse);
        try {
            Object x = task.get();
            shouldThrow();
        }
        catch(ExecutionException ee) {
            Throwable cause = ee.getCause();
            assertEquals(cause, nse);
        }
        catch(Exception e) {
            unexpectedException();
        }
    
public voidtestTimedGet1()
set in one thread causes timed get in another thread to retrieve value

        final FutureTask ft = new FutureTask(new Callable() {
                public Object call() {
                    try {
                        Thread.sleep(MEDIUM_DELAY_MS);
                    } catch(InterruptedException e){
                        threadUnexpectedException();
                    }
                    return Boolean.TRUE;
                }
            });
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
                    } catch(TimeoutException success) {
                    } catch(Exception e){
                        threadUnexpectedException();
                    }
                }
            });
        try {
            assertFalse(ft.isDone());
            assertFalse(ft.isCancelled());
            t.start();
            ft.run();
            t.join();
            assertTrue(ft.isDone());
            assertFalse(ft.isCancelled());
        } catch(InterruptedException e){
            unexpectedException();
            
        }        
    
public voidtestTimedGet_Cancellation()
Cancelling a task causes timed get in another thread to throw CancellationException

        final FutureTask ft = new FutureTask(new Callable() {
                public Object call() {
                    try {
                        Thread.sleep(SMALL_DELAY_MS);
                        threadShouldThrow();
                    } catch(InterruptedException e) {
                    }
                    return Boolean.TRUE;
                }
            });
        try {
            Thread t1 = new Thread(new Runnable() {
                    public void run() {
                        try {
                            ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
                            threadShouldThrow();
                        } catch(CancellationException success) {}
                        catch(Exception e){
                            threadUnexpectedException();
                        }
                    }
                });
            Thread t2 = new Thread(ft);
            t1.start(); 
            t2.start();
            Thread.sleep(SHORT_DELAY_MS);
            ft.cancel(true);
            t1.join();
            t2.join();
        } catch(InterruptedException ie){
            unexpectedException();
        }
    
public voidtestTimedGet_ExecutionException2()
A runtime exception in task causes timed get to throw ExecutionException

        final FutureTask ft = new FutureTask(new Callable() {
                public Object call() {
                    int i = 5/0;
                    return Boolean.TRUE;
                }
            });
        try {
            ft.run();
            ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
            shouldThrow();
        } catch(ExecutionException success) { 
        } catch(TimeoutException success) { } // unlikely but OK
        catch(Exception e){
            unexpectedException();
        }
    
public voidtestTimedGet_InterruptedException2()
Interrupting a waiting timed get causes it to throw InterruptedException

        final FutureTask ft = new FutureTask(new NoOpCallable());
        Thread t = new Thread(new Runnable() {
                 public void run() {                    
                    try {
                        ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS);
                        threadShouldThrow();
                    } catch(InterruptedException success){}
                    catch(Exception e){
                        threadUnexpectedException();
                    }
                }
            });
        try {
            t.start();
            Thread.sleep(SHORT_DELAY_MS);
            t.interrupt();
            t.join();
        } catch(Exception e){
            unexpectedException();
        }