Skip to content

Commit

Permalink
chore: updated retry-logic for integration api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abilevych committed Jan 23, 2024
1 parent 806dddb commit c4bdcae
Show file tree
Hide file tree
Showing 10 changed files with 1,109 additions and 995 deletions.
26 changes: 15 additions & 11 deletions packages/integration-tests/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,25 @@ export class Helper {
return request(environment.blockExplorerAPI).get(apiRoute);
}

async retryAPIrequest(apiRoute, unsucceededResponse = false) {
/**
* A retry wrapper method to enhance test stability in API testing.
* Useful when API response fields may not immediately reflect the expected state,
* but can update to the correct response after a delay.
* Attempts to execute the action a specified number of times (defined in localConfig.maxAPIretries)
* with a delay between attempts (localConfig.intervalAPIretries).
* Throws an error if the action consistently fails after all retries.
*/
async retryTestAction(action) {
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 action();
return;
} catch (error) {
if (i === localConfig.maxAPIretries - 1) {
throw error;
}
await new Promise((resolve) => setTimeout(resolve, localConfig.intervalAPIretries));
}
await this.delay(localConfig.intervalAPIretries);
}
throw new Error(`There is error after the request ${localConfig.maxAPIretries} retries`);
}
}
136 changes: 72 additions & 64 deletions packages/integration-tests/tests/api/accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,84 +14,92 @@ describe("API module: Account", () => {

//@id1704
it("Verify /api?module=account&action=balancemulti response", async () => {
apiRoute = `/api?module=account&action=balancemulti&address=${Wallets.richWalletAddress},${Wallets.mainWalletAddress}`;
const richWalletBalance = await helper.getBalanceETH(Wallets.richWalletAddress, "L2");
const mainWalletBalance = await helper.getBalanceETH(Wallets.mainWalletAddress, "L2");
const richWalletLowerCase = Wallets.richWalletAddress.toLowerCase();
const mainWalletLowerCase = Wallets.mainWalletAddress.toLowerCase();
response = await helper.retryAPIrequest(apiRoute);
await helper.retryTestAction(async () => {
apiRoute = `/api?module=account&action=balancemulti&address=${Wallets.richWalletAddress},${Wallets.mainWalletAddress}`;
const richWalletBalance = await helper.getBalanceETH(Wallets.richWalletAddress, "L2");
const mainWalletBalance = await helper.getBalanceETH(Wallets.mainWalletAddress, "L2");
const richWalletLowerCase = Wallets.richWalletAddress.toLowerCase();
const mainWalletLowerCase = Wallets.mainWalletAddress.toLowerCase();
response = await helper.performGETrequest(apiRoute);

expect(response.status).toBe(200);
expect(response.body.result.length).toBeGreaterThan(1);
expect(response.body).toStrictEqual(expect.objectContaining({ status: "1" }));
expect(response.body).toStrictEqual(expect.objectContaining({ message: "OK" }));
expect(response.body.result[0]).toStrictEqual(
expect.objectContaining({ account: richWalletLowerCase, balance: richWalletBalance })
);
expect(response.body.result[1]).toStrictEqual(
expect.objectContaining({ account: mainWalletLowerCase, balance: mainWalletBalance })
);
expect(response.status).toBe(200);
expect(response.body.result.length).toBeGreaterThan(1);
expect(response.body).toStrictEqual(expect.objectContaining({ status: "1" }));
expect(response.body).toStrictEqual(expect.objectContaining({ message: "OK" }));
expect(response.body.result[0]).toStrictEqual(
expect.objectContaining({ account: richWalletLowerCase, balance: richWalletBalance })
);
expect(response.body.result[1]).toStrictEqual(
expect.objectContaining({ account: mainWalletLowerCase, balance: mainWalletBalance })
);
});
});

//@id1703
it("Verify /api?module=account&action=balance response", async () => {
const balance = await helper.getBalanceETH(Wallets.richWalletAddress, "L2");
apiRoute = `/api?module=account&action=balance&address=${Wallets.richWalletAddress}`;
response = await helper.retryAPIrequest(apiRoute);
await helper.retryTestAction(async () => {
const balance = await helper.getBalanceETH(Wallets.richWalletAddress, "L2");
apiRoute = `/api?module=account&action=balance&address=${Wallets.richWalletAddress}`;
response = await helper.performGETrequest(apiRoute);

expect(response.status).toBe(200);
expect(response.body).toStrictEqual(expect.objectContaining({ status: "1" }));
expect(response.body).toStrictEqual(expect.objectContaining({ message: "OK" }));
expect(response.body).toStrictEqual(expect.objectContaining({ result: balance }));
expect(response.status).toBe(200);
expect(response.body).toStrictEqual(expect.objectContaining({ status: "1" }));
expect(response.body).toStrictEqual(expect.objectContaining({ message: "OK" }));
expect(response.body).toStrictEqual(expect.objectContaining({ result: balance }));
});
});

//@id1705
it("Verify /api?module=account&action=tokenbalance response", async () => {
apiRoute = `/api?module=account&action=tokenbalance&contractaddress=${Token.ETHER_ERC20_Address}&address=${Wallets.richWalletAddress}`;
response = await helper.retryAPIrequest(apiRoute);
await helper.retryTestAction(async () => {
apiRoute = `/api?module=account&action=tokenbalance&contractaddress=${Token.ETHER_ERC20_Address}&address=${Wallets.richWalletAddress}`;
response = await helper.performGETrequest(apiRoute);

expect(response.status).toBe(200);
expect(response.body).toStrictEqual(expect.objectContaining({ status: "1" }));
expect(response.body).toStrictEqual(expect.objectContaining({ message: "OK" }));
expect(typeof response.body.result).toStrictEqual("string");
expect(response.status).toBe(200);
expect(response.body).toStrictEqual(expect.objectContaining({ status: "1" }));
expect(response.body).toStrictEqual(expect.objectContaining({ message: "OK" }));
expect(typeof response.body.result).toStrictEqual("string");
});
});

//@id1702
xit("Verify /api?module=account&action=txlist response", async () => {
const blocks = await request(environment.blockExplorerAPI).get("/blocks");
const blockNumber = blocks.body.items[0].number;
apiRoute = `/api?module=account&action=txlist&page=1&offset=10&sort=desc&endblock${blockNumber}&startblock=0&address=${Wallets.richWalletAddress}`;
response = await helper.retryAPIrequest(apiRoute);
it("Verify /api?module=account&action=txlist response", async () => {
await helper.retryTestAction(async () => {
const blocks = await request(environment.blockExplorerAPI).get("/blocks");
const blockNumber = blocks.body.items[0].number;
apiRoute = `/api?module=account&action=txlist&page=1&offset=10&sort=desc&endblock${blockNumber}&startblock=0&address=${Wallets.richWalletAddress}`;
response = await helper.performGETrequest(apiRoute);

expect(response.status).toBe(200);
expect(response.body.result.length).toBeGreaterThan(1);
expect(response.body).toStrictEqual(expect.objectContaining({ status: "1" }));
expect(response.body).toStrictEqual(expect.objectContaining({ message: "OK" }));
expect(typeof response.body.result[0].blockNumber).toStrictEqual("string");
expect(typeof response.body.result[0].timeStamp).toStrictEqual("string");
expect(typeof response.body.result[0].hash).toStrictEqual("string");
expect(typeof response.body.result[0].nonce).toStrictEqual("string");
expect(typeof response.body.result[0].blockHash).toStrictEqual("string");
expect(typeof response.body.result[0].transactionIndex).toStrictEqual("string");
expect(typeof response.body.result[0].from).toStrictEqual("string");
expect(typeof response.body.result[0].to).toStrictEqual("string");
expect(typeof response.body.result[0].value).toStrictEqual("string");
expect(typeof response.body.result[0].gas).toStrictEqual("string");
expect(typeof response.body.result[0].gasPrice).toStrictEqual("string");
expect(typeof response.body.result[0].isError).toStrictEqual("string");
expect(typeof response.body.result[0].txreceipt_status).toStrictEqual("string");
expect(typeof response.body.result[0].input).toStrictEqual("string");
expect(typeof response.body.result[0].contractAddress).toBeTruthy(); // can be null
expect(typeof response.body.result[0].cumulativeGasUsed).toStrictEqual("string");
expect(typeof response.body.result[0].gasUsed).toStrictEqual("string");
expect(typeof response.body.result[0].confirmations).toStrictEqual("string");
expect(typeof response.body.result[0].fee).toStrictEqual("string");
expect(typeof response.body.result[0].commitTxHash).toStrictEqual("string");
expect(typeof response.body.result[0].proveTxHash).toStrictEqual("string");
expect(typeof response.body.result[0].executeTxHash).toStrictEqual("string");
expect(typeof response.body.result[0].isL1Originated).toStrictEqual("string");
expect(typeof response.body.result[0].l1BatchNumber).toStrictEqual("string");
expect(typeof response.body.result[0].methodId).toStrictEqual("string");
expect(typeof response.body.result[0].functionName).toStrictEqual("string");
expect(response.status).toBe(200);
expect(response.body.result.length).toBeGreaterThan(1);
expect(response.body).toStrictEqual(expect.objectContaining({ status: "1" }));
expect(response.body).toStrictEqual(expect.objectContaining({ message: "OK" }));
expect(typeof response.body.result[0].blockNumber).toStrictEqual("string");
expect(typeof response.body.result[0].timeStamp).toStrictEqual("string");
expect(typeof response.body.result[0].hash).toStrictEqual("string");
expect(typeof response.body.result[0].nonce).toStrictEqual("string");
expect(typeof response.body.result[0].blockHash).toStrictEqual("string");
expect(typeof response.body.result[0].transactionIndex).toStrictEqual("string");
expect(typeof response.body.result[0].from).toStrictEqual("string");
expect(typeof response.body.result[0].to).toStrictEqual("string");
expect(typeof response.body.result[0].value).toStrictEqual("string");
expect(typeof response.body.result[0].gas).toStrictEqual("string");
expect(typeof response.body.result[0].gasPrice).toStrictEqual("string");
expect(typeof response.body.result[0].isError).toStrictEqual("string");
expect(typeof response.body.result[0].txreceipt_status).toStrictEqual("string");
expect(typeof response.body.result[0].input).toStrictEqual("string");
expect(typeof response.body.result[0].contractAddress).toBeTruthy(); // can be null
expect(typeof response.body.result[0].cumulativeGasUsed).toStrictEqual("string");
expect(typeof response.body.result[0].gasUsed).toStrictEqual("string");
expect(typeof response.body.result[0].confirmations).toStrictEqual("string");
expect(typeof response.body.result[0].fee).toStrictEqual("string");
expect(typeof response.body.result[0].commitTxHash).toStrictEqual("string");
expect(typeof response.body.result[0].proveTxHash).toStrictEqual("string");
expect(typeof response.body.result[0].executeTxHash).toStrictEqual("string");
expect(typeof response.body.result[0].isL1Originated).toStrictEqual("string");
expect(typeof response.body.result[0].l1BatchNumber).toStrictEqual("string");
expect(typeof response.body.result[0].methodId).toStrictEqual("string");
expect(typeof response.body.result[0].functionName).toStrictEqual("string");
});
});
});
Loading

0 comments on commit c4bdcae

Please sign in to comment.