Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for user interruptions #5286

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion v-next/core/src/internal/async-mutex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class AsyncMutex {
* @param f The function to run.
* @returns The result of the function.
*/
public async excluiveRun<ReturnT>(
public async exclusiveRun<ReturnT>(
f: () => ReturnT,
): Promise<Awaited<ReturnT>> {
const release = await this.#acquire();
Expand Down
8 changes: 4 additions & 4 deletions v-next/core/src/internal/user-interruptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class UserInterruptionManagerImplementation
interruptor: string,
message: string,
): Promise<void> {
return this.#mutex.excluiveRun(async () => {
return this.#mutex.exclusiveRun(async () => {
return this.#hooks.runHandlerChain(
"userInterruptions",
"displayMessage",
Expand All @@ -31,7 +31,7 @@ export class UserInterruptionManagerImplementation
interruptor: string,
inputDescription: string,
): Promise<string> {
return this.#mutex.excluiveRun(async () => {
return this.#mutex.exclusiveRun(async () => {
return this.#hooks.runHandlerChain(
"userInterruptions",
"requestInput",
Expand All @@ -45,7 +45,7 @@ export class UserInterruptionManagerImplementation
interruptor: string,
inputDescription: string,
): Promise<string> {
return this.#mutex.excluiveRun(async () => {
return this.#mutex.exclusiveRun(async () => {
return this.#hooks.runHandlerChain(
"userInterruptions",
"requestSecretInput",
Expand All @@ -58,7 +58,7 @@ export class UserInterruptionManagerImplementation
public async uninterrupted<ReturnT>(
f: () => ReturnT,
): Promise<Awaited<ReturnT>> {
return this.#mutex.excluiveRun(f);
return this.#mutex.exclusiveRun(f);
}
}

Expand Down
29 changes: 29 additions & 0 deletions v-next/core/test/internal/user-interruptions/async-mutex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";

import { AsyncMutex } from "../../../src/internal/async-mutex.js";

describe("AsyncMutex", () => {
it("should run a function exclusively", async () => {
const mutex = new AsyncMutex();

let running = 0;
let maxRunning = 0;

async function run() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

running++;
maxRunning = Math.max(maxRunning, running);
await new Promise((resolve) => setTimeout(resolve, 10));
running--;
}

await Promise.all([
mutex.exclusiveRun(run),
mutex.exclusiveRun(run),
mutex.exclusiveRun(run),
]);

assert.equal(maxRunning, 1);
assert.equal(running, 0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";

import { HookManagerImplementation } from "../../../src/internal/hook-manager.js";
import { UserInterruptionManagerImplementation } from "../../../src/internal/user-interruptions.js";
import { UserInterruptionHooks } from "../../../src/types/hooks.js";

describe("UserInterruptionManager", () => {
describe("displayMessage", () => {
it("Should call a dynamic handler with a given message from an interruptor", async () => {
const hookManager = new HookManagerImplementation([]);
const userInterruptionManager = new UserInterruptionManagerImplementation(
hookManager,
);
hookManager.setContext({} as any);

let called = false;
let givenInterruptor: string = "";
let givenMessage: string = "";

const handlers: Partial<UserInterruptionHooks> = {
async displayMessage(_context, interruptor, message) {
called = true;
givenInterruptor = interruptor;
givenMessage = message;
},
};

hookManager.registerHandlers("userInterruptions", handlers);

await userInterruptionManager.displayMessage(
"test-interruptor",
"test-message",
);

assert(called, "Handler was not called");
assert.equal(givenInterruptor, "test-interruptor");
assert.equal(givenMessage, "test-message");
});
});

describe("requestInput", () => {
it("Should call a dynamic handler with a given input description from an interruptor", async () => {
const hookManager = new HookManagerImplementation([]);
const userInterruptionManager = new UserInterruptionManagerImplementation(
hookManager,
);
hookManager.setContext({} as any);

let called = false;
let givenInterruptor: string = "";
let givenInputDescription: string = "";

const handlers: Partial<UserInterruptionHooks> = {
async requestInput(_context, interruptor, inputDescription) {
called = true;
givenInterruptor = interruptor;
givenInputDescription = inputDescription;
return "test-input";
},
};

hookManager.registerHandlers("userInterruptions", handlers);

const input = await userInterruptionManager.requestInput(
"test-interruptor",
"test-input-description",
);

assert(called, "Handler was not called");
assert.equal(givenInterruptor, "test-interruptor");
assert.equal(givenInputDescription, "test-input-description");
assert.equal(input, "test-input");
});
});

describe("requestSecretInput", () => {
it("Should call a dynamic handler with a given input description from an interruptor", async () => {
const hookManager = new HookManagerImplementation([]);
const userInterruptionManager = new UserInterruptionManagerImplementation(
hookManager,
);
hookManager.setContext({} as any);

let called = false;
let givenInterruptor: string = "";
let givenInputDescription: string = "";

const handlers: Partial<UserInterruptionHooks> = {
async requestSecretInput(_context, interruptor, inputDescription) {
called = true;
givenInterruptor = interruptor;
givenInputDescription = inputDescription;
return "test-secret-input";
},
};

hookManager.registerHandlers("userInterruptions", handlers);

const input = await userInterruptionManager.requestSecretInput(
"test-interruptor",
"test-input-description",
);

assert(called, "Handler was not called");
assert.equal(givenInterruptor, "test-interruptor");
assert.equal(givenInputDescription, "test-input-description");
assert.equal(input, "test-secret-input");
});
});
});
1 change: 0 additions & 1 deletion v-next/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
"test": "glob --cmd=\"node --import tsx/esm --test\" \"test/**/*.ts\"",
"test:only": "glob --cmd=\"node --import tsx/esm --test --test-only\" \"test/**/*.ts\"",
"test:github": "glob --cmd=\"node --import tsx/esm --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout\" \"test/**/*.ts\"",
"pretest": "pnpm build",
"build": "tsc --build .",
Expand Down
Loading