Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Commit

Permalink
tests(queue, worker): add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Bourrousse committed May 28, 2014
1 parent 84c85fc commit 3335b91
Show file tree
Hide file tree
Showing 12 changed files with 743 additions and 9 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
@@ -0,0 +1,15 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]

indent_style = space
indent_size = 2

end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false
2 changes: 1 addition & 1 deletion .gitignore
@@ -1 +1 @@
node_modules/
node_modules/
53 changes: 53 additions & 0 deletions .jshintrc
@@ -0,0 +1,53 @@
{
"bitwise": true,
"camelcase": false,
"curly": false,
"eqeqeq": true,
"es3": false,
"forin": true,
"immed": true,
"indent": 2,
"latedef": "nofunc",
"newcap": true,
"noarg": true,
"noempty": true,
"nonew": false,
"plusplus": false,
"quotmark": true,
"regexp": false,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"maxparams": 5,
"maxdepth": 2,
"maxstatements": 30,
"maxcomplexity": 10,
"maxlen": 300,

"asi": false,
"boss": false,
"debug": false,
"eqnull": true,
"esnext": false,
"evil": false,
"expr": false,
"funcscope": false,
"globalstrict": false,
"iterator": false,
"lastsemic": false,
"laxbreak": false,
"laxcomma": false,
"loopfunc": false,
"moz": false,
"multistr": true,
"proto": false,
"scripturl": false,
"smarttabs": false,
"shadow": false,
"sub": false,
"supernew": false,
"validthis": true,

"node": true
}
12 changes: 12 additions & 0 deletions lib/driver/redis.js
@@ -1,3 +1,5 @@
'use strict';

var redis = require('redis');

/**
Expand Down Expand Up @@ -59,6 +61,16 @@ RedisDriver.prototype.hset = function (worker, key, data, callback) {
this.client.hset(this.getCompleteWorkerName(worker), key, data, callback);
};

/**
* Get a data in the worker
* @param string worker Worker
* @param string key Key
* @param function callback Callback
*/
RedisDriver.prototype.hget = function (worker, key, callback) {
this.client.hget(this.getCompleteWorkerName(worker), key, callback);
};

/**
* Get all property of a worker
* @param string worker Worker
Expand Down
2 changes: 2 additions & 0 deletions lib/queue.js
@@ -1,3 +1,5 @@
'use strict';

var _ = require('lodash');

/**
Expand Down
14 changes: 8 additions & 6 deletions lib/worker.js
@@ -1,3 +1,5 @@
'use strict';

var _ = require('lodash');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
Expand All @@ -18,7 +20,7 @@ var EventEmitter = require('events').EventEmitter;
*/
function Worker (queue, driver, name, action, options) {
EventEmitter.call(this);

this.options = _.extend({
type: 'FIFO',
loopSleepTime: 0,
Expand All @@ -33,7 +35,7 @@ function Worker (queue, driver, name, action, options) {
this.action = action;
this.completeName = this.queue.getName() + ':' + name;
this.initialized = false;
};
}

util.inherits(Worker, EventEmitter);

Expand Down Expand Up @@ -122,7 +124,7 @@ Worker.prototype.start = function (callback) {
this.setStatus(Worker.STATUS_STARTED, next);

var endDate = new Date(Date.now() + (this.options.expire * 1000)).toJSON();
this.setEndDate(endDate);
this.setEndDate(endDate, _.noop);
}, this));
};

Expand Down Expand Up @@ -207,14 +209,14 @@ Worker.prototype.setStatus = function (status, callback) {

var date = (new Date()).toJSON();

this.driver.hset(this.completeName, 'status_changedate', date);
this.driver.hset(this.completeName, 'status_changedate', date, _.noop);

if (status === Worker.STATUS_WORKING)
this.driver.hincrby(this.completeName, 'cpt_action', 1);

if (status === Worker.STATUS_STARTED) {
this.driver.hset(this.completeName, 'cpt_action', 0);
this.driver.hset(this.completeName, 'start_date', date);
this.driver.hset(this.completeName, 'cpt_action', 0, _.noop);
this.driver.hset(this.completeName, 'start_date', date, _.noop);
}
};

Expand Down
14 changes: 12 additions & 2 deletions package.json
Expand Up @@ -27,7 +27,17 @@
"dependencies": {
"redis": "~0.8.3",
"optimist": "~0.5.2",
"lodash": "~2.4.1"
"lodash": "^2.4.1",
"async": "^0.9.0",
"moment": "^2.6.0"
},
"devDependencies": {
"chai": "~1.7.2",
"mocha": "~1.13.0",
"sinon": "~1.7.3",
"sinon-chai": "~2.4.0",
"chai-things": "~0.2.0",
"proxyquire": "~0.5.2"
},
"licence": "MIT"
}
}
76 changes: 76 additions & 0 deletions test/.jshintrc
@@ -0,0 +1,76 @@
{
"bitwise": true,
"camelcase": false,
"curly": false,
"eqeqeq": true,
"es3": false,
"forin": true,
"immed": true,
"indent": 2,
"latedef": "nofunc",
"newcap": true,
"noarg": true,
"noempty": true,
"nonew": true,
"plusplus": false,
"quotmark": true,
"regexp": false,
"undef": true,
"unused": true,
"strict": false,
"trailing": true,
"maxparams": false,
"maxdepth": 2,
"maxstatements": 0,
"maxcomplexity": 10,
"maxlen": 110,
"globals": {
"describe": false,
"it": false,
"after": false,
"afterEach": false,
"before": false,
"beforeEach": false,
"define": false,
"xit": false,
"xdescribe": false,
"window": false,
"document": false,
"inject": false,
"mocha": false,
"$": false,
"requirejs": false,
"angular": false,
"sinon": true,
"expect": true,
"chai": true,
"e2e": true,
"app": true,
"localStorage": false
},
"asi": false,
"boss": false,
"debug": false,
"eqnull": true,
"esnext": false,
"evil": false,
"expr": true,
"funcscope": false,
"globalstrict": false,
"iterator": false,
"lastsemic": false,
"laxbreak": false,
"laxcomma": false,
"loopfunc": false,
"moz": false,
"multistr": true,
"proto": false,
"scripturl": false,
"smarttabs": false,
"shadow": false,
"sub": false,
"supernew": false,
"validthis": false,
"node": true,
"-W024": false
}
16 changes: 16 additions & 0 deletions test/bootstrap.js
@@ -0,0 +1,16 @@
// chai
chai = require('chai');
expect = chai.expect;
chai.Assertion.includeStack = true;

// sinon
sinon = require('sinon');
chai.use(require('sinon-chai'));
chai.use(require('chai-things'));

var base = __dirname + '/../lib/';

app = {
base: base,
config: require('./config')
};
9 changes: 9 additions & 0 deletions test/config.js
@@ -0,0 +1,9 @@
module.exports = {
redis: {
run: {
host: 'localhost',
port: 6379
},
db: 12
}
};

0 comments on commit 3335b91

Please sign in to comment.