-
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.
- Loading branch information
Showing
8 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
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
33 changes: 33 additions & 0 deletions
33
x-pack/plugins/task_manager/server/lib/timeout_promise_after.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,33 @@ | ||
/* | ||
* 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 { timeoutPromiseAfter } from './timeout_promise_after'; | ||
|
||
const delay = (ms: number, result: unknown) => | ||
new Promise((resolve) => setTimeout(() => resolve(result), ms)); | ||
|
||
const delayRejection = (ms: number, result: unknown) => | ||
new Promise((resolve, reject) => setTimeout(() => reject(result), ms)); | ||
|
||
describe('Promise Timeout', () => { | ||
test('resolves when wrapped promise resolves', async () => { | ||
return expect(timeoutPromiseAfter(delay(100, 'OK'), 1000)).resolves.toMatchInlineSnapshot( | ||
`"OK"` | ||
); | ||
}); | ||
|
||
test('reject when wrapped promise rejects', async () => { | ||
return expect( | ||
timeoutPromiseAfter(delayRejection(100, 'ERR'), 1000) | ||
).rejects.toMatchInlineSnapshot(`"ERR"`); | ||
}); | ||
|
||
test('reject it the timeout elapses', async () => { | ||
return expect( | ||
timeoutPromiseAfter(delay(1000, 'OK'), 100, () => 'ERR') | ||
).rejects.toMatchInlineSnapshot(`"ERR"`); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
x-pack/plugins/task_manager/server/lib/timeout_promise_after.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,16 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export function timeoutPromiseAfter<T, G>( | ||
future: Promise<T>, | ||
ms: number, | ||
onTimeout: () => G | ||
): Promise<T> { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => reject(onTimeout()), ms); | ||
future.then(resolve).catch(reject); | ||
}); | ||
} |
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