Skip to content

Commit

Permalink
fix: handle retry edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Gummersall committed May 15, 2021
1 parent 257e868 commit 470f93d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libraries/botbuilder-stdlib/src/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export async function retry<T>(
n = 1,
maybeError: Error | undefined;

// Take care of negative or zero
maxRetries = Math.max(maxRetries, 1);

while (n <= maxRetries) {
try {
// Note: return await intentional so we can catch errors
Expand Down
12 changes: 12 additions & 0 deletions libraries/botbuilder-stdlib/tests/retry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ describe('retry', function () {
assert.strictEqual(fake.callCount, 1);
});

it('handles zero retries', async function () {
const fake = sinon.fake((n) => Promise.resolve(n));
assert.strictEqual(await retry(fake, 0, 0), 1);
assert.strictEqual(fake.callCount, 1);
});

it('handles negative retries', async function () {
const fake = sinon.fake((n) => Promise.resolve(n));
assert.strictEqual(await retry(fake, -10, 0), 1);
assert.strictEqual(fake.callCount, 1);
});

it('succeeds eventually', async function () {
const fake = sinon.fake((n) => (n < 3 ? Promise.reject() : Promise.resolve(10)));
assert.strictEqual(await retry(fake, 3, 0), 10);
Expand Down

0 comments on commit 470f93d

Please sign in to comment.