-
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.
- Loading branch information
1 parent
26dd47a
commit 54baa62
Showing
3 changed files
with
79 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,41 @@ | ||
/** | ||
* 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 { timeout = 30000, timeBetweenRequests = 3000 } = opts || {}; | ||
let resp: any; | ||
let requestCount = 0; | ||
|
||
do { | ||
// on subsequent requests, check if the configured | ||
// 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,37 @@ | ||
import { poll } from "../../src/utils/poll"; | ||
|
||
describe("poll", () => { | ||
it("polls the request function until the validation function succeeds", async () => { | ||
const validationFn = (resp) => 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) => resp && resp.id; | ||
const requestFnSpy = jasmine | ||
.createSpy() | ||
.and.returnValue(Promise.resolve({})); | ||
|
||
let errorMessage = "Polling timeout"; | ||
try { | ||
await poll(requestFnSpy, validationFn, { | ||
timeBetweenRequests: 100, | ||
timeout: 200, | ||
}); | ||
} catch (error) { | ||
errorMessage = error.message; | ||
} | ||
expect(requestFnSpy).toHaveBeenCalledTimes(2); | ||
expect(errorMessage).toBe("Polling timeout"); | ||
}); | ||
}); |