Skip to content

Commit

Permalink
Fix aborting when searching without batching (elastic#49966)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasolson authored and rylnd committed Nov 13, 2019
1 parent 4205bc3 commit 78e8943
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
});
});
});

0 comments on commit 78e8943

Please sign in to comment.