mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 02:48:50 +00:00
* hell * make it so bun-debug-src * teag * wild * yippee * fas * fix async hooks assertions * yap * yeah that's wild * aa * a * increase time allowed * so trivial
53 lines
1.7 KiB
TypeScript
53 lines
1.7 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("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);
|
|
});
|