taskbuffer/test/test.js

129 lines
9.6 KiB
JavaScript
Raw Normal View History

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-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-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);
});
});
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 () {
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 03:49:31 +00:00
});
2016-08-01 11:17:15 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdX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