Skip to content

Commit

Permalink
Merge pull request #13 from livio/bugfix/issue_12
Browse files Browse the repository at this point in the history
Fixe Issue #12: Handle quickly canceled tasks before operations
  • Loading branch information
JulianKast authored Mar 18, 2021
2 parents a115fc9 + 5b66088 commit 11cb241
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 51 deletions.
11 changes: 8 additions & 3 deletions Taskmaster/src/main/java/com/livio/taskmaster/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ boolean prepareNextTask() {
if(nextTask != null){
int taskState = nextTask.getState();
while ( head != null && taskState != Task.READY ){
if(taskState == Task.CANCELED){
TaskmasterLogger.v(TAG, nextTask.name + " task was canceled, dropping.");
if(taskState == Task.CANCELED || taskState == Task.ERROR || taskState == Task.FINISHED){
TaskmasterLogger.v(TAG, nextTask.name + " task was stale, dropping.");
//move to next task
head = head.next;
if(head == null){
Expand All @@ -133,7 +133,7 @@ boolean prepareNextTask() {

}
}else if( taskState == Task.BLOCKED){
nextTask.switchStates(Task.READY);
head.item.switchStates(Task.READY);
break;
}
}
Expand Down Expand Up @@ -411,6 +411,11 @@ private Task searchAndDestroy(String name, boolean shouldRemove){
* @return the current head of the queue
*/
public Task peekNextTask() {
if(currentTask == null) {
//The queue must be cleared of stale tasks before returning a potential task
//This is only when there are no currently executing tasks
prepareNextTask();
}
synchronized (TASKS_LOCK) {
if (head != null) {
return head.item;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.livio.taskmaster.smoketests;

import com.livio.taskmaster.Queue;
import com.livio.taskmaster.Task;

public class CancelTasksTest extends BaseTest {
Queue queue;

public CancelTasksTest(ITest callback) {
super(1, callback);
}

@Override
public void setUp() {
queue = taskmaster.createQueue("Queue", 1, false);
queue.add(taskA, false);
taskA.cancelTask();
queue.add(taskB, false);
queue.add(taskC, false);
}


Task taskA = new Task("-- Task A --") {

@Override
public void onExecute() {
System.out.println("-------- Running task A, this should not happen");
this.onFinished();
}
};

Task taskB = new Task("-- Task B --") {

@Override
public void onExecute() {
System.out.println("-------- Running task B");
this.onFinished();
}
};
Task taskC = new Task("-- Task C --") {

@Override
public void onExecute() {
System.out.println("-------- Running task C");
this.onFinished();
taskmaster.shutdown();
onTestCompleted(true);
}
};
}
103 changes: 55 additions & 48 deletions Taskmaster/src/test/java/com/livio/taskmaster/smoketests/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,65 +37,72 @@

public class Tests {

private final static int SIMPLE = 0;
private final static int QUEUE_MOD = 1;
private final static int BIG = 2;
private final static int PAUSE = 3;
private final static int CANCEL_TASK = 4;

public static void main(String[] args) {

Taskmaster.setLogger(new Logger());

//Need to fix the chaining requirement, but for smoke tests this should be fine
SimpleTest simpleTest = new SimpleTest(new BaseTest.ITest() {
@Override
public void onTestCompleted(boolean success) {
System.out.println("Finished simple test");
QueueModTest queueModTest = new QueueModTest(new BaseTest.ITest() {
@Override
public void onTestCompleted(boolean success) {
System.out.println("Finished queue mod test");

BigTest bigTest = new BigTest(new BaseTest.ITest() {
@Override
public void onTestCompleted(boolean success) {
System.out.println("Finished big test");

PauseTest pauseTest = new PauseTest(new BaseTest.ITest() {
@Override
public void onTestCompleted(boolean success) {
System.out.println("Finished pause test");
}
});
pauseTest.start();
}
});
bigTest.start();
}
});
queueModTest.start();
}
});

simpleTest.start();
//simpleTest();

//testQueueModificaitons();
//startTheMachine();

BaseTest.ITest testCallback = new BaseTest.ITest() {
int testProgress = -1;

/* If you want to test the daemon setting use something like the following
new Thread(new Runnable(){
@Override
public void run() {
Object LOCK = new Object();
try{
//LOCK.wait();
Thread.sleep(50000);
}catch (Exception e){
e.printStackTrace();}
public void onTestCompleted(boolean success) {
if (testProgress >= 0) {
System.out.println("------------ Finished " + nameForTask(testProgress) + " test successfully? " + success);
}

testProgress++;
BaseTest nextTest = nextTestToRun(testProgress, this);

if (nextTest != null) {
System.out.println("------------ Starting test: " + nameForTask(testProgress) + " ------------");
nextTest.start();
} else {
System.out.println(" ------------------ Finished all tests ------------------");
return;
}
}
};
testCallback.onTestCompleted(true);
}


}).start();
*/
public static BaseTest nextTestToRun(int progress, BaseTest.ITest callback) {
switch (progress) {
case SIMPLE:
return new SimpleTest(callback);
case QUEUE_MOD:
return new QueueModTest(callback);
case BIG:
return new BigTest(callback);
case PAUSE:
return new PauseTest(callback);
case CANCEL_TASK:
return new CancelTasksTest(callback);
}
return null;
}

public static String nameForTask(int progress) {
switch (progress) {
case SIMPLE:
return "SIMPLE";
case QUEUE_MOD:
return "QUEUE_MOD";
case BIG:
return "BIG";
case PAUSE:
return "PAUSE";
case CANCEL_TASK:
return "CANCEL_TASK";
}
return null;
}


Expand Down

0 comments on commit 11cb241

Please sign in to comment.