-
Notifications
You must be signed in to change notification settings - Fork 9
/
mock-kue.js
67 lines (55 loc) · 1.11 KB
/
mock-kue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
var kue = require('kue')
, Q = require('q');
var mockJobs = [];
var jobsProcessor = {};
kue.Job.prototype.save = function(fn){
this._state = this._state || 'inactive';
this.id = mockJobs.length;
mockJobs.push(this);
if(fn){
fn();
}
}
kue.prototype.process = function(type, n, fn){
if ('function' == typeof n) fn = n, n = 1;
jobsProcessor[type] = fn;
}
kue.jobCount = function(){
return mockJobs.length;
}
kue.getJobs = function () {
return mockJobs;
};
kue.drain = function(){
var promises = [];
var self = this;
while(mockJobs.length > 0){
var job = mockJobs.shift();
var fn = jobsProcessor[job.type];
if(fn){
var deferred = Q.defer();
var done = callback(job, deferred);
job._state = 'active';
fn(job, done);
promises.push(deferred.promise);
}
}
return Q.all(promises);
}
kue.clear = function(){
mockJobs = [];
}
var callback = function(job, deferred){
return function(err){
if(err){
job._state = 'failed';
deferred.reject(err);
}else{
job._progress = 100;
job._state = 'complete';
deferred.resolve();
}
};
}
module.exports = kue;