From e6432c06dc2cfc524caaf43c86311dca2d019051 Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Fri, 1 Nov 2019 10:55:50 -0700 Subject: [PATCH] Fix aborting when searching without batching --- .../default_search_strategy.js | 5 ++- .../default_search_strategy.test.js | 35 +++++++++++++++++-- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/legacy/ui/public/courier/search_strategy/default_search_strategy.js b/src/legacy/ui/public/courier/search_strategy/default_search_strategy.js index 7d9865c137e62..42a9b64136454 100644 --- a/src/legacy/ui/public/courier/search_strategy/default_search_strategy.js +++ b/src/legacy/ui/public/courier/search_strategy/default_search_strategy.js @@ -66,10 +66,9 @@ function search({ searchRequests, es, config, esShardTimeout }) { const abortController = new AbortController(); const searchParams = getSearchParams(config, esShardTimeout); const promises = searchRequests.map(({ index, body }) => { - const searching = es.search({ index: index.title || index, body, ...searchParams }) - .catch(({ response }) => JSON.parse(response)); + const searching = es.search({ index: index.title || index, body, ...searchParams }); abortController.signal.addEventListener('abort', searching.abort); - return searching; + return searching.catch(({ response }) => JSON.parse(response)); }); return { searching: Promise.all(promises), diff --git a/src/legacy/ui/public/courier/search_strategy/default_search_strategy.test.js b/src/legacy/ui/public/courier/search_strategy/default_search_strategy.test.js index 953ca4fe800f1..a1ea53e8b5b47 100644 --- a/src/legacy/ui/public/courier/search_strategy/default_search_strategy.test.js +++ b/src/legacy/ui/public/courier/search_strategy/default_search_strategy.test.js @@ -27,16 +27,29 @@ function getConfigStub(config = {}) { }; } +const msearchMockResponse = Promise.resolve([]); +msearchMockResponse.abort = jest.fn(); +const msearchMock = jest.fn().mockReturnValue(msearchMockResponse); + +const searchMockResponse = Promise.resolve([]); +searchMockResponse.abort = jest.fn(); +const searchMock = jest.fn().mockReturnValue(searchMockResponse); + describe('defaultSearchStrategy', function () { describe('search', function () { let searchArgs; beforeEach(() => { - const msearchMock = jest.fn().mockReturnValue(Promise.resolve([])); - const searchMock = jest.fn().mockReturnValue(Promise.resolve([])); + msearchMockResponse.abort.mockClear(); + msearchMock.mockClear(); + + searchMockResponse.abort.mockClear(); + searchMock.mockClear(); searchArgs = { - searchRequests: [], + searchRequests: [{ + index: { title: 'foo' } + }], es: { msearch: msearchMock, search: searchMock, @@ -73,5 +86,21 @@ describe('defaultSearchStrategy', function () { await search(searchArgs); expect(searchArgs.es.msearch.mock.calls[0][0]).toHaveProperty('ignore_throttled', false); }); + + test('should properly call abort with msearch', () => { + searchArgs.config = getConfigStub({ + 'courier:batchSearches': true + }); + search(searchArgs).abort(); + expect(msearchMockResponse.abort).toHaveBeenCalled(); + }); + + test('should properly abort with search', async () => { + searchArgs.config = getConfigStub({ + 'courier:batchSearches': false + }); + search(searchArgs).abort(); + expect(searchMockResponse.abort).toHaveBeenCalled(); + }); }); });