Skip to content

Instantly share code, notes, and snippets.

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