1 /* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.concurrent; 27 28 import javafx.concurrent.mocks.EpicFailTask; 29 import javafx.concurrent.mocks.InfiniteTask; 30 import javafx.concurrent.mocks.RunAwayTask; 31 import javafx.concurrent.mocks.SimpleTask; 32 import org.junit.Before; 33 import org.junit.Test; 34 35 import static org.junit.Assert.*; 36 37 /** 38 * Tests what happens to a Task if it is canceled in each of the various 39 * states that a Task may be in. 40 */ 41 public class TaskCancelTest { 42 /** 43 * Since the InfiniteTask never ends (normally), I can test it from the 44 * ready, scheduled, running, cancelled, and failed states. I can't use 45 * it for testing what happens when the task succeeds though. 46 */ 47 private InfiniteTask task; 48 49 @Before public void setup() { 50 task = new InfiniteTask(); 51 } 52 53 /** 54 * Since the task begins in the ready state, I can just cancel it and 55 * see what happens. 56 */ 57 @Test public void cancellingA_READY_TaskShouldChangeStateTo_CANCELLED() { 58 assertTrue(task.cancel()); 59 assertEquals(Task.State.CANCELLED, task.getState()); 60 assertTrue(task.isDone()); 61 } 62 63 /** 64 * I have some cheap mechanism for simulating the scheduling of a Task 65 * (it just changes the Task state correctly). So put it in the 66 * Scheduled state and then cancel it and see what happens. 67 */ 68 @Test public void cancellingA_SCHEDULED_TaskShouldChangeStateTo_CANCELLED() { 69 task.simulateSchedule(); 70 assertTrue(task.cancel()); 71 assertEquals(Task.State.CANCELLED, task.getState()); 72 assertTrue(task.isDone()); 73 } 74 75 /** 76 * Since the task is an infinitely running task, and since there is a 77 * semaphore on the AbstractTask that tells me when certain state 78 * transitions have occurred, I can simply fire up another thread and 79 * run the infinite task, and then wait for that semaphore to trip. When 80 * it does, I know that we're running (and will never leave that state) 81 * so I can go ahead and then cancel it. 82 * 83 * @throws Exception shouldn't throw anything unless th.join fails 84 */ 85 @Test public void cancellingA_RUNNING_TaskShouldChangeStateTo_CANCELLED() throws Exception { 86 Thread th = new Thread(task); 87 th.start(); 88 task.runningSemaphore.acquire(); 89 assertTrue(task.cancel()); 90 th.join(); 91 92 assertEquals(Task.State.CANCELLED, task.getState()); 93 // TODO why is this commented out? 94 // assertNull(task.getException()); 95 assertNull(task.getValue()); 96 assertTrue(task.isDone()); 97 } 98 99 /** 100 * In this case I don't want to use the infinite task, so I'll just 101 * use a SimpleTask instead 102 */ 103 @Test public void cancellingA_SUCCEEDED_TaskShouldNotChangeTo_CANCELLED() { 104 Task t = new SimpleTask(); 105 t.run(); 106 assertFalse(t.cancel()); 107 assertEquals(Task.State.SUCCEEDED, t.getState()); 108 assertTrue(t.isDone()); 109 } 110 111 /** 112 * Although I could end up using the infinite task for this one, I'm going 113 * to go ahead and reuse the epic fail task instead 114 */ 115 @Test public void cancellingA_FAILED_TaskShouldNotChangeTo_CANCELLED() { 116 Task t = new EpicFailTask(); 117 t.run(); 118 assertFalse(t.cancel()); 119 assertEquals(Task.State.FAILED, t.getState()); 120 assertTrue(t.isDone()); 121 } 122 123 /** 124 * 125 */ 126 @Test public void aFreeRunningCancelledTaskReturnValueShouldBeIgnored() throws Exception { 127 RunAwayTask runAway = new RunAwayTask() { 128 protected void loop(int count) throws Exception { 129 } 130 }; 131 Thread th = new Thread(runAway); 132 th.start(); 133 runAway.runningSemaphore.acquire(); 134 assertTrue(runAway.cancel()); 135 runAway.stopLooping.set(true); 136 th.join(); 137 138 assertEquals(Task.State.CANCELLED, runAway.getState()); 139 // TODO why is this commented out? 140 // assertNull(task.getException()); 141 assertNull(runAway.getValue()); 142 assertTrue(runAway.isDone()); 143 } 144 }