Skip to content

Commit

Permalink
fix: improved log tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Jun 27, 2024
1 parent 4d77418 commit 5427159
Show file tree
Hide file tree
Showing 8 changed files with 403 additions and 69 deletions.
23 changes: 22 additions & 1 deletion .github/workflows/jest-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,31 @@ jobs:
with:
fetch-depth: 0

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Install dependencies
run: yarn install

- name: Build & Run test suite
- name: Build
run: yarn build

- name: Start Anvil
run: yarn test:anvil &

- name: Wait for Anvil
run: |
for i in {1..30}
do
if curl -s http://localhost:8545; then
break
fi
sleep 1
done || exit 1
- name: Run test suite
run: |
yarn test | tee ./coverage.txt && exit ${PIPESTATUS[0]}
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,19 @@ app.provider = await handler.getFastestRpcProvider();

## Testing

1. In terminal A run the following command:
1. Build the package:

```bash
anvil
yarn build
```

2. In terminal B run the following command:
2. In terminal A run the following command to start a local Anvil instance:

```bash
yarn test:anvil
```

3. In terminal B run the following command to run the tests:

```bash
yarn test
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@
]
},
"packageManager": "yarn@4.2.2"
}
}
76 changes: 76 additions & 0 deletions tests/anvil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { spawnSync } from "child_process";
import { RPCHandler } from "../dist/";

class Anvil {
rpcs: string[] = [];
rpcHandler: RPCHandler | null = null;

async init() {
this.rpcHandler = new RPCHandler({
autoStorage: false,
cacheRefreshCycles: 3,
networkId: 100,
networkName: "test",
rpcTimeout: 1000,
runtimeRpcs: null,
networkRpcs: null,
proxySettings: {
logger: null,
logTier: "ok",
retryCount: 3,
retryDelay: 100,
strictLogs: true,
moduleName: "TestModule",
},
});
await this.rpcHandler.testRpcPerformance();
const latencies: Record<string, number> = this.rpcHandler.getLatencies();
const sorted = Object.entries(latencies).sort(([, a], [, b]) => a - b);
console.log(
`Fetched ${sorted.length} RPCs.\nFastest: ${sorted[0][0]} (${sorted[0][1]}ms)\nSlowest: ${sorted[sorted.length - 1][0]} (${sorted[sorted.length - 1][1]}ms)`
);

this.rpcs = sorted.map(([rpc]) => rpc.split("__")[1]);
}

async run() {
await this.init();
console.log(`Starting Anvil...`);
const isSuccess = await this.spawner(this.rpcs.shift());

if (!isSuccess) {
throw new Error(`Anvil failed to start`);
}
}

async spawner(rpc?: string): Promise<boolean> {
if (!rpc) {
console.log(`No RPCs left to try`);
return false;
}

console.log(`Forking with RPC: ${rpc}`);

const anvil = spawnSync("anvil", ["--chain-id", "31337", "--fork-url", rpc, "--host", "127.0.0.1", "--port", "8545"], {
stdio: "inherit",
});

if (anvil.status !== 0) {
console.log(`Anvil failed to start with RPC: ${rpc}`);
console.log(`Retrying with next RPC...`);
return this.spawner(this.rpcs.shift());
}

return true;
}
}

async function main() {
const anvil = new Anvil();
await anvil.run();
}

main().catch((error) => {
console.error(error);
process.exit(1);
});
32 changes: 30 additions & 2 deletions tests/call-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const rpcList = [
"http://127.0.0.1:81",
"http://127.0.0.1:8545",
];
const txHashRegex = new RegExp("0x[0-9a-f]{64}");

describe("Call Handler", () => {
afterAll(() => {
Expand Down Expand Up @@ -56,10 +57,9 @@ describe("Call Handler", () => {
});
});

describe("Success cases", () => {
describe("Write ops cases", () => {
let provider: JsonRpcProvider;
let handler: RPCHandler;
const txHashRegex = new RegExp("0x[0-9a-f]{64}");

const mods: HandlerConstructorConfig = {
runtimeRpcs: rpcList,
Expand Down Expand Up @@ -172,8 +172,36 @@ describe("Call Handler", () => {
}

expect(thrownError).toBeInstanceOf(Error);
expect((thrownError as Error).message).toContain("invalid type: null");
expect(newHandler["_runtimeRpcs"].length).toBe(1);
expect(newHandler["_runtimeRpcs"][0]).toBe("http://127.0.0.1:8545");
});

it("should return a standard JsonRpcProvider if proxySettings.disabled", async () => {
const newHandler = new RPCHandler({ ...mods, proxySettings: { ...mods.proxySettings, disabled: true, strictLogs: false, logTier: "error" } });
const newProvider = await newHandler.getFastestRpcProvider();

const txData = {
gas: "0x1f4f8",
from: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
to: "0x000000000022d473030f116ddee9f6b43ac78ba3",
data: "0x4fe02b4400000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c800d2429b6ec3b99c749d9629667197d4af1dd7ab825c27adf3477c79e9e5ac22",
};

const txHash = await newProvider.send("eth_sendTransaction", [txData]);
expect(txHash).toBeDefined();
expect(txHash).toMatch(txHashRegex);

let thrownError: Error | unknown = null;

try {
await newProvider.send("eth_call", [null, null, null]);
} catch (err) {
thrownError = err;
}

expect(thrownError).toBeInstanceOf(Error);
expect((thrownError as Error).message).toContain("invalid type: null");
});
});
});
Loading

0 comments on commit 5427159

Please sign in to comment.