2016-08-01 13:17:15 +02:00
|
|
|
import "typings-test";
|
2016-05-05 19:30:24 +02:00
|
|
|
import taskbuffer = require("../dist/index");
|
|
|
|
let should = require("should");
|
2016-08-01 13:17:15 +02:00
|
|
|
import q = require("q")
|
2016-05-06 02:05:45 +02:00
|
|
|
|
2016-05-15 03:16:50 +02:00
|
|
|
// setup some testData to work with
|
2016-05-06 02:05:45 +02:00
|
|
|
let testTask:taskbuffer.Task;
|
|
|
|
let testTaskFunction = function(){
|
2016-08-01 13:17:15 +02:00
|
|
|
let done = q.defer();
|
2016-05-06 02:05:45 +02:00
|
|
|
console.log("main function executed!")
|
|
|
|
done.resolve();
|
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
let testPreTask = new taskbuffer.Task({
|
|
|
|
taskFunction:function(){
|
2016-08-01 13:17:15 +02:00
|
|
|
let done = q.defer();
|
2016-05-06 02:05:45 +02:00
|
|
|
console.log("preTask executed");
|
2016-08-01 13:17:15 +02:00
|
|
|
done.resolve();
|
|
|
|
return done.promise;
|
2016-05-06 02:05:45 +02:00
|
|
|
},
|
|
|
|
preTask:testTask
|
|
|
|
});
|
|
|
|
|
2016-05-04 02:49:43 +02:00
|
|
|
describe("taskbuffer",function(){
|
2016-05-06 02:05:45 +02:00
|
|
|
describe(".Task",function(){
|
|
|
|
it("new Task() should return a new task",function(){
|
|
|
|
testTask = new taskbuffer.Task({taskFunction:testTaskFunction,preTask:testPreTask});
|
2016-05-04 02:49:43 +02:00
|
|
|
});
|
2016-05-05 19:30:24 +02:00
|
|
|
it("testTask should be and instance of Task",function(){
|
|
|
|
testTask.should.be.instanceof(taskbuffer.Task);
|
2016-05-04 02:49:43 +02:00
|
|
|
});
|
|
|
|
it("testTask.idle is true",function(){
|
|
|
|
if (!testTask.idle){
|
|
|
|
throw new Error("testTask.idle is not true");
|
|
|
|
}
|
2016-05-06 02:05:45 +02:00
|
|
|
|
2016-05-04 02:49:43 +02:00
|
|
|
});
|
|
|
|
it("testTask.running is type boolean and initially false",function(){
|
|
|
|
testTask.running.should.be.type("boolean");
|
|
|
|
testTask.running.should.be.false();
|
|
|
|
});
|
2016-05-06 02:05:45 +02:00
|
|
|
it("testTask.trigger() should return Promise",function(){
|
|
|
|
testTask.trigger().should.be.Promise();
|
|
|
|
});
|
|
|
|
it("testTask.trigger() returned Promise should be fullfilled",function(done){
|
|
|
|
testTask.trigger()
|
|
|
|
.then(done);
|
2016-05-13 05:14:29 +02:00
|
|
|
});
|
2016-05-15 03:16:50 +02:00
|
|
|
it("should run a task without pre and afterTask",function(done){
|
|
|
|
let localTestTask = new taskbuffer.Task({taskFunction:testTaskFunction});
|
|
|
|
localTestTask.trigger().then(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe("Taskchain",function(){
|
|
|
|
let testTaskchain;
|
|
|
|
let testTaskArray = [
|
|
|
|
new taskbuffer.Task({
|
|
|
|
name:"task1",
|
|
|
|
taskFunction:function(){
|
2016-08-01 13:17:15 +02:00
|
|
|
let done = q.defer();
|
2016-05-15 15:52:59 +02:00
|
|
|
setTimeout(done.resolve,2000);
|
2016-05-15 03:16:50 +02:00
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
new taskbuffer.Task({
|
|
|
|
name:"task2",
|
|
|
|
taskFunction: function(){
|
2016-08-01 13:17:15 +02:00
|
|
|
let done = q.defer();
|
2016-05-15 15:52:59 +02:00
|
|
|
setTimeout(done.resolve,2000);
|
2016-05-15 03:16:50 +02:00
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
it("should run tasks in sequence",function(done){
|
2016-05-15 15:52:59 +02:00
|
|
|
this.timeout(5000);
|
2016-05-15 03:16:50 +02:00
|
|
|
testTaskchain = new taskbuffer.Taskchain({
|
|
|
|
name:"Taskchain1",
|
|
|
|
taskArray:testTaskArray
|
|
|
|
});
|
|
|
|
testTaskchain.trigger().then(done);
|
|
|
|
});
|
2016-02-12 04:49:31 +01:00
|
|
|
});
|
2016-08-01 13:17:15 +02:00
|
|
|
describe("taskparallel",function(){
|
|
|
|
let task1 = new taskbuffer.Task({
|
|
|
|
name:"Task 1",
|
|
|
|
taskFunction: () => {
|
|
|
|
let done = q.defer();
|
|
|
|
console.log("Task1 started");
|
|
|
|
setTimeout(() => {
|
|
|
|
console.log("Task1 executed");
|
|
|
|
done.resolve();
|
|
|
|
},5000)
|
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let task2 = new taskbuffer.Task({
|
|
|
|
name:"Task 1",
|
|
|
|
taskFunction: () => {
|
|
|
|
let done = q.defer();
|
|
|
|
console.log("Task2 started");
|
|
|
|
setTimeout(() => {
|
|
|
|
console.log("Task2 executed");
|
|
|
|
done.resolve();
|
|
|
|
},5000)
|
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let task3 = new taskbuffer.Task({
|
|
|
|
name:"Task 3",
|
|
|
|
taskFunction: () => {
|
|
|
|
let done = q.defer();
|
|
|
|
console.log("Task3 started");
|
|
|
|
setTimeout(() => {
|
|
|
|
console.log("Task3 executed");
|
|
|
|
done.resolve();
|
|
|
|
},5000)
|
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it("should run in Parallel",function(done){
|
|
|
|
this.timeout("7000");
|
|
|
|
let testTaskparallel = new taskbuffer.Taskparallel({
|
|
|
|
taskArray:[task1,task2,task3]
|
|
|
|
});
|
|
|
|
testTaskparallel.trigger().then(() => {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
})
|
2016-02-12 04:49:31 +01:00
|
|
|
});
|