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");
|
2016-08-02 09:09:42 +00:00
|
|
|
const 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 () {
|
2017-01-20 17:50:37 +00:00
|
|
|
should(testTask).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 () {
|
2017-01-20 17:50:37 +00:00
|
|
|
should(testTask.running).be.type("boolean");
|
|
|
|
should(testTask.running).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 () {
|
2017-01-20 17:50:37 +00:00
|
|
|
should(testTask.trigger()).be.Promise();
|
2016-05-06 00:05:45 +00:00
|
|
|
});
|
|
|
|
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) {
|
2017-01-20 20:36:18 +00:00
|
|
|
this.timeout(7000);
|
2016-08-01 11:17:15 +00:00
|
|
|
let testTaskparallel = new taskbuffer.Taskparallel({
|
|
|
|
taskArray: [task1, task2, task3]
|
|
|
|
});
|
|
|
|
testTaskparallel.trigger().then(() => {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-08-02 09:09:42 +00:00
|
|
|
describe("some flowtests", function () {
|
|
|
|
let flowTask1 = new taskbuffer.Task({
|
|
|
|
taskFunction: (x) => {
|
|
|
|
let done = q.defer();
|
|
|
|
console.log("flowTask1");
|
|
|
|
console.log(x);
|
|
|
|
done.resolve(x);
|
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
});
|
2016-08-03 12:51:43 +00:00
|
|
|
let flowTaskBuffered = new taskbuffer.Task({
|
|
|
|
taskFunction: (x) => {
|
|
|
|
let done = q.defer();
|
|
|
|
console.log("flowTask1");
|
|
|
|
console.log(x);
|
|
|
|
done.resolve(x);
|
|
|
|
return done.promise;
|
|
|
|
},
|
|
|
|
buffered: true,
|
|
|
|
bufferMax: 1
|
|
|
|
});
|
2016-08-02 09:09:42 +00:00
|
|
|
let flowTask2 = new taskbuffer.Task({
|
|
|
|
taskFunction: (x) => {
|
|
|
|
let done = q.defer();
|
|
|
|
console.log("flowTask2");
|
|
|
|
console.log(x);
|
|
|
|
done.resolve(x);
|
|
|
|
return done.promise;
|
|
|
|
},
|
|
|
|
preTask: flowTask1
|
|
|
|
});
|
|
|
|
let flowTask3 = new taskbuffer.Taskchain({
|
|
|
|
taskArray: [flowTask1, flowTask2]
|
|
|
|
});
|
|
|
|
it("should let a value flow through a task", function (done) {
|
|
|
|
flowTask1.trigger(12).then((x) => {
|
|
|
|
should.equal(x, 12);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
it("should let a values flow between tasks", function (done) {
|
|
|
|
flowTask2.trigger(12).then((x) => {
|
|
|
|
should.equal(x, 12);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2016-08-03 12:51:43 +00:00
|
|
|
it("should let a values flow between tasks when buffered", function (done) {
|
|
|
|
flowTaskBuffered.trigger(12).then((x) => {
|
|
|
|
should.equal(x, 12);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2016-08-02 09:09:42 +00:00
|
|
|
it("should let a values flow between tasks in Taskchain", function (done) {
|
|
|
|
flowTask3.trigger(12).then((x) => {
|
|
|
|
should.equal(x, 12);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
2016-02-12 03:49:31 +00:00
|
|
|
});
|
2017-01-20 20:36:18 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFzQjtBQUN0Qiw0Q0FBNkM7QUFDN0MsaUNBQWtDO0FBQ2xDLHVCQUF1QjtBQUV2QixtQ0FBbUM7QUFDbkMsSUFBSSxRQUF5QixDQUFDO0FBQzlCLElBQUksZ0JBQWdCLEdBQUc7SUFDbkIsSUFBSSxJQUFJLEdBQUcsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFDO0lBQ3JCLE9BQU8sQ0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