|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. |
| 3 | + * All Rights Reserved. SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { expect } from 'chai' |
| 7 | +import * as sinon from 'sinon' |
| 8 | +import { retryWithBackoff, DEFAULT_MAX_RETRIES, DEFAULT_BASE_DELAY } from './retryUtils' |
| 9 | + |
| 10 | +describe('retryUtils', () => { |
| 11 | + let clock: sinon.SinonFakeTimers |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + clock = sinon.useFakeTimers() |
| 15 | + }) |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + clock.restore() |
| 19 | + }) |
| 20 | + |
| 21 | + describe('retryWithBackoff', () => { |
| 22 | + it('should return result on first success', async () => { |
| 23 | + const fn = sinon.stub().resolves('success') |
| 24 | + |
| 25 | + const result = await retryWithBackoff(fn) |
| 26 | + |
| 27 | + expect(result).to.equal('success') |
| 28 | + expect(fn.callCount).to.equal(1) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should retry on retryable errors', async () => { |
| 32 | + const fn = sinon.stub() |
| 33 | + fn.onFirstCall().rejects({ code: 'ThrottlingException' }) |
| 34 | + fn.onSecondCall().resolves('success') |
| 35 | + |
| 36 | + const promise = retryWithBackoff(fn) |
| 37 | + await clock.tickAsync(DEFAULT_BASE_DELAY) |
| 38 | + const result = await promise |
| 39 | + |
| 40 | + expect(result).to.equal('success') |
| 41 | + expect(fn.callCount).to.equal(2) |
| 42 | + }) |
| 43 | + |
| 44 | + it('should not retry on non-retryable client errors', async () => { |
| 45 | + const error = { statusCode: 404 } |
| 46 | + const fn = sinon.stub().rejects(error) |
| 47 | + |
| 48 | + try { |
| 49 | + await retryWithBackoff(fn) |
| 50 | + expect.fail('Expected function to throw') |
| 51 | + } catch (e) { |
| 52 | + expect(e).to.equal(error) |
| 53 | + } |
| 54 | + expect(fn.callCount).to.equal(1) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should retry on server errors', async () => { |
| 58 | + const fn = sinon.stub() |
| 59 | + fn.onFirstCall().rejects({ statusCode: 500 }) |
| 60 | + fn.onSecondCall().resolves('success') |
| 61 | + |
| 62 | + const promise = retryWithBackoff(fn) |
| 63 | + await clock.tickAsync(DEFAULT_BASE_DELAY) |
| 64 | + const result = await promise |
| 65 | + |
| 66 | + expect(result).to.equal('success') |
| 67 | + expect(fn.callCount).to.equal(2) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should use exponential backoff by default', async () => { |
| 71 | + const fn = sinon.stub() |
| 72 | + const error = { code: 'ThrottlingException' } |
| 73 | + fn.onFirstCall().rejects(error) |
| 74 | + fn.onSecondCall().rejects(error) |
| 75 | + |
| 76 | + const promise = retryWithBackoff(fn) |
| 77 | + |
| 78 | + // First retry after baseDelay * 1 |
| 79 | + await clock.tickAsync(DEFAULT_BASE_DELAY) |
| 80 | + // Second retry after baseDelay * 2 |
| 81 | + await clock.tickAsync(DEFAULT_BASE_DELAY * 2) |
| 82 | + |
| 83 | + try { |
| 84 | + await promise |
| 85 | + expect.fail('Expected function to throw') |
| 86 | + } catch (e) { |
| 87 | + expect(e).to.equal(error) |
| 88 | + } |
| 89 | + expect(fn.callCount).to.equal(DEFAULT_MAX_RETRIES) |
| 90 | + }) |
| 91 | + |
| 92 | + it('should respect custom maxRetries', async () => { |
| 93 | + const error = { code: 'ThrottlingException' } |
| 94 | + const fn = sinon.stub().rejects(error) |
| 95 | + |
| 96 | + try { |
| 97 | + await retryWithBackoff(fn, { maxRetries: 1 }) |
| 98 | + expect.fail('Expected function to throw') |
| 99 | + } catch (e) { |
| 100 | + expect(e).to.equal(error) |
| 101 | + } |
| 102 | + expect(fn.callCount).to.equal(1) |
| 103 | + }) |
| 104 | + |
| 105 | + it('should use custom isRetryable function', async () => { |
| 106 | + const error = { custom: 'error' } |
| 107 | + const fn = sinon.stub().rejects(error) |
| 108 | + const isRetryable = sinon.stub().returns(false) |
| 109 | + |
| 110 | + try { |
| 111 | + await retryWithBackoff(fn, { isRetryable }) |
| 112 | + expect.fail('Expected function to throw') |
| 113 | + } catch (e) { |
| 114 | + expect(e).to.equal(error) |
| 115 | + } |
| 116 | + expect(fn.callCount).to.equal(1) |
| 117 | + expect(isRetryable.calledWith(error)).to.equal(true) |
| 118 | + }) |
| 119 | + }) |
| 120 | +}) |
0 commit comments