diff --git a/lib/queue.js b/lib/queue.js index 2caddf5bd..346d3c89b 100755 --- a/lib/queue.js +++ b/lib/queue.js @@ -279,6 +279,12 @@ function redisClientGetter(queue, options, initCallback) { return connections[type]; } const client = (connections[type] = createClient(type, options.redis)); + + // Since connections are lazily initialized, we can't check queue.client + // without initializing a connection. So expose a boolean we can safely + // query. + queue[type + 'Initialized'] = true; + if (!options.createClient) { queue.clients.push(client); } @@ -1144,6 +1150,11 @@ Queue.prototype.clean = function(grace, type, limit) { */ Queue.prototype.whenCurrentJobsFinished = function() { return new Promise((resolve, reject) => { + if (!this.bclientInitialized) { + // bclient not yet initialized, so no jobs to wait for + return resolve(); + } + // // Force reconnection of blocking connection to abort blocking redis call immediately. // diff --git a/test/test_connection.js b/test/test_connection.js index 58ad1fc18..a01afc6e6 100644 --- a/test/test_connection.js +++ b/test/test_connection.js @@ -98,8 +98,15 @@ describe('connection', () => { const testQueue = utils.buildQueue('external connections', opts); - return testQueue - .isReady() + return new Promise(resolve => { + if (subscriber.status === 'ready') { + return resolve(); + } + subscriber.once('ready', resolve); + }) + .then(() => { + return testQueue.isReady(); + }) .then(() => { return testQueue.add({ foo: 'bar' }); }) diff --git a/test/test_pause.js b/test/test_pause.js index 832610b52..ec05068d9 100644 --- a/test/test_pause.js +++ b/test/test_pause.js @@ -6,6 +6,7 @@ const expect = require('chai').expect; const redis = require('ioredis'); const utils = require('./utils'); const delay = require('delay'); +const sinon = require('sinon'); describe('.pause', () => { let client; @@ -15,6 +16,7 @@ describe('.pause', () => { }); afterEach(() => { + sinon.restore(); return client.quit(); }); @@ -284,6 +286,17 @@ describe('.pause', () => { }); }); + it('should not initialize blocking client if not already initialized', async () => { + const createClient = sinon.spy(() => client); + const queue = utils.buildQueue('pause-queue', { createClient }); + + await queue.pause(true); + const bClientCalls = createClient + .getCalls() + .filter(c => c.args[0] === 'bclient'); + expect(bClientCalls).to.have.lengthOf(0); + }); + it('pauses fast when queue is drained', function(done) { this.timeout(10000); const queue = new Queue('test');