diff options
author | snek <snek@deno.com> | 2024-08-28 19:25:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-29 02:25:38 +0000 |
commit | f7556d8962b2106e82c9f0eb90f406eec0b60dd0 (patch) | |
tree | ed8cc065b713ed17750be1170d51725801565bd7 /tests | |
parent | 4f97261a012edda267ff6b74c5b0b6d08a1c12b8 (diff) |
fix: reland async context (#25140)
This reverts commit 71ca61e189cca9215982ce4598b7a4da8430c584.
Now uses a shared implementation from deno_core.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit_node/async_hooks_test.ts | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/tests/unit_node/async_hooks_test.ts b/tests/unit_node/async_hooks_test.ts index f153f6753..91130972c 100644 --- a/tests/unit_node/async_hooks_test.ts +++ b/tests/unit_node/async_hooks_test.ts @@ -1,5 +1,7 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import { AsyncLocalStorage, AsyncResource } from "node:async_hooks"; +import process from "node:process"; +import { setImmediate } from "node:timers"; import { assert, assertEquals } from "@std/assert"; Deno.test(async function foo() { @@ -92,7 +94,7 @@ Deno.test(async function enterWith() { }); assertEquals(await deferred.promise, { x: 2 }); - assertEquals(await deferred1.promise, { x: 1 }); + assertEquals(await deferred1.promise, null); }); Deno.test(async function snapshot() { @@ -135,3 +137,26 @@ Deno.test(function emitDestroyStub() { const resource = new AsyncResource("foo"); assert(typeof resource.emitDestroy === "function"); }); + +Deno.test(async function worksWithAsyncAPIs() { + const store = new AsyncLocalStorage(); + const test = () => assertEquals(store.getStore(), "data"); + await store.run("data", async () => { + test(); + queueMicrotask(() => test()); + process.nextTick(() => test()); + setImmediate(() => test()); + setTimeout(() => test(), 0); + const intervalId = setInterval(() => { + test(); + clearInterval(intervalId); + }, 0); + + store.run("data2", () => { + assertEquals(store.getStore(), "data2"); + }); + + await new Promise((r) => setTimeout(r, 50)); + test(); + }); +}); |