Skip to content

Commit

Permalink
Fix e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Jul 15, 2023
1 parent 2e115d0 commit 49df2c2
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions packages/beacon-node/test/e2e/eth1/jsonRpcHttpClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {JsonRpcHttpClient} from "../../../src/eth1/provider/jsonRpcHttpClient.js
import {getGoerliRpcUrl} from "../../testParams.js";
import {RpcPayload} from "../../../src/eth1/interface.js";

type FetchError = {
code: string;
};

describe("eth1 / jsonRpcHttpClient", function () {
this.timeout("10 seconds");

Expand All @@ -22,6 +26,7 @@ describe("eth1 / jsonRpcHttpClient", function () {
abort?: true;
timeout?: number;
error: any;
errorCode?: string;
}[] = [
// // NOTE: This DNS query is very expensive, all cache miss. So it can timeout the tests and cause false positives
// {
Expand All @@ -39,7 +44,8 @@ describe("eth1 / jsonRpcHttpClient", function () {
id: "Bad port",
url: `http://localhost:${port + 1}`,
requestListener: (req, res) => res.end(),
error: "connect ECONNREFUSED",
error: "",
errorCode: "ECONNREFUSED",
},
{
id: "Not a JSON RPC endpoint",
Expand Down Expand Up @@ -122,7 +128,6 @@ describe("eth1 / jsonRpcHttpClient", function () {

for (const testCase of testCases) {
const {id, requestListener, abort, timeout} = testCase;
const error = testCase.error as Error;
let {url, payload} = testCase;

it(id, async function () {
Expand All @@ -148,7 +153,16 @@ describe("eth1 / jsonRpcHttpClient", function () {
const controller = new AbortController();
if (abort) setTimeout(() => controller.abort(), 50);
const eth1JsonRpcClient = new JsonRpcHttpClient([url], {signal: controller.signal});
await expect(eth1JsonRpcClient.fetch(payload, {timeout})).to.be.rejectedWith(error);

try {
await eth1JsonRpcClient.fetch(payload, {timeout});
} catch (error) {
if (testCase.errorCode) {
expect((error as FetchError).code).to.eql(testCase.errorCode);
} else {
expect((error as Error).message).includes(testCase.error);
}
}
});
}
});
Expand Down Expand Up @@ -201,16 +215,18 @@ describe("eth1 / jsonRpcHttpClient - with retries", function () {

const controller = new AbortController();
const eth1JsonRpcClient = new JsonRpcHttpClient([url], {signal: controller.signal});
await expect(
eth1JsonRpcClient.fetchWithRetries(payload, {
try {
await eth1JsonRpcClient.fetchWithRetries(payload, {
retryAttempts,
shouldRetry: () => {
// using the shouldRetry function to keep tab of the retried requests
retryCount++;
return true;
},
})
).to.be.rejectedWith("connect ECONNREFUSED");
});
} catch (error) {
expect((error as FetchError).code).eql("ECONNREFUSED");
}
expect(retryCount).to.be.equal(retryAttempts, "connect ECONNREFUSED should be retried before failing");
});

Expand Down

0 comments on commit 49df2c2

Please sign in to comment.