Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasolson committed Nov 3, 2023
1 parent 8623348 commit aa3817f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/plugins/data/server/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,15 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
isStored: true,
};

return this.cancel(deps, searchId, searchOptions).catch(() => {
// Some strategies don't support cancellation - swallow these errors
});
try {
await this.cancel(deps, searchId, searchOptions);
} catch (e) {
// A 404 means either this search request does not exist, or that it is already cancelled
if (e.meta?.statusCode === 404) return;

// Log all other (unexpected) error messages
this.logger.error(`cancelSessionSearches error: ${e.message}`);
}
})
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getMockSearchConfig } from '../../../../config.mock';

const getMockEqlResponse = () => ({
body: {
id: 'my-search-id',
is_partial: false,
is_running: false,
took: 162,
Expand Down Expand Up @@ -54,13 +55,16 @@ describe('EQL search strategy', () => {
describe('search()', () => {
let mockEqlSearch: jest.Mock;
let mockEqlGet: jest.Mock;
let mockEqlDelete: jest.Mock;
let mockDeps: SearchStrategyDependencies;
let params: Required<EqlSearchStrategyRequest>['params'];
let options: Required<EqlSearchStrategyRequest>['options'];

beforeEach(() => {
mockEqlSearch = jest.fn().mockResolvedValueOnce(getMockEqlResponse());
mockEqlGet = jest.fn().mockResolvedValueOnce(getMockEqlResponse());
mockEqlDelete = jest.fn();

mockDeps = {
uiSettingsClient: {
get: jest.fn(),
Expand All @@ -70,6 +74,7 @@ describe('EQL search strategy', () => {
eql: {
get: mockEqlGet,
search: mockEqlSearch,
delete: mockEqlDelete,
},
},
},
Expand Down Expand Up @@ -124,6 +129,34 @@ describe('EQL search strategy', () => {
});
});

it('should delete when aborted', async () => {
const response = getMockEqlResponse();
mockEqlSearch.mockReset().mockResolvedValueOnce({
...response,
body: {
...response.body,
is_running: true,
},
});
const eqlSearch = await eqlSearchStrategyProvider(mockSearchConfig, mockLogger);
const abortController = new AbortController();
const abortSignal = abortController.signal;

// Abort after an incomplete first response is returned
setTimeout(() => abortController.abort(), 100);

let err: any;
try {
await eqlSearch.search({ options, params }, { abortSignal }, mockDeps).toPromise();
} catch (e) {
err = e;
}

expect(mockEqlSearch).toBeCalled();
expect(err).not.toBeUndefined();
expect(mockEqlDelete).toBeCalled();
});

describe('arguments', () => {
it('sends along async search options', async () => {
const eqlSearch = await eqlSearchStrategyProvider(mockSearchConfig, mockLogger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ export const enhancedEsSearchStrategyProvider = (

const cancel = () => {
if (!id || options.isStored) return;
cancelAsyncSearch(id, esClient).catch(() => {
// Swallow errors from cancellation
cancelAsyncSearch(id, esClient).catch((e) => {
// A 404 means either this search request does not exist, or that it is already cancelled
if (e.meta?.statusCode === 404) return;

// Log all other (unexpected) error messages
logger.error(`cancelAsyncSearch error: ${e.message}`);
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,33 @@ describe('SQL search strategy', () => {
signal: undefined,
});
});

it('should delete when aborted', async () => {
mockSqlQuery.mockResolvedValueOnce({
...mockSqlResponse,
body: {
...mockSqlResponse.body,
is_running: true,
},
});
const esSearch = await sqlSearchStrategyProvider(mockSearchConfig, mockLogger);
const abortController = new AbortController();
const abortSignal = abortController.signal;

// Abort after an incomplete first response is returned
setTimeout(() => abortController.abort(), 100);

let err: any;
try {
await esSearch.search({ params: {} }, { abortSignal }, mockDeps).toPromise();
} catch (e) {
err = e;
}

expect(mockSqlQuery).toBeCalled();
expect(err).not.toBeUndefined();
expect(mockSqlDelete).toBeCalled();
});
});

// skip until full search session support https://github.com/elastic/kibana/issues/127880
Expand Down

0 comments on commit aa3817f

Please sign in to comment.