diff --git a/src/core_plugins/elasticsearch/lib/__tests__/health_check.js b/src/core_plugins/elasticsearch/lib/__tests__/health_check.js index d6ffc5d1757b9..d4d6a0a51195f 100644 --- a/src/core_plugins/elasticsearch/lib/__tests__/health_check.js +++ b/src/core_plugins/elasticsearch/lib/__tests__/health_check.js @@ -24,13 +24,18 @@ describe('plugins/elasticsearch', () => { let plugin; let cluster; let server; + const sandbox = sinon.sandbox.create(); + + function getTimerCount() { + return Object.keys(sandbox.clock.timers || {}).length; + } beforeEach(() => { const COMPATIBLE_VERSION_NUMBER = '5.0.0'; // Stub the Kibana version instead of drawing from package.json. - sinon.stub(kibanaVersion, 'get').returns(COMPATIBLE_VERSION_NUMBER); - sinon.stub(ensureTypesExistNS, 'ensureTypesExist'); + sandbox.stub(kibanaVersion, 'get').returns(COMPATIBLE_VERSION_NUMBER); + sandbox.stub(ensureTypesExistNS, 'ensureTypesExist'); // setup the plugin stub plugin = { @@ -58,7 +63,7 @@ describe('plugins/elasticsearch', () => { } })); - sinon.stub(determineEnabledScriptingLangsNS, 'determineEnabledScriptingLangs').returns(Promise.resolve([])); + sandbox.stub(determineEnabledScriptingLangsNS, 'determineEnabledScriptingLangs').returns(Promise.resolve([])); // setup the config().get()/.set() stubs const get = sinon.stub(); @@ -82,16 +87,37 @@ describe('plugins/elasticsearch', () => { savedObjectsClientFactory: () => ({ find: sinon.stub().returns(Promise.resolve({ saved_objects: [] })), create: sinon.stub().returns(Promise.resolve({ id: 'foo' })), - }) + }), + ext: sinon.stub() }; health = healthCheck(plugin, server, { mappings }); }); afterEach(() => { - kibanaVersion.get.restore(); - determineEnabledScriptingLangs.restore(); - ensureTypesExistNS.ensureTypesExist.restore(); + sandbox.restore(); + }); + + it('should stop when cluster is shutdown', () => { + sandbox.useFakeTimers(); + + // ensure that health.start() is responsible for the timer we are observing + expect(getTimerCount()).to.be(0); + health.start(); + expect(getTimerCount()).to.be(1); + + // ensure that a server extension was registered + sinon.assert.calledOnce(server.ext); + sinon.assert.calledWithExactly(server.ext, sinon.match.string, sinon.match.func); + + // call the server extension + const reply = sinon.stub(); + const [,handler] = server.ext.firstCall.args; + handler({}, reply); + + // ensure that the handler called reply and unregistered the time + sinon.assert.calledOnce(reply); + expect(getTimerCount()).to.be(0); }); it('should set the cluster green if everything is ready', function () { diff --git a/src/core_plugins/elasticsearch/lib/health_check.js b/src/core_plugins/elasticsearch/lib/health_check.js index 579b112bfc8c7..c54ed7eca0915 100644 --- a/src/core_plugins/elasticsearch/lib/health_check.js +++ b/src/core_plugins/elasticsearch/lib/health_check.js @@ -152,6 +152,11 @@ module.exports = function (plugin, server, { mappings }) { return true; } + server.ext('onPreStop', (request, reply) => { + stopChecking(); + reply(); + }); + return { waitUntilReady: waitUntilReady, run: check,