-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add cancel functionality to pollSearch Makes sure that DELETE requests are properly sent even if consumer unsubscribes. * Update poll_search.test.ts * cancel on abort * fix jest * ADded jest test * Cancel to be called once * Only cancel internally on abort * ts + addd defer * ts * make cancel code prettier * Cancel even after unsubscribe * Throw abort error if already aborted * Only delete once Co-authored-by: Lukas Olson <olson.lukas@gmail.com> Co-authored-by: Liza Katz <lizka.k@gmail.com>
- Loading branch information
1 parent
eba5539
commit 83de2af
Showing
6 changed files
with
187 additions
and
32 deletions.
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
x-pack/plugins/data_enhanced/common/search/poll_search.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { pollSearch } from './poll_search'; | ||
import { AbortError } from '../../../../../src/plugins/kibana_utils/common'; | ||
|
||
describe('pollSearch', () => { | ||
function getMockedSearch$(resolveOnI = 1, finishWithError = false) { | ||
let counter = 0; | ||
return jest.fn().mockImplementation(() => { | ||
counter++; | ||
const lastCall = counter === resolveOnI; | ||
return new Promise((resolve) => { | ||
if (lastCall) { | ||
resolve({ | ||
isRunning: false, | ||
isPartial: finishWithError, | ||
}); | ||
} else { | ||
resolve({ | ||
isRunning: true, | ||
isPartial: true, | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
test('Defers execution', async () => { | ||
const searchFn = getMockedSearch$(1); | ||
const cancelFn = jest.fn(); | ||
pollSearch(searchFn, cancelFn); | ||
expect(searchFn).toBeCalledTimes(0); | ||
expect(cancelFn).toBeCalledTimes(0); | ||
}); | ||
|
||
test('Resolves immediatelly', async () => { | ||
const searchFn = getMockedSearch$(1); | ||
const cancelFn = jest.fn(); | ||
await pollSearch(searchFn, cancelFn).toPromise(); | ||
expect(searchFn).toBeCalledTimes(1); | ||
expect(cancelFn).toBeCalledTimes(0); | ||
}); | ||
|
||
test('Resolves when complete', async () => { | ||
const searchFn = getMockedSearch$(3); | ||
const cancelFn = jest.fn(); | ||
await pollSearch(searchFn, cancelFn).toPromise(); | ||
expect(searchFn).toBeCalledTimes(3); | ||
expect(cancelFn).toBeCalledTimes(0); | ||
}); | ||
|
||
test('Throws Error on ES error response', async () => { | ||
const searchFn = getMockedSearch$(2, true); | ||
const cancelFn = jest.fn(); | ||
const poll = pollSearch(searchFn, cancelFn).toPromise(); | ||
await expect(poll).rejects.toThrow(Error); | ||
expect(searchFn).toBeCalledTimes(2); | ||
expect(cancelFn).toBeCalledTimes(0); | ||
}); | ||
|
||
test('Throws AbortError on empty response', async () => { | ||
const searchFn = jest.fn().mockResolvedValue(undefined); | ||
const cancelFn = jest.fn(); | ||
const poll = pollSearch(searchFn, cancelFn).toPromise(); | ||
await expect(poll).rejects.toThrow(AbortError); | ||
expect(searchFn).toBeCalledTimes(1); | ||
expect(cancelFn).toBeCalledTimes(0); | ||
}); | ||
|
||
test('Throws AbortError and cancels on abort', async () => { | ||
const searchFn = getMockedSearch$(20); | ||
const cancelFn = jest.fn(); | ||
const abortController = new AbortController(); | ||
const poll = pollSearch(searchFn, cancelFn, { | ||
abortSignal: abortController.signal, | ||
}).toPromise(); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 500)); | ||
abortController.abort(); | ||
|
||
await expect(poll).rejects.toThrow(AbortError); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
|
||
expect(searchFn).toBeCalledTimes(1); | ||
expect(cancelFn).toBeCalledTimes(1); | ||
}); | ||
|
||
test("Stops, but doesn't cancel on unsubscribe", async () => { | ||
const searchFn = getMockedSearch$(20); | ||
const cancelFn = jest.fn(); | ||
const subscription = pollSearch(searchFn, cancelFn).subscribe(() => {}); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 500)); | ||
subscription.unsubscribe(); | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
|
||
expect(searchFn).toBeCalledTimes(1); | ||
expect(cancelFn).toBeCalledTimes(0); | ||
}); | ||
|
||
test('Calls cancel even when consumer unsubscribes', async () => { | ||
const searchFn = getMockedSearch$(20); | ||
const cancelFn = jest.fn(); | ||
const abortController = new AbortController(); | ||
const subscription = pollSearch(searchFn, cancelFn, { | ||
abortSignal: abortController.signal, | ||
}).subscribe(() => {}); | ||
subscription.unsubscribe(); | ||
abortController.abort(); | ||
|
||
expect(searchFn).toBeCalledTimes(1); | ||
expect(cancelFn).toBeCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters