Skip to content

Commit

Permalink
Make all available tests work
Browse files Browse the repository at this point in the history
  • Loading branch information
Doubl3 committed Feb 3, 2018
1 parent 8a558c5 commit eceec02
Showing 1 changed file with 123 additions and 45 deletions.
168 changes: 123 additions & 45 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,22 @@ var expect = require('chai').expect;
var role = 'scheduler';
seneca.use('../lib/scheduler.js');

/**
* Check scheduler 'task' has a 'job' inside
*/
checkTaskJob = function(task) {
if (task && task.job) {
return;
}
throw new Error('task is null or task doesn\'t owns any job');
}


suite('register');

/**
* 'for' mode with Date object
*/
test('the "for" argument accepting Date object', function (done) {
var registeredAt;

Expand All @@ -23,9 +37,6 @@ test('the "for" argument accepting Date object', function (done) {
for: moment().add(1, 'seconds').toDate(),
task: function () {
var now = Date.now();

// expect(now).to.be.closeTo(registeredAt + 1000, 500);

expect(now).to.be.above(registeredAt);
done();
}
Expand All @@ -37,8 +48,10 @@ test('the "for" argument accepting Date object', function (done) {
});


/**
* 'for' mode with Object describing a Date
*/
test('the "for" argument accepting object literal', function (done) {

var registeredAt;
var date = moment().add(1, 'seconds');

Expand All @@ -55,9 +68,6 @@ test('the "for" argument accepting object literal', function (done) {
},
task: function() {
var now = Date.now()

// expect(now).to.be.closeTo(registeredAt + 1000, 500);

expect(now).to.be.above(registeredAt);
done();
}
Expand All @@ -68,6 +78,10 @@ test('the "for" argument accepting object literal', function (done) {

});


/**
* 'for' mode with Date string
*/
test('the "for" argument accepting string', function (done) {

seneca.act({
Expand All @@ -83,6 +97,9 @@ test('the "for" argument accepting string', function (done) {
});


/**
* 'every' mode with Object containing recurring indications (here, trigger a task each hour)
*/
test('the "every" argument accepting object literal', function (done) {

seneca.act({
Expand All @@ -99,13 +116,21 @@ test('the "every" argument accepting object literal', function (done) {
task: function() {}
}, function (err, task) {
expect(err).to.be.null;
var nextCall = task.nextInvocation();
expect(function() {
checkTaskJob(task);
}).to.not.throw(Error);

var nextCall = task.job.nextInvocation();
expect(nextCall.getHours()).to.equal(1);
done();
});

});


/**
*
*/
test('the "every" argument with intuitive object literal', function (done) {

seneca.act({
Expand All @@ -118,8 +143,12 @@ test('the "every" argument with intuitive object literal', function (done) {
},
task: function() {}
}, function (err, task) {
var every = task.pattern.every;
expect(err).to.be.null;
expect(function() {
checkTaskJob(task);
}).to.not.throw(Error);

var every = task.job.pattern.every;

expect(every).to.have.property('hour');
expect(every).to.have.property('minute');
Expand All @@ -135,57 +164,86 @@ test('the "every" argument with intuitive object literal', function (done) {
});


/**
*
*/
test('the "every" argument accepting object literal containing cron style scheduling', function (done) {
var minutesToWait = 5;

seneca.act({
role: role,
cmd: 'register',
every: {
cron: '*/5 * * * *'
cron: '*/' + minutesToWait + ' * * * *'
},
name: 'cron-like scheduling',
task: function() {}
}, function (err, task) {
expect(err).to.be.null;
var nextCall = task.nextInvocation();
var nextTriggerInMinutes = 5 * Math.ceil( new Date().getMinutes() / 5 );
expect(function() {
checkTaskJob(task);
}).to.not.throw(Error);

var minutesToCheck = new Date().getMinutes() / minutesToWait;
if ((new Date().getMinutes() % minutesToWait) === 0) {
minutesToCheck = (new Date().getMinutes() + 1) / 5;
}

var nextTriggerInMinutes = minutesToWait * Math.ceil( minutesToCheck );
var nextCall = task.job.nextInvocation();
expect(nextCall.getMinutes()).to.equal(nextTriggerInMinutes);

done();
});

});



//TODO
// suite('update');
//
//TODO
// suite('pause');
//


suite('retrieve');

test('returns a task for a given id', function (done) {
var someTask = function () { };
var callbackResponse = 'Callback execution !!';
var someCallback = function () { return callbackResponse };
var taskName = 'A name for a task to retrieve';

seneca.act({
role: role,
cmd: 'register',
for: moment().add(1, 'years'),
task: someTask,
name: taskName,
task: function () { return 'task execution' },
callback: someCallback
}, function (err, task) {

expect(err).to.be.null;
expect(function() {
checkTaskJob(task);
}).to.not.throw(Error);

seneca.act({
role: role,
cmd: 'retrieve',
id: task.id
id: task.job.id,
}, function (err, retrievedTask) {
expect(err).to.be.null;
expect(retrievedTask).to.exist;
expect(retrievedTask.job).to.equal(someTask);
expect(retrievedTask).has.property('id');
expect(retrievedTask).has.property('name');
expect(function() {
checkTaskJob(task);
}).to.not.throw(Error);
var job = retrievedTask.job;

expect(job).has.property('id');
expect(job).has.property('pattern');
expect(job).has.property('callback');
expect(job.name).to.equal(taskName);
expect(job.callback).to.equal(someCallback);
expect(job.callback()).to.equal(callbackResponse);

done();

Expand All @@ -199,31 +257,42 @@ test('returns a task for a given id', function (done) {

suite('list');

test('outputs the ids of all registerd tasks', function (done) {
test('outputs the ids of all registered tasks', function (done) {
var ids = [];
seneca.act({
role: role,
cmd: 'register',
for: moment().add(1, 'years'),
task: function () {},
}, function (err, task) {
ids.push(task.id);
expect(err).to.be.null;
expect(function() {
checkTaskJob(task);
}).to.not.throw(Error);

ids.push(task.job.id);

seneca.act({
role: role,
cmd: 'register',
for: moment().add(1, 'years'),
task: function () {},
}, function (err, task) {
ids.push(task.id);
expect(err).to.be.null;
expect(function() {
checkTaskJob(task);
}).to.not.throw(Error);

ids.push(task.job.id);

seneca.act({
role: role,
cmd: 'list',
}, function (err, list) {
expect(err).to.be.null;
expect(list).to.contain(ids[0]);
expect(list).to.contain(ids[1]);

expect(list.list).to.contain(ids[0]);
expect(list.list).to.contain(ids[1]);
done();
});
});
Expand All @@ -244,20 +313,23 @@ test('cancels a task when given an id', function (done) {
for: moment().add(1, 'years'),
task: function () {},
}, function (err, task) {

expect(err).to.be.null;
expect(function() {
checkTaskJob(task);
}).to.not.throw(Error);

seneca.act({
role: role,
cmd: 'remove',
id: task.id
id: task.job.id
}, function (err) {
expect(err).to.be.null;

seneca.act({
role: role,
cmd: 'list'
}, function (err, list) {
expect(list).not.to.contain(task.id);
expect(list.list).not.to.contain(task.job.id);
done();
})

Expand All @@ -277,17 +349,25 @@ test('cancels multiple tasks when given an array of ids', function (done) {
for: moment().add(1, 'years'),
task: function () {},
}, function (err, task) {
expect(err).to.be.null;
expect(function() {
checkTaskJob(task);
}).to.not.throw(Error);

ids.push(task.id);
ids.push(task.job.id);

seneca.act({
role: role,
cmd: 'register',
for: moment().add(1, 'years'),
task: function () {},
}, function (err, task) {
expect(err).to.be.null;
expect(function() {
checkTaskJob(task);
}).to.not.throw(Error);

ids.push(task.id);
ids.push(task.job.id);

seneca.act({
role: role,
Expand All @@ -296,21 +376,16 @@ test('cancels multiple tasks when given an array of ids', function (done) {
}, function (err) {
expect(err).to.be.null;


seneca.act({
role: role,
cmd: 'list'
}, function (err, list) {
expect(list).not.to.contain(ids[0]);
expect(list).not.to.contain(ids[1]);
expect(list.list).not.to.contain(ids[0]);
expect(list.list).not.to.contain(ids[1]);
done();
})


});

});

});

});
Expand All @@ -327,34 +402,37 @@ test('removes all jobs', function (done) {
for: moment().add(1, 'years'),
task: function () {},
}, function (err, task) {
expect(err).to.be.null;
expect(function() {
checkTaskJob(task);
}).to.not.throw(Error);

seneca.act({
role: role,
cmd: 'register',
for: moment().add(1, 'years'),
task: function () {},
}, function (err, task) {

expect(err).to.be.null;
expect(function() {
checkTaskJob(task);
}).to.not.throw(Error);

seneca.act({
role: role,
cmd: 'clear',
}, function (err) {
expect(err).to.be.null;


seneca.act({
role: role,
cmd: 'list'
}, function (err, list) {
expect(list.length).to.equal(0);
expect(list.list.length).to.equal(0);
done();
})


});

});

});

})

0 comments on commit eceec02

Please sign in to comment.