Skip to content

Commit

Permalink
chore: api retry logic for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abilevych committed Jan 11, 2024
1 parent 533b4ed commit ecce757
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 192 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ cypress/videos/
cypress/screenshots/
tests/e2e/reports/
**/tests/e2e/artifacts/
**/playbook/artifacts-zk/
**/playbook/artifacts/
**/playbook/buffer/
**/playbook/cache-zk/
**/playbook/cache/

# Logs
logs
Expand Down
3 changes: 3 additions & 0 deletions packages/integration-tests/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Wallets } from "./entities";

export const localConfig = {
debugAPIwrapper: false,
gasLimit: { gasLimit: 10000000 },
l2GasLimit: 10000000,
L1Network: "http://localhost:8545",
Expand All @@ -11,6 +12,8 @@ export const localConfig = {
extendedPause: 20 * 1000,
standardPause: 5 * 1000,
minimalPause: 1 * 1000,
maxAPIretries: 10,
intervalAPIretries: 0.5 * 1000,
};

export const environment = {
Expand Down
28 changes: 28 additions & 0 deletions packages/integration-tests/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { execSync } from "child_process";
import { promises as fs } from "fs";
import * as path from "path";
import * as request from "supertest";

import { environment, localConfig } from "./config";
import { Logger } from "./entities";

export class Helper {
Expand Down Expand Up @@ -35,4 +37,30 @@ export class Helper {
console.log(`There is no the expected file: ${fileName}`);
}
}

async delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

async performGETrequest(apiRoute: string) {
return request(environment.blockExplorerAPI).get(apiRoute);
}

async retryAPIrequest(apiRoute, unsucceededResponse = false) {
for (let i = 0; i < localConfig.maxAPIretries; i++) {
try {
const response = await this.performGETrequest(apiRoute);

if (response.status === 200 || unsucceededResponse) {
return response;
}
} catch (e) {
if (localConfig.debugAPIwrapper) {
console.error(e);
}
}
await this.delay(localConfig.intervalAPIretries);
}
throw new Error(`There is error after the request ${localConfig.maxAPIretries} retries`);
}
}
Loading

0 comments on commit ecce757

Please sign in to comment.