From 14195f29c010363237d1c4338861c8cdd6e4b760 Mon Sep 17 00:00:00 2001 From: Justin Bourrousse Date: Thu, 8 Nov 2018 17:36:55 +0100 Subject: [PATCH] feat: add prettier and format files --- .gitignore | 3 +- .prettierrc | 5 + index.js | 2 +- package.json | 4 +- test/keys/queue.js | 12 +- test/keys/worker.js | 8 +- test/parsers/json.js | 28 +- test/queue.js | 628 +++++++++++++++++++++++-------------------- test/redis.js | 22 +- test/taskman.js | 22 +- test/util.js | 14 +- test/worker.js | 228 ++++++++-------- yarn.lock | 4 + 13 files changed, 527 insertions(+), 453 deletions(-) create mode 100644 .prettierrc diff --git a/.gitignore b/.gitignore index 40b878d..104d999 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules/ \ No newline at end of file +node_modules/ +tmp/ \ No newline at end of file diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..a0627b1 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "singleQuote": true, + "trailingComma": "es5", + "semi": true +} diff --git a/index.js b/index.js index b2d6d07..4a484a8 100644 --- a/index.js +++ b/index.js @@ -2,4 +2,4 @@ * Expose module. */ -module.exports = require('./lib/taskman'); \ No newline at end of file +module.exports = require('./lib/taskman'); diff --git a/package.json b/package.json index 4728efa..b3902a4 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "url": "https://github.com/neoziro/node-taskman/issues" }, "scripts": { - "test": "mocha --recursive" + "test": "mocha --recursive", + "format": "prettier --write '{lib,test,tmp}/**/*.js' '*.js'" }, "dependencies": { "async": "^2.1.4", @@ -33,6 +34,7 @@ "devDependencies": { "chai": "^3.5.0", "mocha": "^3.2.0", + "prettier": "1.15.1", "sinon": "^1.17.6", "sinon-chai": "^2.8.0" }, diff --git a/test/keys/queue.js b/test/keys/queue.js index f358fa2..840b50c 100644 --- a/test/keys/queue.js +++ b/test/keys/queue.js @@ -1,16 +1,16 @@ var queueKey = require('../../lib/keys/queue'); var expect = require('chai').expect; -describe('Queue key', function () { - describe('#formatList', function () { - it('should format key', function () { +describe('Queue key', function() { + describe('#formatList', function() { + it('should format key', function() { expect(queueKey.formatList('test')).to.equal('queue:test'); }); }); - describe('#formatSet', function () { - it('should format key', function () { + describe('#formatSet', function() { + it('should format key', function() { expect(queueKey.formatSet('test')).to.equal('unique:test'); }); }); -}); \ No newline at end of file +}); diff --git a/test/keys/worker.js b/test/keys/worker.js index 422b626..db10d64 100644 --- a/test/keys/worker.js +++ b/test/keys/worker.js @@ -1,10 +1,10 @@ var workerKey = require('../../lib/keys/worker'); var expect = require('chai').expect; -describe('Worker key', function () { - describe('#formatHash', function () { - it('should format key', function () { +describe('Worker key', function() { + describe('#formatHash', function() { + it('should format key', function() { expect(workerKey.formatHash('test', 'w1')).to.equal('worker:test:w1'); }); }); -}); \ No newline at end of file +}); diff --git a/test/parsers/json.js b/test/parsers/json.js index 36a077e..92a3031 100644 --- a/test/parsers/json.js +++ b/test/parsers/json.js @@ -1,23 +1,23 @@ var parser = require('../../lib/parsers/json'); var expect = require('chai').expect; -describe('JSON parser', function () { - describe('#format', function () { - it('should catch errors', function (done) { +describe('JSON parser', function() { + describe('#format', function() { + it('should catch errors', function(done) { // Create a circular structure. var obj = {}; obj.obj = obj; - parser.format(obj, function (err) { + parser.format(obj, function(err) { expect(err).to.exists; done(); }); }); - it('should format data', function (done) { - var obj = {foo: 'bar'}; + it('should format data', function(done) { + var obj = { foo: 'bar' }; - parser.format(obj, function (err, data) { + parser.format(obj, function(err, data) { if (err) return done(err); expect(data).to.equal('{"foo":"bar"}'); done(); @@ -25,20 +25,20 @@ describe('JSON parser', function () { }); }); - describe('#parse', function () { - it('should catch errors', function (done) { - parser.parse('{foo:bar}', function (err) { + describe('#parse', function() { + it('should catch errors', function(done) { + parser.parse('{foo:bar}', function(err) { expect(err).to.exists; done(); }); }); - it('should parse data', function (done) { - parser.parse('{"foo":"bar"}', function (err, data) { + it('should parse data', function(done) { + parser.parse('{"foo":"bar"}', function(err, data) { if (err) return done(err); - expect(data).to.eql({foo: 'bar'}); + expect(data).to.eql({ foo: 'bar' }); done(); }); }); }); -}); \ No newline at end of file +}); diff --git a/test/queue.js b/test/queue.js index 2686835..91f1048 100644 --- a/test/queue.js +++ b/test/queue.js @@ -3,366 +3,414 @@ var sinon = require('sinon'); var async = require('async'); var taskman = require('../'); -describe('Taskman queue', function () { - describe('non unique', function () { +describe('Taskman queue', function() { + describe('non unique', function() { var queue, queue2; - beforeEach(function () { - queue = taskman.createQueue('myQueue', {unique: false}); - queue2 = taskman.createQueue('myQueue', {unique: false}); + beforeEach(function() { + queue = taskman.createQueue('myQueue', { unique: false }); + queue2 = taskman.createQueue('myQueue', { unique: false }); }); - beforeEach(function (done) { + beforeEach(function(done) { queue.redis.flushdb(done); }); - describe('#push', function () { - it('should push a new string in the queue', function (done) { - async.series([ - function push(next) { - queue.push('test', next); - }, - function checkData(next) { - queue.redis.lpop('queue:myQueue', function (err, data) { - if (err) return next(err); - expect(data).to.equal('"test"'); - next(); - }); - } - ], done); + describe('#push', function() { + it('should push a new string in the queue', function(done) { + async.series( + [ + function push(next) { + queue.push('test', next); + }, + function checkData(next) { + queue.redis.lpop('queue:myQueue', function(err, data) { + if (err) return next(err); + expect(data).to.equal('"test"'); + next(); + }); + }, + ], + done + ); }); - it('should push a new object in the queue', function (done) { - async.series([ - function push(next) { - queue.push({foo: 'bar'}, next); - }, - function checkData(next) { - queue.redis.lpop('queue:myQueue', function (err, data) { - if (err) return next(err); - expect(data).to.equal('{"foo":"bar"}'); - next(); - }); - } - ], done); + it('should push a new object in the queue', function(done) { + async.series( + [ + function push(next) { + queue.push({ foo: 'bar' }, next); + }, + function checkData(next) { + queue.redis.lpop('queue:myQueue', function(err, data) { + if (err) return next(err); + expect(data).to.equal('{"foo":"bar"}'); + next(); + }); + }, + ], + done + ); }); - it('should emit a "created" event', function (done) { + it('should emit a "created" event', function(done) { var spy = sinon.spy(); queue.on('created', spy); - queue.push({foo: 'bar'}, function (err) { + queue.push({ foo: 'bar' }, function(err) { if (err) return done(err); - expect(spy).to.be.calledWith({foo: 'bar'}); + expect(spy).to.be.calledWith({ foo: 'bar' }); done(); }); }); }); - describe('#pop', function () { - it('should pop a string from the queue', function (done) { - async.series([ - function push(next) { - queue.redis.rpush('queue:myQueue', '"test"', next); - }, - function pop(next) { - queue.pop('fifo', 1, function (err, data) { - if (err) return next(err); - expect(data).to.eql(['test']); - next(); - }); - } - ], done); + describe('#pop', function() { + it('should pop a string from the queue', function(done) { + async.series( + [ + function push(next) { + queue.redis.rpush('queue:myQueue', '"test"', next); + }, + function pop(next) { + queue.pop('fifo', 1, function(err, data) { + if (err) return next(err); + expect(data).to.eql(['test']); + next(); + }); + }, + ], + done + ); }); - it('should pop an object from the queue', function (done) { - async.series([ - function push(next) { - queue.redis.rpush('queue:myQueue', '{"foo":"bar"}', next); - }, - function pop(next) { - queue.pop('fifo', 1, function (err, data) { - if (err) return next(err); - expect(data).to.eql([{foo: 'bar'}]); - next(); - }); - } - ], done); + it('should pop an object from the queue', function(done) { + async.series( + [ + function push(next) { + queue.redis.rpush('queue:myQueue', '{"foo":"bar"}', next); + }, + function pop(next) { + queue.pop('fifo', 1, function(err, data) { + if (err) return next(err); + expect(data).to.eql([{ foo: 'bar' }]); + next(); + }); + }, + ], + done + ); }); - it('should work on an empty queue', function (done) { - queue.pop('fifo', 1, function (err, data) { + it('should work on an empty queue', function(done) { + queue.pop('fifo', 1, function(err, data) { if (err) return done(err); - expect(data).to.eql([{foo: 'bar'}]); + expect(data).to.eql([{ foo: 'bar' }]); done(); }); - setTimeout(function () { + setTimeout(function() { queue2.redis.rpush('queue:myQueue', ['{"foo":"bar"}']); }, 10); }); - it('should fetch multiple values', function (done) { - async.series([ - function push(next) { - queue.redis.rpush('queue:myQueue', 1, next); - }, - function push(next) { - queue.redis.rpush('queue:myQueue', 2, next); - }, - function pop(next) { - queue.pop('fifo', 3, function (err, data) { - if (err) return next(err); - expect(data).to.eql([1, 2, null]); - next(); - }); - } - ], done); + it('should fetch multiple values', function(done) { + async.series( + [ + function push(next) { + queue.redis.rpush('queue:myQueue', 1, next); + }, + function push(next) { + queue.redis.rpush('queue:myQueue', 2, next); + }, + function pop(next) { + queue.pop('fifo', 3, function(err, data) { + if (err) return next(err); + expect(data).to.eql([1, 2, null]); + next(); + }); + }, + ], + done + ); }); }); }); - describe('unique', function () { + describe('unique', function() { var queue; - beforeEach(function () { - queue = taskman.createQueue('myQueue', {unique: true}); + beforeEach(function() { + queue = taskman.createQueue('myQueue', { unique: true }); }); - beforeEach(function (done) { + beforeEach(function(done) { queue.redis.flushdb(done); }); - describe('#push', function () { - it('should push a new string in the queue', function (done) { - async.series([ - function push(next) { - queue.push('test', next); - }, - function checkQueue(next) { - queue.redis.lpop('queue:myQueue', function (err, data) { - if (err) return next(err); - expect(data).to.equal('"test"'); - next(); - }); - }, - function check(next) { - queue.redis.sismember('unique:myQueue', '"test"', function (err, exists) { - if (err) return next(err); - expect(exists).to.equal(1); - next(); - }); - } - ], done); + describe('#push', function() { + it('should push a new string in the queue', function(done) { + async.series( + [ + function push(next) { + queue.push('test', next); + }, + function checkQueue(next) { + queue.redis.lpop('queue:myQueue', function(err, data) { + if (err) return next(err); + expect(data).to.equal('"test"'); + next(); + }); + }, + function check(next) { + queue.redis.sismember('unique:myQueue', '"test"', function( + err, + exists + ) { + if (err) return next(err); + expect(exists).to.equal(1); + next(); + }); + }, + ], + done + ); }); - it('should not push if it\'s already in the queue', function (done) { - async.series([ - function firstPush(next) { - queue.push('test', next); - }, - function secondPush(next) { - queue.push('test', next); - }, - function checkQueue(next) { - queue.redis.llen('queue:myQueue', function (err, data) { - if (err) return next(err); - expect(data).to.equal(1); - next(); - }); - } - ], done); + it("should not push if it's already in the queue", function(done) { + async.series( + [ + function firstPush(next) { + queue.push('test', next); + }, + function secondPush(next) { + queue.push('test', next); + }, + function checkQueue(next) { + queue.redis.llen('queue:myQueue', function(err, data) { + if (err) return next(err); + expect(data).to.equal(1); + next(); + }); + }, + ], + done + ); }); - it('should work even if we push in parallel', function (done) { - var queue1 = taskman.createQueue('myQueue', {unique: true}); - var queue2 = taskman.createQueue('myQueue', {unique: true}); - - async.parallel([ - function firstPush(next) { - queue1.push('test1', next); - }, - function secondPush(next) { - queue2.push('test2', next); - }, - function secondPush(next) { - queue.push('test', next); - } - ], function (err) { - if (err) return done(err); + it('should work even if we push in parallel', function(done) { + var queue1 = taskman.createQueue('myQueue', { unique: true }); + var queue2 = taskman.createQueue('myQueue', { unique: true }); - queue.redis.llen('queue:myQueue', function (err, data) { + async.parallel( + [ + function firstPush(next) { + queue1.push('test1', next); + }, + function secondPush(next) { + queue2.push('test2', next); + }, + function secondPush(next) { + queue.push('test', next); + }, + ], + function(err) { if (err) return done(err); - expect(data).to.equal(3); - done(); - }); - }); - }); - }); - describe('#pop', function () { - it('should pop a string from the queue', function (done) { - async.series([ - function pushList(next) { - queue.redis.rpush('queue:myQueue', '"test"', next); - }, - function addToSet(next) { - queue.redis.sadd('unique:myQueue', '"test"', next); - }, - function pop(next) { - queue.pop('fifo', 1, function (err, data) { - if (err) return next(err); - expect(data).to.eql(['test']); - next(); - }); - }, - function checkList(next) { - queue.redis.llen('queue:myQueue', function (err, count) { - if (err) return next(err); - expect(count).to.equal(0); - next(); - }); - }, - function checkSet(next) { - queue.redis.sismember('unique:myQueue', '"test"', function (err, exists) { - if (err) return next(err); - expect(exists).to.equal(0); - next(); + queue.redis.llen('queue:myQueue', function(err, data) { + if (err) return done(err); + expect(data).to.equal(3); + done(); }); } - ], done); + ); }); + }); - it('should return null if queue is empty', function (done) { - async.series([ - function pop(next) { - queue.pop('fifo', 1, function (err, data) { - if (err) return next(err); - expect(data).to.eql(null); - next(); - }); - }, - function checkList(next) { - queue.redis.llen('queue:myQueue', function (err, count) { - if (err) return next(err); - expect(count).to.equal(0); - next(); - }); - }, - function checkSet(next) { - queue.redis.sismember('unique:myQueue', '"test"', function (err, exists) { - if (err) return next(err); - expect(exists).to.equal(0); - next(); - }); - } - ], done); + describe('#pop', function() { + it('should pop a string from the queue', function(done) { + async.series( + [ + function pushList(next) { + queue.redis.rpush('queue:myQueue', '"test"', next); + }, + function addToSet(next) { + queue.redis.sadd('unique:myQueue', '"test"', next); + }, + function pop(next) { + queue.pop('fifo', 1, function(err, data) { + if (err) return next(err); + expect(data).to.eql(['test']); + next(); + }); + }, + function checkList(next) { + queue.redis.llen('queue:myQueue', function(err, count) { + if (err) return next(err); + expect(count).to.equal(0); + next(); + }); + }, + function checkSet(next) { + queue.redis.sismember('unique:myQueue', '"test"', function( + err, + exists + ) { + if (err) return next(err); + expect(exists).to.equal(0); + next(); + }); + }, + ], + done + ); }); - it('should pop in the right direction (#fifo)', function (done) { - async.series([ - function push(next) { - queue.push('test1', next); - }, - function push(next) { - queue.push('test2', next); - }, - function pop(next) { - queue.pop('fifo', 1, function (err, data) { - if (err) return next(err); - expect(data).to.eql(['test1']); - next(); - }); - }, - function pop(next) { - queue.pop('fifo', 1, function (err, data) { - if (err) return next(err); - expect(data).to.eql(['test2']); - next(); - }); - } - ], function (err) { - if (err) return done(err); - done(); - }); + it('should return null if queue is empty', function(done) { + async.series( + [ + function pop(next) { + queue.pop('fifo', 1, function(err, data) { + if (err) return next(err); + expect(data).to.eql(null); + next(); + }); + }, + function checkList(next) { + queue.redis.llen('queue:myQueue', function(err, count) { + if (err) return next(err); + expect(count).to.equal(0); + next(); + }); + }, + function checkSet(next) { + queue.redis.sismember('unique:myQueue', '"test"', function( + err, + exists + ) { + if (err) return next(err); + expect(exists).to.equal(0); + next(); + }); + }, + ], + done + ); }); - it('should pop in the right direction (#lifo)', function (done) { - async.series([ - function push(next) { - queue.push('test1', next); - }, - function push(next) { - queue.push('test2', next); - }, - function pop(next) { - queue.pop('lifo', 1, function (err, data) { - if (err) return next(err); - expect(data).to.eql(['test2']); - next(); - }); - }, - function pop(next) { - queue.pop('lifo', 1, function (err, data) { - if (err) return next(err); - expect(data).to.eql(['test1']); - next(); - }); + it('should pop in the right direction (#fifo)', function(done) { + async.series( + [ + function push(next) { + queue.push('test1', next); + }, + function push(next) { + queue.push('test2', next); + }, + function pop(next) { + queue.pop('fifo', 1, function(err, data) { + if (err) return next(err); + expect(data).to.eql(['test1']); + next(); + }); + }, + function pop(next) { + queue.pop('fifo', 1, function(err, data) { + if (err) return next(err); + expect(data).to.eql(['test2']); + next(); + }); + }, + ], + function(err) { + if (err) return done(err); + done(); } - ], function (err) { - if (err) return done(err); - done(); - }); + ); }); - it('should fetch multiple values', function (done) { - async.series([ - function pushList(next) { - queue.redis.rpush('queue:myQueue', 1, next); - }, - function addToSet(next) { - queue.redis.sadd('unique:myQueue', 1, next); - }, - function pushList(next) { - queue.redis.rpush('queue:myQueue', 2, next); - }, - function addToSet(next) { - queue.redis.sadd('unique:myQueue', 2, next); - }, - function pop(next) { - queue.pop('fifo', 3, function (err, data) { - if (err) return next(err); - expect(data).to.eql([1, 2, null]); - next(); - }); - }, - function checkList(next) { - queue.redis.llen('queue:myQueue', function (err, count) { - if (err) return next(err); - expect(count).to.equal(0); - next(); - }); - }, - function checkSet(next) { - queue.redis.smembers('unique:myQueue', function (err, results) { - if (err) return next(err); - expect(results.length).to.equal(0); - next(); - }); + it('should pop in the right direction (#lifo)', function(done) { + async.series( + [ + function push(next) { + queue.push('test1', next); + }, + function push(next) { + queue.push('test2', next); + }, + function pop(next) { + queue.pop('lifo', 1, function(err, data) { + if (err) return next(err); + expect(data).to.eql(['test2']); + next(); + }); + }, + function pop(next) { + queue.pop('lifo', 1, function(err, data) { + if (err) return next(err); + expect(data).to.eql(['test1']); + next(); + }); + }, + ], + function(err) { + if (err) return done(err); + done(); } - ], done); + ); + }); + + it('should fetch multiple values', function(done) { + async.series( + [ + function pushList(next) { + queue.redis.rpush('queue:myQueue', 1, next); + }, + function addToSet(next) { + queue.redis.sadd('unique:myQueue', 1, next); + }, + function pushList(next) { + queue.redis.rpush('queue:myQueue', 2, next); + }, + function addToSet(next) { + queue.redis.sadd('unique:myQueue', 2, next); + }, + function pop(next) { + queue.pop('fifo', 3, function(err, data) { + if (err) return next(err); + expect(data).to.eql([1, 2, null]); + next(); + }); + }, + function checkList(next) { + queue.redis.llen('queue:myQueue', function(err, count) { + if (err) return next(err); + expect(count).to.equal(0); + next(); + }); + }, + function checkSet(next) { + queue.redis.smembers('unique:myQueue', function(err, results) { + if (err) return next(err); + expect(results.length).to.equal(0); + next(); + }); + }, + ], + done + ); }); }); }); - describe('#close', function () { - it('should close connection to redis', function (done) { + describe('#close', function() { + it('should close connection to redis', function(done) { var queue = taskman.createQueue('myQueue'); - queue.close(function (err) { + queue.close(function(err) { if (err) return done(err); expect(queue.redis.closing).to.be.true; done(); }); }); }); -}); \ No newline at end of file +}); diff --git a/test/redis.js b/test/redis.js index b0ab2a3..ada3a74 100644 --- a/test/redis.js +++ b/test/redis.js @@ -3,40 +3,40 @@ var expect = require('chai').use(require('sinon-chai')).expect; var redis = require('../lib/redis'); var legacyRedis = require('redis'); -describe('Redis', function () { - describe('#createClient', function () { - beforeEach(function () { +describe('Redis', function() { + describe('#createClient', function() { + beforeEach(function() { sinon.stub(legacyRedis, 'createClient').returns('redis client'); }); - afterEach(function () { + afterEach(function() { legacyRedis.createClient.restore(); }); - it('should create a new redis client from a function', function () { - var client = redis.createClient(function () { + it('should create a new redis client from a function', function() { + var client = redis.createClient(function() { return 'foo'; }); expect(client).to.equal('foo'); }); - it('should create a new redis client without options', function () { + it('should create a new redis client without options', function() { var client = redis.createClient(); expect(client).to.equal('redis client'); }); - it('should create a new redis client from options', function () { + it('should create a new redis client from options', function() { var client = redis.createClient({ host: 'localhost', port: 6379, - connect_timeout: 10 + connect_timeout: 10, }); expect(client).to.equal('redis client'); expect(legacyRedis.createClient).to.be.calledWith(6379, 'localhost', { - connect_timeout: 10 + connect_timeout: 10, }); }); }); -}); \ No newline at end of file +}); diff --git a/test/taskman.js b/test/taskman.js index 12108ae..0a5a026 100644 --- a/test/taskman.js +++ b/test/taskman.js @@ -3,40 +3,40 @@ var taskman = require('..'); var TQueue = require('../lib/queue'); var TWorker = require('../lib/worker'); -describe('Taskman', function () { - describe('#createQueue', function () { - it('should create a new queue without options', function () { +describe('Taskman', function() { + describe('#createQueue', function() { + it('should create a new queue without options', function() { var queue = taskman.createQueue('myQueue'); expect(queue).to.be.instanceOf(TQueue); }); - it('should create a new queue with options', function () { - var queue = taskman.createQueue('myQueue', {unique: true}); + it('should create a new queue with options', function() { + var queue = taskman.createQueue('myQueue', { unique: true }); expect(queue).to.be.instanceOf(TQueue); }); }); - describe('#createWorker', function () { + describe('#createWorker', function() { var queue; - beforeEach(function () { + beforeEach(function() { queue = taskman.createQueue('myQueue'); }); - it('should create a new worker without options', function () { + it('should create a new worker without options', function() { var worker = taskman.createWorker(queue); expect(worker).to.be.instanceOf(TWorker); }); - it('should create a new worker with options', function () { + it('should create a new worker with options', function() { var worker = taskman.createWorker(queue, { dataPerTick: 10, loopSleepTime: 1, name: 'test', pauseSleepTime: 1, - type: 'FIFO' + type: 'FIFO', }); expect(worker).to.be.instanceOf(TWorker); }); }); -}); \ No newline at end of file +}); diff --git a/test/util.js b/test/util.js index c1c3c61..e0c896a 100644 --- a/test/util.js +++ b/test/util.js @@ -3,17 +3,17 @@ var sinon = require('sinon'); var EventEmitter = require('events').EventEmitter; var util = require('../lib/util'); -describe('Util', function () { - describe('#wrapCallback', function () { +describe('Util', function() { + describe('#wrapCallback', function() { var emitter, onErrorSpy; - beforeEach(function () { + beforeEach(function() { onErrorSpy = sinon.spy(); emitter = new EventEmitter(); emitter.on('error', onErrorSpy); }); - it('should call callback if present', function () { + it('should call callback if present', function() { var callback = sinon.spy(); util.wrapCallback(emitter, callback)('err', 'res'); @@ -21,14 +21,14 @@ describe('Util', function () { expect(onErrorSpy).to.not.be.called; }); - it('should call emitter else', function () { + it('should call emitter else', function() { util.wrapCallback(emitter)('err'); expect(onErrorSpy).to.be.calledWith('err'); }); - it('should not call emitter if there is no error', function () { + it('should not call emitter if there is no error', function() { util.wrapCallback(emitter)(); expect(onErrorSpy).to.not.be.called; }); }); -}); \ No newline at end of file +}); diff --git a/test/worker.js b/test/worker.js index 357198a..b95b50d 100644 --- a/test/worker.js +++ b/test/worker.js @@ -5,75 +5,81 @@ var async = require('async'); var taskman = require('../'); var TQueue = require('../lib/queue'); -describe('Taskman worker', function () { - describe('constructor', function () { - it('should instantiate queue automatically', function () { - var worker = taskman.createWorker('test', {unique:true}); +describe('Taskman worker', function() { + describe('constructor', function() { + it('should instantiate queue automatically', function() { + var worker = taskman.createWorker('test', { unique: true }); expect(worker.queue).to.be.instanceOf(TQueue); expect(worker.queue).to.have.deep.property('options.unique', true); expect(worker.queue).to.have.property('name', 'test'); }); - it('should default name to hostname', function () { + it('should default name to hostname', function() { var worker = taskman.createWorker('test'); expect(worker).to.have.deep.property('options.name', os.hostname()); }); }); - describe('#set', function () { + describe('#set', function() { var worker, clock; - beforeEach(function () { - worker = taskman.createWorker('test', {name:'w'}); + beforeEach(function() { + worker = taskman.createWorker('test', { name: 'w' }); }); - beforeEach(function (done) { + beforeEach(function(done) { worker.redis.flushdb(done); }); - beforeEach(function () { + beforeEach(function() { clock = sinon.useFakeTimers(); }); - afterEach(function () { + afterEach(function() { clock.restore(); }); - it('should accept an object', function (done) { - async.series([ - function set(next) { - worker.set({foo: 'bar'}, next); - }, - function checkHash(next) { - worker.redis.hget('worker:test:w', 'foo', function (err, res) { - if (err) return next(err); - expect(res).to.equal('bar'); - next(); - }); - } - ], done); + it('should accept an object', function(done) { + async.series( + [ + function set(next) { + worker.set({ foo: 'bar' }, next); + }, + function checkHash(next) { + worker.redis.hget('worker:test:w', 'foo', function(err, res) { + if (err) return next(err); + expect(res).to.equal('bar'); + next(); + }); + }, + ], + done + ); }); - it('should set the updateAt', function (done) { - async.series([ - function set(next) { - worker.set({foo: 'bar'}, next); - }, - function checkHash(next) { - worker.redis.hget('worker:test:w', 'updatedAt', function (err, res) { - if (err) return next(err); - expect(res).to.equal('1970-01-01T00:00:00.000Z'); - next(); - }); - } - ], done); + it('should set the updateAt', function(done) { + async.series( + [ + function set(next) { + worker.set({ foo: 'bar' }, next); + }, + function checkHash(next) { + worker.redis.hget('worker:test:w', 'updatedAt', function(err, res) { + if (err) return next(err); + expect(res).to.equal('1970-01-01T00:00:00.000Z'); + next(); + }); + }, + ], + done + ); }); - it('should emit a "status change" event if the status change', function (done) { + it('should emit a "status change" event if the status change', function(done) { var spy = sinon.spy(); worker.status = 'waiting'; worker.on('status change', spy); - worker.set({status: 'working'}, function (err) { + worker.set({ status: 'working' }, function(err) { if (err) return done(err); expect(spy).to.be.calledWith('working'); done(); @@ -81,31 +87,31 @@ describe('Taskman worker', function () { }); }); - describe('#get', function () { + describe('#get', function() { var worker; - beforeEach(function () { - worker = taskman.createWorker('test', {name:'w'}); + beforeEach(function() { + worker = taskman.createWorker('test', { name: 'w' }); }); - beforeEach(function (done) { + beforeEach(function(done) { worker.redis.flushdb(done); }); - beforeEach(function (done) { - worker.redis.hmset('worker:test:w', {a: 'b', c: 'd'}, done); + beforeEach(function(done) { + worker.redis.hmset('worker:test:w', { a: 'b', c: 'd' }, done); }); - it('should get all data (no args)', function (done) { - worker.get(function (err, res) { + it('should get all data (no args)', function(done) { + worker.get(function(err, res) { if (err) return done(err); - expect(res).to.eql({a: 'b', c: 'd'}); + expect(res).to.eql({ a: 'b', c: 'd' }); done(); }); }); - it('should get one value', function (done) { - worker.get('a', function (err, res) { + it('should get one value', function(done) { + worker.get('a', function(err, res) { if (err) return done(err); expect(res).to.eql('b'); done(); @@ -113,29 +119,33 @@ describe('Taskman worker', function () { }); }); - describe('#fetch', function () { + describe('#fetch', function() { var worker; - beforeEach(function () { - worker = taskman.createWorker('test', {name:'w'}); + beforeEach(function() { + worker = taskman.createWorker('test', { name: 'w' }); }); - beforeEach(function (done) { + beforeEach(function(done) { worker.redis.flushdb(done); }); - beforeEach(function (done) { - worker.redis.hmset('worker:test:w', { - batch: 20, - ping: 4200, - sleep: 1000, - type: 'lifo', - status: 'working' - }, done); + beforeEach(function(done) { + worker.redis.hmset( + 'worker:test:w', + { + batch: 20, + ping: 4200, + sleep: 1000, + type: 'lifo', + status: 'working', + }, + done + ); }); - it('should update worker informations from redis', function (done) { - worker.fetch(function (err) { + it('should update worker informations from redis', function(done) { + worker.fetch(function(err) { if (err) return done(err); expect(worker).to.have.property('batch', 20); expect(worker).to.have.property('ping', 4200); @@ -145,11 +155,11 @@ describe('Taskman worker', function () { }); }); - it('should emit a "status change" event if the status change', function (done) { + it('should emit a "status change" event if the status change', function(done) { var spy = sinon.spy(); worker.status = 'waiting'; worker.on('status change', spy); - worker.fetch(function (err) { + worker.fetch(function(err) { if (err) return done(err); expect(spy).to.be.calledWith('working'); done(); @@ -157,29 +167,29 @@ describe('Taskman worker', function () { }); }); - describe('#process', function () { + describe('#process', function() { var worker, queue; - beforeEach(function () { - worker = taskman.createWorker('test', {name:'w'}); + beforeEach(function() { + worker = taskman.createWorker('test', { name: 'w' }); }); - beforeEach(function (done) { + beforeEach(function(done) { worker.redis.flushdb(done); }); - afterEach(function (done) { + afterEach(function(done) { worker.close(done); }); - afterEach(function (done) { + afterEach(function(done) { if (queue) queue.close(done); else done(); }); - it('should update data in redis', function (done) { - worker.process(function (tasks, next) { - worker.get(function (err, infos) { + it('should update data in redis', function(done) { + worker.process(function(tasks, next) { + worker.get(function(err, infos) { if (err) return done(err); expect(infos).to.have.property('createdAt'); expect(infos).to.have.property('pid', process.pid + ''); @@ -198,12 +208,12 @@ describe('Taskman worker', function () { worker.queue.push('x'); }); - it('should process tasks', function (done) { - worker = taskman.createWorker('test', {name: 'w', ping: 10000}); + it('should process tasks', function(done) { + worker = taskman.createWorker('test', { name: 'w', ping: 10000 }); queue = taskman.createQueue('test'); var c = 0; - worker.process(function (res, next) { + worker.process(function(res, next) { if (c === 0) expect(res).to.eql(['a']); if (c === 1) expect(res).to.eql(['b']); if (c === 2) done(); @@ -211,21 +221,25 @@ describe('Taskman worker', function () { next(); }); - async.series([ - queue.push.bind(queue, 'a'), - queue.push.bind(queue, 'b') - ], function (err) { - if (err) return done(err); - setTimeout(queue.push.bind(queue, 'c'), 50); - }); + async.series( + [queue.push.bind(queue, 'a'), queue.push.bind(queue, 'b')], + function(err) { + if (err) return done(err); + setTimeout(queue.push.bind(queue, 'c'), 50); + } + ); }); - it('should process unique tasks', function (done) { - worker = taskman.createWorker('test', {name: 'w', unique: true, ping: 10}); + it('should process unique tasks', function(done) { + worker = taskman.createWorker('test', { + name: 'w', + unique: true, + ping: 10, + }); queue = taskman.createQueue('test'); var c = 0; - worker.process(function (res, next) { + worker.process(function(res, next) { if (c === 0) expect(res).to.eql(['a']); if (c === 1) expect(res).to.eql(['b']); if (c === 2) done(); @@ -233,43 +247,43 @@ describe('Taskman worker', function () { next(); }); - async.series([ - queue.push.bind(queue, 'a'), - queue.push.bind(queue, 'b') - ], function (err) { - if (err) return done(err); - setTimeout(queue.push.bind(queue, 'c'), 50); - }); + async.series( + [queue.push.bind(queue, 'a'), queue.push.bind(queue, 'b')], + function(err) { + if (err) return done(err); + setTimeout(queue.push.bind(queue, 'c'), 50); + } + ); }); - it('should emit a "job failure" event if process returns an error', function (done) { + it('should emit a "job failure" event if process returns an error', function(done) { worker = taskman.createWorker('jobfailure'); queue = taskman.createQueue('jobfailure'); - worker.on('job failure', function (tasks, err) { + worker.on('job failure', function(tasks, err) { expect(tasks).to.eql(['task']); expect(err).to.be.instanceOf(Error); expect(err).to.have.property('message', 'error'); done(); }); - worker.process(function (tasks, next) { + worker.process(function(tasks, next) { next(new Error('error')); }); queue.push('task'); }); - it('should emit a "job complete" event if process does\'t return an error', function (done) { + it('should emit a "job complete" event if process does\'t return an error', function(done) { worker = taskman.createWorker('jobcomplete'); queue = taskman.createQueue('jobcomplete'); - worker.on('job complete', function (tasks) { + worker.on('job complete', function(tasks) { expect(tasks).to.eql(['task']); done(); }); - worker.process(function (tasks, next) { + worker.process(function(tasks, next) { next(); }); @@ -277,15 +291,15 @@ describe('Taskman worker', function () { }); }); - describe('#close', function () { + describe('#close', function() { var worker; - beforeEach(function () { + beforeEach(function() { worker = taskman.createWorker('test'); }); - it('should close connection to redis', function (done) { - worker.close(function (err) { + it('should close connection to redis', function(done) { + worker.close(function(err) { if (err) return done(err); expect(worker.queue.redis.closing).to.be.true; expect(worker.redis.closing).to.be.true; @@ -293,8 +307,8 @@ describe('Taskman worker', function () { }); }); - it('should be possible to not close the queue', function (done) { - worker.close({queue: false}, function (err) { + it('should be possible to not close the queue', function(done) { + worker.close({ queue: false }, function(err) { if (err) return done(err); expect(worker.queue.redis.closing).to.be.false; expect(worker.redis.closing).to.be.true; diff --git a/yarn.lock b/yarn.lock index 1899971..b479209 100644 --- a/yarn.lock +++ b/yarn.lock @@ -226,6 +226,10 @@ pipe-event@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/pipe-event/-/pipe-event-0.1.0.tgz#a5f5e03e5a97b2b7493d4b2a060cd83daccb9a61" +prettier@1.15.1: + version "1.15.1" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.15.1.tgz#06c67106afb1b40e74b002353b2079cc7e0e67bf" + redis-commands@^1.2.0: version "1.3.0" resolved "https://registry.yarnpkg.com/redis-commands/-/redis-commands-1.3.0.tgz#4307d8094aee1315829ab6729b37b99f62365d63"