mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
90 lines
2.9 KiB
TypeScript
90 lines
2.9 KiB
TypeScript
import { AsyncLocalStorage, AsyncResource } from "async_hooks";
|
|
import assert from "assert";
|
|
|
|
test("node async_hooks.AsyncLocalStorage enable disable", async done => {
|
|
const asyncLocalStorage = new AsyncLocalStorage<Map<string, any>>();
|
|
|
|
asyncLocalStorage.run(new Map(), () => {
|
|
asyncLocalStorage.getStore()!.set("foo", "bar");
|
|
process.nextTick(() => {
|
|
assert.strictEqual(asyncLocalStorage.getStore()!.get("foo"), "bar");
|
|
process.nextTick(() => {
|
|
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
|
|
});
|
|
|
|
asyncLocalStorage.disable();
|
|
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
|
|
|
|
// Calls to exit() should not mess with enabled status
|
|
asyncLocalStorage.exit(() => {
|
|
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
|
|
});
|
|
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
|
|
|
|
process.nextTick(() => {
|
|
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
|
|
asyncLocalStorage.run(new Map().set("bar", "foo"), () => {
|
|
assert.strictEqual(asyncLocalStorage.getStore()!.get("bar"), "foo");
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
test("node async_hooks.AsyncLocalStorage enable disable multiple times", async () => {
|
|
const asyncLocalStorage = new AsyncLocalStorage();
|
|
|
|
asyncLocalStorage.enterWith("first value");
|
|
expect(asyncLocalStorage.getStore()).toBe("first value");
|
|
asyncLocalStorage.disable();
|
|
expect(asyncLocalStorage.getStore()).toBe(undefined);
|
|
|
|
asyncLocalStorage.enterWith("second value");
|
|
expect(asyncLocalStorage.getStore()).toBe("second value");
|
|
asyncLocalStorage.disable();
|
|
expect(asyncLocalStorage.getStore()).toBe(undefined);
|
|
|
|
const { promise, resolve, reject } = Promise.withResolvers();
|
|
asyncLocalStorage.run("first run value", () => {
|
|
try {
|
|
expect(asyncLocalStorage.getStore()).toBe("first run value");
|
|
asyncLocalStorage.disable();
|
|
expect(asyncLocalStorage.getStore()).toBe(undefined);
|
|
asyncLocalStorage.run("second run value", () => {
|
|
try {
|
|
expect(asyncLocalStorage.getStore()).toBe("second run value");
|
|
asyncLocalStorage.disable();
|
|
expect(asyncLocalStorage.getStore()).toBe(undefined);
|
|
|
|
resolve(undefined);
|
|
} catch (e) {
|
|
reject(e);
|
|
}
|
|
});
|
|
} catch (e) {
|
|
reject(e);
|
|
}
|
|
});
|
|
|
|
await promise;
|
|
});
|
|
|
|
test("AsyncResource.prototype.bind", () => {
|
|
const localStorage = new AsyncLocalStorage<true>();
|
|
let ar!: AsyncResource;
|
|
localStorage.run(true, () => {
|
|
ar = new AsyncResource("test");
|
|
});
|
|
expect(ar.bind(() => localStorage.getStore())()).toBe(true);
|
|
});
|
|
|
|
test("AsyncResource.bind", () => {
|
|
const localStorage = new AsyncLocalStorage<true>();
|
|
let fn!: () => true | undefined;
|
|
localStorage.run(true, () => {
|
|
fn = AsyncResource.bind(() => localStorage.getStore());
|
|
});
|
|
expect(fn()).toBe(true);
|
|
});
|