-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ULMS-2596 Added timeout and retry logic
- Loading branch information
Showing
10 changed files
with
265 additions
and
65 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,58 @@ | ||
import retry from '../src/retry' | ||
|
||
const resolvedData = {} | ||
const error = new Error('example error') | ||
const taskThatResolves = () => Promise.resolve(resolvedData) | ||
const taskThatRejects = () => Promise.reject(error) | ||
const taskThatThrows = () => { | ||
throw error | ||
} | ||
const taskThatResolvesAfter = (n) => { | ||
let count = 0 | ||
|
||
return () => { | ||
count += 1 | ||
|
||
if (count < n) { | ||
return taskThatRejects() | ||
} | ||
|
||
return taskThatResolves() | ||
} | ||
} | ||
|
||
describe('retry', () => { | ||
it('resolves when task resolves', async () => { | ||
await expect(retry(taskThatResolves)).resolves.toBe(resolvedData) | ||
}) | ||
|
||
it('rejects when task rejects', async () => { | ||
await expect(retry(taskThatRejects)).rejects.toBe(error) | ||
}) | ||
|
||
it('rejects when task throws', async () => { | ||
await expect(retry(taskThatThrows)).rejects.toBe(error) | ||
}) | ||
|
||
it('resolves when task resolves after 2 calls', async () => { | ||
const task = taskThatResolvesAfter(2) | ||
|
||
await expect(retry(task)).resolves.toBe(resolvedData) | ||
}) | ||
|
||
it('onRetry have not been called', async () => { | ||
const onRetry = jest.fn() | ||
|
||
await expect(retry(taskThatResolves, onRetry)).resolves.toBe(resolvedData) | ||
|
||
expect(onRetry).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('onRetry have been called 2 times', async () => { | ||
const onRetry = jest.fn() | ||
|
||
await expect(retry(taskThatRejects, onRetry)).rejects.toBe(error) | ||
|
||
expect(onRetry).toHaveBeenCalledTimes(2) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.