Skip to content

Commit

Permalink
Merge pull request #1359 from canalplus/fix/flaky-test-get-license
Browse files Browse the repository at this point in the history
fix(test): fix flaky test get_license
  • Loading branch information
peaBerberian committed Feb 5, 2024
2 parents 6753afa + 1c86b07 commit 1de6ca5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/main_thread/decrypt/__tests__/__global__/get_license.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,19 @@ function checkGetLicense({
timeout = 800;
break;
case 3:
timeout = 1200;
timeout = 2000;
break;
case 4:
timeout = 3000;
timeout = 4000;
break;
case 5:
timeout = 8000;
break;
case 6:
timeout = 12000;
break;
default:
timeout = 10000;
timeout = 16000;
}
setTimeout(() => {
try {
Expand Down
39 changes: 39 additions & 0 deletions src/utils/__tests__/retry_promise_with_backoff.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { getRetryDelay } from "../retry_promise_with_backoff";

describe("utils - RetryPromiseWithBackoff", () => {
it("retry delay should increase at each retry", () => {
/*
* The delay after each retry is multiplied by a factor 2.
* This delay is slightly modified to randomize the distribution.
* Following table display the expected delay for each retry, in the
* most pessimists and most optimistics scenarios.
* The last two columns show the minimum and maximum accumulated time
* waited since the first try.
*
* |Retry| base | max | min |totalMax|totalMin |
* |-----|-------|-------|-------|--------|---------|
* | 1 |200 | 260 | 140 | 260 | 140 |
* | 2 |400 | 520 | 280 | 780 | 420 |
* | 3 |800 | 1040 | 560 | 1820 | 980 |
* | 4 |1600 | 2080 | 1120 | 3900 | 21OO |
* | 5 |3000 | 3000 | 2100 | 7800 | 4200 |
* | 6 |3000 | 3000 | 2100 | 11700 | 6300 |
* | 7 |3000 | 3000 | 2100 | 15600 | 8400 |
*/
const baseDelay = 200;
const maxDelay = 3000;
expect(getRetryDelay(baseDelay, 1, maxDelay)).toBeGreaterThanOrEqual(140);
expect(getRetryDelay(baseDelay, 1, maxDelay)).toBeLessThanOrEqual(260);

expect(getRetryDelay(baseDelay, 3, maxDelay)).toBeGreaterThanOrEqual(560);
expect(getRetryDelay(baseDelay, 3, maxDelay)).toBeLessThanOrEqual(1040);

expect(getRetryDelay(baseDelay, 6, maxDelay)).toBeGreaterThanOrEqual(2100);
expect(getRetryDelay(baseDelay, 6, maxDelay)).toBeLessThanOrEqual(3000);
});

it("retry delay should be capped by maximumDelay", () => {
const maxDelay = 1000;
expect(getRetryDelay(500, 6, maxDelay)).toBeLessThanOrEqual(maxDelay);
});
});
20 changes: 15 additions & 5 deletions src/utils/retry_promise_with_backoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,26 @@ export default function retryPromiseWithBackoff<T>(
if (typeof onRetry === "function") {
onRetry(error, retryCount);
}

const delay = Math.min(baseDelay * Math.pow(2, retryCount - 1), maxDelay);

const fuzzedDelay = getFuzzedDelay(delay);
await sleep(fuzzedDelay);
const delay = getRetryDelay(baseDelay, retryCount, maxDelay);
await sleep(delay);
const res = iterate();
return res;
}
}
}
/**
* Get the delay that should be applied to the following retry, it depends on the base delay
* and is increaser for with the retry count. The result is ceiled by the maxDelay.
* @param baseDelay delay after wich the first request is retried after a failure
* @param retryCount count of retries
* @param maxDelay maximum delay
* @returns the delay that should be applied to the following retry
*/
export function getRetryDelay(baseDelay: number, retryCount: number, maxDelay: number) {
const delay = baseDelay * Math.pow(2, retryCount - 1);
const fuzzedDelay = getFuzzedDelay(delay);
return Math.min(fuzzedDelay, maxDelay);
}

export interface IBackoffOptions {
baseDelay: number;
Expand Down

0 comments on commit 1de6ca5

Please sign in to comment.