Skip to content

Instantly share code, notes, and snippets.

@lydemann
Created November 16, 2022 16:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
exponential-backoff-retry.spec.ts
import { fakeAsync, tick } from '@angular/core/testing';
import { of, throwError } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import {
exponentialBackoffRetry,
RETRY_COUNT,
RETRY_TIME,
} from './exponential-backoff-retry';
describe('exponentialBackoffRetry', () => {
it('should retry http GET with exponential backoff', fakeAsync(() => {
let callCount = 0;
of(() => {})
.pipe(
mergeMap(() => {
callCount = callCount + 1;
return throwError(() => new Error('Some error'));
}),
exponentialBackoffRetry(RETRY_COUNT, RETRY_TIME),
)
.subscribe({
error: () => {
// silence error in test
},
});
tick(RETRY_TIME);
tick(RETRY_TIME * 4);
tick(RETRY_TIME * 9);
expect(callCount).toBe(RETRY_COUNT + 1);
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment