-
-
Notifications
You must be signed in to change notification settings - Fork 10
fix(billing): short-circuit retryWithBackoff on non-transient Stripe errors #3697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
b3f08d5
fix(billing): short-circuit retryWithBackoff on non-transient Stripe …
PierreBrisorgueil 46efee0
test(billing): cover StripeInvalidRequestError class-name branch in r…
PierreBrisorgueil 3c27a01
fix(billing): address reviewer nits — narrow comment + improve log wo…
PierreBrisorgueil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,93 @@ | ||
| /** | ||
| * Module dependencies. | ||
| */ | ||
| import { jest, describe, test, afterEach, expect } from '@jest/globals'; | ||
| import { retryWithBackoff } from '../lib/billing.retry.js'; | ||
|
|
||
| /** | ||
| * Unit tests for retryWithBackoff: | ||
| * - success / transient-retry / exhaustion paths | ||
| * - shouldRetry short-circuit on non-transient (deterministic) errors | ||
| * - argument validation | ||
| * Retry-delay paths use fake timers so the suite never incurs real backoff waits. | ||
| */ | ||
| describe('retryWithBackoff', () => { | ||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| test('returns the result on first success without retrying', async () => { | ||
| const fn = jest.fn().mockResolvedValue('ok'); | ||
| await expect(retryWithBackoff(fn)).resolves.toBe('ok'); | ||
| expect(fn).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('retries a transient failure then resolves', async () => { | ||
| jest.useFakeTimers(); | ||
| const fn = jest.fn().mockRejectedValueOnce(new Error('boom')).mockResolvedValueOnce('ok'); | ||
| const promise = retryWithBackoff(fn, { attempts: 3, baseMs: 200 }); | ||
| await jest.runAllTimersAsync(); | ||
| await expect(promise).resolves.toBe('ok'); | ||
| expect(fn).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| test('throws the last error after exhausting all attempts', async () => { | ||
| jest.useFakeTimers(); | ||
| const fn = jest.fn().mockRejectedValue(new Error('always fails')); | ||
| const promise = retryWithBackoff(fn, { attempts: 3, baseMs: 200 }); | ||
| const assertion = expect(promise).rejects.toThrow('always fails'); | ||
| await jest.runAllTimersAsync(); | ||
| await assertion; | ||
| expect(fn).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| test('short-circuits (1 call, not 3) when shouldRetry returns false', async () => { | ||
| const err = Object.assign(new Error('bad params'), { type: 'invalid_request_error' }); | ||
| const fn = jest.fn().mockRejectedValue(err); | ||
| await expect( | ||
| retryWithBackoff(fn, { | ||
| attempts: 3, | ||
| baseMs: 200, | ||
| shouldRetry: (e) => e?.type !== 'StripeInvalidRequestError' && e?.type !== 'invalid_request_error', | ||
| }), | ||
| ).rejects.toThrow('bad params'); | ||
| expect(fn).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('short-circuits on the StripeInvalidRequestError class-name type too', async () => { | ||
| // stripe-node exposes the error class name on .type in some SDK versions and the | ||
| // raw API type string in others; the call-site predicate guards both, so cover both. | ||
| const err = Object.assign(new Error('bad params'), { type: 'StripeInvalidRequestError' }); | ||
| const fn = jest.fn().mockRejectedValue(err); | ||
| await expect( | ||
| retryWithBackoff(fn, { | ||
| attempts: 3, | ||
| baseMs: 200, | ||
| shouldRetry: (e) => e?.type !== 'StripeInvalidRequestError' && e?.type !== 'invalid_request_error', | ||
| }), | ||
| ).rejects.toThrow('bad params'); | ||
| expect(fn).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('retries to exhaustion when shouldRetry returns true', async () => { | ||
| jest.useFakeTimers(); | ||
| const fn = jest.fn().mockRejectedValue(new Error('transient')); | ||
| const promise = retryWithBackoff(fn, { | ||
| attempts: 3, | ||
| baseMs: 200, | ||
| shouldRetry: () => true, | ||
| }); | ||
| const assertion = expect(promise).rejects.toThrow('transient'); | ||
| await jest.runAllTimersAsync(); | ||
| await assertion; | ||
| expect(fn).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| test('rejects invalid attempts / baseMs / shouldRetry with TypeError', async () => { | ||
| const fn = jest.fn().mockResolvedValue('ok'); | ||
| await expect(retryWithBackoff(fn, { attempts: 0 })).rejects.toThrow(TypeError); | ||
| await expect(retryWithBackoff(fn, { baseMs: -1 })).rejects.toThrow(TypeError); | ||
| await expect(retryWithBackoff(fn, { shouldRetry: 'nope' })).rejects.toThrow(TypeError); | ||
| expect(fn).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.