Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix aborting when searching without batching #49966

Merged
merged 4 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});