-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
v-next/core/test/internal/user-interruptions/async-mutex.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
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); | ||
}); | ||
}); |
111 changes: 111 additions & 0 deletions
111
v-next/core/test/internal/user-interruptions/user-interruptions-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.