-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds a new poll util which accepts a generic request and validation function and will execute the request until the validation condition(s) have been met or a timeout is reached.
- Loading branch information
1 parent
26dd47a
commit e36c142
Showing
3 changed files
with
82 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Function to poll a provided request until a validation | ||
* state or timeout is reached | ||
* @param requestFn | ||
* @param validationFn | ||
* @param opts | ||
*/ | ||
export const poll = async ( | ||
requestFn: () => any, | ||
validationFn: (resp: any) => boolean, | ||
opts?: { | ||
timeout?: number; | ||
timeBetweenRequests?: number; | ||
} | ||
): Promise<any> => { | ||
const options = | ||
opts || /* istanbul ignore next - must pass in overrides for tests */ {}; | ||
const timeout = options.timeout || 30000; | ||
const timeBetweenRequests = | ||
options.timeBetweenRequests || | ||
/* istanbul ignore next - cannot delay by this much in tests */ 3000; | ||
|
||
let resp: any; | ||
let requestCount = 0; | ||
|
||
do { | ||
// On subsequent requests, check if the timeout has been reached | ||
// If YES: throw an error | ||
// If NO: delay before the next request | ||
if (requestCount > 0) { | ||
if (requestCount * timeBetweenRequests >= timeout) { | ||
throw new Error("Polling timeout"); | ||
} | ||
await delay(timeBetweenRequests); | ||
} | ||
|
||
resp = await requestFn(); | ||
requestCount++; | ||
} while (!validationFn(resp)); | ||
|
||
return resp; | ||
}; | ||
|
||
const delay = (milliseconds: number) => { | ||
return new Promise((resolve) => setTimeout(resolve, milliseconds)); | ||
}; |
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,35 @@ | ||
import { poll } from "../../src/utils/poll"; | ||
|
||
describe("poll", () => { | ||
it("polls the request function until the validation function succeeds", async () => { | ||
const validationFn = (resp: any) => resp && resp.id; | ||
const requestFnSpy = jasmine | ||
.createSpy() | ||
.and.returnValues(Promise.resolve({}), Promise.resolve({ id: "00c" })); | ||
|
||
const chk = await poll( | ||
requestFnSpy, | ||
validationFn, | ||
// set delay to 0 for testing purposes | ||
{ timeBetweenRequests: 0 } | ||
); | ||
expect(requestFnSpy).toHaveBeenCalledTimes(2); | ||
expect(chk.id).toBe("00c"); | ||
}); | ||
it("throws an error when the timeout is reached", async () => { | ||
const validationFn = (resp: any) => resp && resp.id; | ||
const requestFnSpy = jasmine | ||
.createSpy() | ||
.and.returnValue(Promise.resolve({})); | ||
|
||
try { | ||
await poll(requestFnSpy, validationFn, { | ||
timeBetweenRequests: 100, | ||
timeout: 200, | ||
}); | ||
} catch (error) { | ||
expect(requestFnSpy).toHaveBeenCalledTimes(2); | ||
expect((error as Error).message).toBe("Polling timeout"); | ||
} | ||
}); | ||
}); |