2016-05-05 17:30:24 +00:00
|
|
|
"use strict";
|
2016-08-01 11:17:15 +00:00
|
|
|
require("typings-test");
|
2016-07-31 22:53:27 +00:00
|
|
|
const taskbuffer = require("../dist/index");
|
|
|
|
let should = require("should");
|
2016-08-01 11:17:15 +00:00
|
|
|
const q = require("q");
|
2016-05-15 01:16:50 +00:00
|
|
|
// setup some testData to work with
|
2016-07-31 22:53:27 +00:00
|
|
|
let testTask;
|
|
|
|
let testTaskFunction = function () {
|
2016-08-01 11:17:15 +00:00
|
|
|
let done = q.defer();
|
2016-05-06 00:05:45 +00:00
|
|
|
console.log("main function executed!");
|
|
|
|
done.resolve();
|
|
|
|
return done.promise;
|
|
|
|
};
|
2016-08-01 14:10:00 +00:00
|
|
|
let testTaskFunctionTimeout = function () {
|
|
|
|
let done = q.defer();
|
|
|
|
console.log("main function started!");
|
|
|
|
setTimeout(() => {
|
|
|
|
console.log("main function ended!");
|
|
|
|
done.resolve();
|
|
|
|
}, 2000);
|
|
|
|
return done.promise;
|
|
|
|
};
|
2016-07-31 22:53:27 +00:00
|
|
|
let testPreTask = new taskbuffer.Task({
|
2016-05-14 02:28:22 +00:00
|
|
|
taskFunction: function () {
|
2016-08-01 11:17:15 +00:00
|
|
|
let done = q.defer();
|
2016-05-14 02:28:22 +00:00
|
|
|
console.log("preTask executed");
|
2016-08-01 11:17:15 +00:00
|
|
|
done.resolve();
|
|
|
|
return done.promise;
|
2016-05-14 02:28:22 +00:00
|
|
|
},
|
|
|
|
preTask: testTask
|
|
|
|
});
|
2016-08-01 14:10:00 +00:00
|
|
|
// some more tasks to wirj with
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// the actual tests
|
2016-05-04 00:49:43 +00:00
|
|
|
describe("taskbuffer", function () {
|
2016-05-06 00:05:45 +00:00
|
|
|
describe(".Task", function () {
|
|
|
|
it("new Task() should return a new task", function () {
|
|
|
|
testTask = new taskbuffer.Task({ taskFunction: testTaskFunction, preTask: testPreTask });
|
2016-05-04 00:49:43 +00:00
|
|
|
});
|
2016-05-05 17:30:24 +00:00
|
|
|
it("testTask should be and instance of Task", function () {
|
|
|
|
testTask.should.be.instanceof(taskbuffer.Task);
|
2016-05-04 00:49:43 +00:00
|
|
|
});
|
|
|
|
it("testTask.idle is true", function () {
|
|
|
|
if (!testTask.idle) {
|
|
|
|
throw new Error("testTask.idle is not true");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it("testTask.running is type boolean and initially false", function () {
|
|
|
|
testTask.running.should.be.type("boolean");
|
|
|
|
testTask.running.should.be.false();
|
2016-02-12 03:49:31 +00:00
|
|
|
});
|
2016-05-06 00:05:45 +00: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-15 01:16:50 +00:00
|
|
|
it("should run a task without pre and afterTask", function (done) {
|
2016-07-31 22:53:27 +00:00
|
|
|
let localTestTask = new taskbuffer.Task({ taskFunction: testTaskFunction });
|
2016-05-15 01:16:50 +00:00
|
|
|
localTestTask.trigger().then(done);
|
|
|
|
});
|
2016-08-01 14:10:00 +00:00
|
|
|
it("should run buffered", function (done) {
|
|
|
|
this.timeout(10000);
|
|
|
|
let localTestTask = new taskbuffer.Task({
|
|
|
|
taskFunction: testTaskFunctionTimeout,
|
|
|
|
buffered: true,
|
|
|
|
bufferMax: 2
|
|
|
|
});
|
|
|
|
localTestTask.trigger();
|
|
|
|
localTestTask.trigger();
|
|
|
|
localTestTask.trigger();
|
|
|
|
localTestTask.trigger().then(() => {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-05-15 01:16:50 +00:00
|
|
|
});
|
|
|
|
describe("Taskchain", function () {
|
2016-07-31 22:53:27 +00:00
|
|
|
let testTaskchain;
|
|
|
|
let testTaskArray = [
|
2016-05-15 01:16:50 +00:00
|
|
|
new taskbuffer.Task({
|
|
|
|
name: "task1",
|
|
|
|
taskFunction: function () {
|
2016-08-01 11:17:15 +00:00
|
|
|
let done = q.defer();
|
2016-05-15 13:52:59 +00:00
|
|
|
setTimeout(done.resolve, 2000);
|
2016-05-15 01:16:50 +00:00
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
new taskbuffer.Task({
|
|
|
|
name: "task2",
|
|
|
|
taskFunction: function () {
|
2016-08-01 11:17:15 +00:00
|
|
|
let done = q.defer();
|
2016-05-15 13:52:59 +00:00
|
|
|
setTimeout(done.resolve, 2000);
|
2016-05-15 01:16:50 +00:00
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
it("should run tasks in sequence", function (done) {
|
2016-05-15 13:52:59 +00:00
|
|
|
this.timeout(5000);
|
2016-05-15 01:16:50 +00:00
|
|
|
testTaskchain = new taskbuffer.Taskchain({
|
|
|
|
name: "Taskchain1",
|
|
|
|
taskArray: testTaskArray
|
|
|
|
});
|
|
|
|
testTaskchain.trigger().then(done);
|
|
|
|
});
|
2016-02-12 03:49:31 +00:00
|
|
|
});
|
2016-08-01 11:17:15 +00:00
|
|
|
describe("taskparallel", function () {
|
|
|
|
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 03:49:31 +00:00
|
|
|
});
|
2016-08-01 14:10:00 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsTUFBTyxVQUFVLFdBQVcsZUFBZSxDQUFDLENBQUM7QUFDN0MsSUFBSSxNQUFNLEdBQUcsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFDO0FBQy9CLE1BQU8sQ0FBQyxXQUFXLEdBQUcsQ0FBQyxDQUFBO0FBRXZCLG1DQUFtQztBQUNuQyxJQUFJLFFBQXlCLENBQUM7QUFDOUIsSUFBSSxnQkFBZ0IsR0FBRztJQUNuQixJQUFJLElBQUksR0FBRyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUM7SUFDckIsT0FBTyxDQUFDLEdBQUcsQ0FBQyx5QkFBeUIsQ0FBQyxDQUFBO0lBQ3RDLElBQUksQ0FBQyxPQUFPLEVBQUUsQ0FBQztJQUNmLE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDO0FBQ3hC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