diff --git a/x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.test.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.test.ts index 831bf45cf72ea..b7147fe0a9ebd 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.test.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.test.ts @@ -7,17 +7,13 @@ import expect from '@kbn/expect'; import sinon from 'sinon'; import { CancellationToken } from '../../../../common'; -import { LevelLogger } from '../../../lib'; +import { createMockLevelLogger } from '../../../test_helpers/create_mock_levellogger'; import { ScrollConfig } from '../../../types'; import { createHitIterator } from './hit_iterator'; -const mockLogger = { - error: new Function(), - debug: new Function(), - warning: new Function(), -} as LevelLogger; +const mockLogger = createMockLevelLogger(); const debugLogStub = sinon.stub(mockLogger, 'debug'); -const warnLogStub = sinon.stub(mockLogger, 'warning'); +const warnLogStub = sinon.stub(mockLogger, 'warn'); const errorLogStub = sinon.stub(mockLogger, 'error'); const mockCallEndpoint = sinon.stub(); const mockSearchRequest = {}; @@ -134,4 +130,30 @@ describe('hitIterator', function () { expect(errorLogStub.callCount).to.be(1); expect(errorThrown).to.be(true); }); + + it('handles scroll id could not be cleared', async () => { + // Setup + mockCallEndpoint.withArgs('clearScroll').rejects({ status: 404 }); + + // Begin + const hitIterator = createHitIterator(mockLogger); + const iterator = hitIterator( + mockConfig, + mockCallEndpoint, + mockSearchRequest, + realCancellationToken + ); + + while (true) { + const { done: iterationDone, value: hit } = await iterator.next(); + if (iterationDone) { + break; + } + expect(hit).to.be('you found me'); + } + + expect(mockCallEndpoint.callCount).to.be(13); + expect(warnLogStub.callCount).to.be(1); + expect(errorLogStub.callCount).to.be(1); + }); }); diff --git a/x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.ts index dee653cf30007..b95a311200266 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.ts @@ -68,11 +68,17 @@ export function createHitIterator(logger: LevelLogger) { ); } - function clearScroll(scrollId: string | undefined) { + async function clearScroll(scrollId: string | undefined) { logger.debug('executing clearScroll request'); - return callEndpoint('clearScroll', { - scrollId: [scrollId], - }); + try { + await callEndpoint('clearScroll', { + scrollId: [scrollId], + }); + } catch (err) { + // Do not throw the error, as the job can still be completed successfully + logger.warn('Scroll context can not be cleared!'); + logger.error(err); + } } try { @@ -86,7 +92,7 @@ export function createHitIterator(logger: LevelLogger) { ({ scrollId, hits } = await scroll(scrollId)); if (cancellationToken.isCancelled()) { - logger.warning( + logger.warn( 'Any remaining scrolling searches have been cancelled by the cancellation token.' ); }