Skip to content

Commit

Permalink
fix: handle potential concurrency issues with promises
Browse files Browse the repository at this point in the history
  • Loading branch information
schaable committed Jan 30, 2025
1 parent efde982 commit df927dd
Showing 1 changed file with 45 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,47 +92,56 @@ describe("http-provider", () => {
assert.equal(result, "0x1");
});

it("should emit an event when the method is evm_revert", async () => {
let eventEmitted = false;
const jsonRpcRequest = {
jsonrpc: "2.0",
id: 1,
method: "evm_revert",
params: ["0x1"],
};
const jsonRpcResponse = {
jsonrpc: "2.0",
id: 1,
result: true,
};
it(
"should emit an event when the method is evm_revert",
{ timeout: 1000 },
async () => {
let eventEmitted = false;
const jsonRpcRequest = {
jsonrpc: "2.0",
id: 1,
method: "evm_revert",
params: ["0x1"],
};
const jsonRpcResponse = {
jsonrpc: "2.0",
id: 1,
result: true,
};

interceptor
.intercept({
...baseInterceptorOptions,
body: JSON.stringify(jsonRpcRequest),
})
.reply(200, jsonRpcResponse);
interceptor
.intercept({
...baseInterceptorOptions,
body: JSON.stringify(jsonRpcRequest),
})
.reply(200, jsonRpcResponse);

const provider = await HttpProvider.create({
url: "http://localhost",
// it doesn't matter that this isn't a real edr network
// as we are mocking the dispatcher
networkName: "edrNetwork",
timeout: 20_000,
testDispatcher: interceptor,
});
const provider = await HttpProvider.create({
url: "http://localhost",
// it doesn't matter that this isn't a real edr network
// as we are mocking the dispatcher
networkName: "edrNetwork",
timeout: 20_000,
testDispatcher: interceptor,
});

provider.on(EDR_NETWORK_REVERT_SNAPSHOT_EVENT, () => {
eventEmitted = true;
});
const eventPromise = new Promise<void>((resolve) => {
provider.on(EDR_NETWORK_REVERT_SNAPSHOT_EVENT, () => {
eventEmitted = true;
resolve();
});
});

await provider.request({
method: "evm_revert",
params: ["0x1"],
});
await provider.request({
method: "evm_revert",
params: ["0x1"],
});

assert.ok(eventEmitted, "The evm_revert event should be emitted");
});
await eventPromise;

assert.ok(eventEmitted, "The evm_revert event should be emitted");
},
);

it("should throw if the params are an object", async () => {
const provider = await HttpProvider.create({
Expand Down

0 comments on commit df927dd

Please sign in to comment.