Skip to content

Commit

Permalink
fix(azure-iot-common): Fix for #338: Exp backoff retry formula
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Cauchois committed Jul 27, 2018
1 parent 08f541f commit dd36fbe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion common/core/src/retry_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class ExponentialBackOffWithJitter implements RetryPolicy {
let minRandomFactor = constants.c * (1 - constants.jd);
let maxRandomFactor = constants.c * (1 - constants.ju);
let randomJitter = Math.random() * (maxRandomFactor - minRandomFactor);
return Math.min(constants.cMin + (Math.pow(retryCount - 1, 2) - 1) * randomJitter, constants.cMax);
return Math.min(constants.cMin + (Math.pow(2, retryCount - 1) - 1) * randomJitter, constants.cMax);
}
}

Expand Down
18 changes: 9 additions & 9 deletions common/core/test/_retry_policy_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ describe('RetryPolicy', function () {
Math.random.restore();
});

it('returns \'C\' for the first retry if the first retry is not set to be immediate', function () {
it('returns a non-zero value for the first retry if the first retry is not set to be immediate', function () {
sinon.stub(Math, 'random').returns(0.42);
var policy = new ExponentialBackOffWithJitter(false);
assert.strictEqual(policy.nextRetryTimeout(0, false), 100);
assert.isAbove(policy.nextRetryTimeout(0, false), 0);
Math.random.restore();
});

Expand All @@ -59,10 +59,10 @@ describe('RetryPolicy', function () {
```]*/
[
{ retryCount: 0, expected: 0 },
{ retryCount: 1, expected: 89.5 },
{ retryCount: 2, expected: 100 },
{ retryCount: 1, expected: 100 },
{ retryCount: 2, expected: 110.5 },
{ retryCount: 3, expected: 131.5 },
{ retryCount: 10, expected: 940 },
{ retryCount: 10, expected: 5465.5 },
{ retryCount: 42, expected: 10000 }
].forEach(function (testConfig) {
assert.strictEqual(policy.nextRetryTimeout(testConfig.retryCount, false), testConfig.expected);
Expand All @@ -77,11 +77,11 @@ describe('RetryPolicy', function () {
jd = 0.25
```]*/
[
{ retryCount: 0, expected: 10000 },
{ retryCount: 1, expected: 9475 },
{ retryCount: 2, expected: 10000 },
{ retryCount: 0, expected: 9737.5 },
{ retryCount: 1, expected: 10000 },
{ retryCount: 2, expected: 10525 },
{ retryCount: 3, expected: 11575 },
{ retryCount: 10, expected: 52000 },
{ retryCount: 10, expected: 60000 },
{ retryCount: 42, expected: 60000 }
].forEach(function (testConfig) {
assert.strictEqual(policy.nextRetryTimeout(testConfig.retryCount, true), testConfig.expected);
Expand Down

0 comments on commit dd36fbe

Please sign in to comment.