Skip to content

Commit

Permalink
feat(Nyuma): add retryHook
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierCuyp committed Oct 2, 2020
1 parent a5e345d commit 6621b95
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ const main = async () => {

try {
const body = await nyuma
.retryHook(({ error, retryCount, lastDelay, duration }) =>
console.log('Retry with:', { error, retryCount, lastDelay, duration })
)
.failHook(({ reason, retryCount, lastDelay, duration }) =>
console.log({ reason, retryCount, lastDelay, duration })
console.log('Failed with:', { reason, retryCount, lastDelay, duration })
)
.start(async () => {
const response = await fetch('https://api.github.com/repos/webinmove/nyuma');
Expand Down
15 changes: 15 additions & 0 deletions src/Nyuma.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = class Nyuma {
this.retryCount = 0;
this.delay = 0;
this.lastError = null;
this.onRetry = () => {};
this.onFail = () => {};
}

Expand All @@ -32,6 +33,12 @@ module.exports = class Nyuma {
return this;
}

retryHook (onRetry) {
this.onRetry = onRetry;

return this;
}

getDuration () {
if (!this.startTime) {
return 0;
Expand Down Expand Up @@ -88,6 +95,7 @@ module.exports = class Nyuma {
return;
}
this.lastError = err;

if (this.retryCount >= this.maxRetries) {
this.onFail({
reason: 'Max retries reached',
Expand All @@ -99,6 +107,13 @@ module.exports = class Nyuma {
throw this.lastError;
}

this.onRetry({
error: this.lastError,
retryCount: this.retryCount,
lastDelay: this.delay,
duration: this.getDuration()
});

const strategyDelay = this.initialDelay * this.strategy.next();
this.delay = Math.min(strategyDelay, this.maxDelay);

Expand Down
82 changes: 82 additions & 0 deletions test/src/Nyuma.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,62 @@ describe('Nyuma', () => {
await expect(promise).to.eventually.be.rejectedWith(error);
});

it('should trigger retry hook on first error', async () => {
const nyuma = new Nyuma({
initialDelay: 10,
maxTime: 100
});
let capturedFailInfo;
let error;

nyuma.retryHook((failInfo) => {
capturedFailInfo = failInfo;
});

try {
await nyuma.start((retryCount, delay) => {
if (retryCount < 2) {
error = new Error('Try again');
throw error;
}
});
} catch (err) {}

expect(capturedFailInfo).to.deep.include({
error,
retryCount: 1,
lastDelay: 10
});
});

it('should chain on retry hook', async () => {
const nyuma = new Nyuma({
initialDelay: 10,
maxTime: 100
});
let capturedFailInfo;
let error;

try {
await nyuma
.retryHook((failInfo) => {
capturedFailInfo = failInfo;
})
.start((retryCount, delay) => {
if (retryCount < 2) {
error = new Error('Try again');
throw error;
}
});
} catch (err) {}

expect(capturedFailInfo).to.deep.include({
error,
retryCount: 1,
lastDelay: 10
});
});

it('should trigger fail hook when max time is reached', async () => {
const nyuma = new Nyuma({
initialDelay: 10,
Expand All @@ -180,6 +236,32 @@ describe('Nyuma', () => {
});
});

it('should chain on fail hook', async () => {
const nyuma = new Nyuma({
initialDelay: 10,
maxTime: 100
});
let capturedFailInfo;

try {
await nyuma
.failHook((failInfo) => {
capturedFailInfo = failInfo;
})
.start((retryCount, delay) => {
if (retryCount < 10) {
throw new Error('Try again');
}
});
} catch (err) {}

expect(capturedFailInfo).to.deep.include({
reason: 'Max time reached',
retryCount: 3,
lastDelay: 80
});
});

it('should return default error when 1st try exceed max time', async () => {
const nyuma = new Nyuma({
initialDelay: 10,
Expand Down

0 comments on commit 6621b95

Please sign in to comment.