From 3e082c0c7ed81eb4429237365ad10e1aff8286d1 Mon Sep 17 00:00:00 2001 From: Gabe Gorelick Date: Tue, 11 Jun 2019 15:28:21 -0500 Subject: [PATCH 1/2] fix: whenCurrentJobsFinished shouldn't initialize bclient Fixes #1346 --- lib/queue.js | 11 +++++++++++ test/test_pause.js | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/queue.js b/lib/queue.js index db61360aa..b642a8a38 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); } @@ -1139,6 +1145,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_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'); From 2f9b580e8ac2b12938a274b5c56d832e0afc15ba Mon Sep 17 00:00:00 2001 From: Gabe Gorelick Date: Wed, 12 Jun 2019 11:17:46 -0500 Subject: [PATCH 2/2] test: fix race condition in test_connection.js --- test/test_connection.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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' }); })