Skip to content

Instantly share code, notes, and snippets.

@lydemann
Created November 16, 2022 16:30
Show Gist options
  • Save lydemann/0411ec663d068c25ed3fe25b21f730a4 to your computer and use it in GitHub Desktop.
Save lydemann/0411ec663d068c25ed3fe25b21f730a4 to your computer and use it in GitHub Desktop.
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