fix(feature-flags): ensure terminate closes redis connections too

This commit is contained in:
Phil Booth 2019-03-13 10:10:08 +00:00
Родитель 7f181de4c9
Коммит a98b6d478c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 36FBB106F9C32516
3 изменённых файлов: 57 добавлений и 5 удалений

Просмотреть файл

@ -90,7 +90,7 @@ function initialise (config, log, defaults) {
*/
async function get () {
try {
return await cache;
return await cache || defaults || {};
} catch (error) {
if (defaults) {
return defaults;
@ -101,13 +101,18 @@ function initialise (config, log, defaults) {
}
/**
* Terminate the refresh loop.
* Terminate the refresh loop
* and close all redis connections.
* Useful for e.g. terminating tests cleanly.
*
* @returns {Promise}
*/
function terminate () {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
return redis.close();
}
}

Просмотреть файл

@ -22,11 +22,12 @@ describe('feature-flags/index:', () => {
get: sinon.spy(() => new Promise((res, rej) => {
resolve = res;
reject = rej;
}))
})),
close: sinon.spy()
};
redisFactory = sinon.spy(() => redis);
log = {};
initialise = proxyquire('../feature-flags', {
initialise = proxyquire('../../feature-flags', {
'../redis': redisFactory
});
setImmediate(done);
@ -139,7 +140,7 @@ describe('feature-flags/index:', () => {
describe('terminate:', () => {
beforeEach(() => {
featureFlags.terminate();
return featureFlags.terminate();
});
it('called clearTimeout', () => {
@ -149,6 +150,11 @@ describe('feature-flags/index:', () => {
assert.equal(args[0], 'wibble');
});
it('called redis.close', () => {
assert.equal(redis.close.callCount, 1);
assert.lengthOf(redis.close.args[0], 0);
});
describe('terminate:', () => {
beforeEach(() => {
featureFlags.terminate();
@ -157,6 +163,10 @@ describe('feature-flags/index:', () => {
it('did not call clearTimeout a second time', () => {
assert.equal(clearTimeout.callCount, 1);
});
it('called redis.close', () => {
assert.equal(redis.close.callCount, 2);
});
});
});
});

Просмотреть файл

@ -0,0 +1,37 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
'use strict';
const { assert } = require('chai');
describe('featureFlags integration:', () => {
let config, log, featureFlags;
before(() => {
config = {
interval: 10000,
host: process.env.REDIS_HOST || '127.0.0.1',
port: process.env.REDIS_PORT || 6379,
maxConnections: process.env.REDIS_POOL_MAX_CONNECTIONS || 1,
minConnections: process.env.REDIS_POOL_MIN_CONNECTIONS || 1,
};
log = { info () {}, warn () {}, error () {} };
featureFlags = require(`../../feature-flags`)(config, log, {});
});
after(() => featureFlags.terminate());
describe('get:', () => {
let result;
before(async () => {
result = await featureFlags.get();
});
it('returned an object', () => {
assert.isObject(result);
});
});
});