diff --git a/examples/async/src/server.ts b/examples/async/src/server.ts index 174b1020..50030a69 100644 --- a/examples/async/src/server.ts +++ b/examples/async/src/server.ts @@ -5,11 +5,10 @@ import { tsonOptions } from "./shared.js"; const tsonStringifyAsync = createTsonStringifyAsync(tsonOptions); -const randomNumber = (min: number, max: number) => { - return Math.floor(Math.random() * (max - min + 1) + min); -}; +const randomNumber = (min: number, max: number) => + Math.floor(Math.random() * (max - min + 1) + min); -const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); /** * This function returns the object we will be sending to the client. @@ -18,7 +17,7 @@ export function getResponseShape() { async function* bigintGenerator() { const iterate = new Array(10).fill(0).map((_, i) => BigInt(i)); for (const number of iterate) { - await wait(randomNumber(1, 400)); + await sleep(randomNumber(1, 400)); yield number; } } @@ -26,7 +25,7 @@ export function getResponseShape() { async function* numberGenerator() { const iterate = new Array(10).fill(0).map((_, i) => i); for (const number of iterate) { - await wait(randomNumber(1, 400)); + await sleep(randomNumber(1, 400)); yield number; } } diff --git a/src/async/deserializeAsync.test.ts b/src/async/deserializeAsync.test.ts index ec907cfd..afd6f33b 100644 --- a/src/async/deserializeAsync.test.ts +++ b/src/async/deserializeAsync.test.ts @@ -27,7 +27,7 @@ test("deserialize variable chunk length", async () => { }); { const iterable = (async function* () { - await new Promise((resolve) => setTimeout(resolve, 1)); + await sleep(1); yield '[\n{"json":{"foo":"bar"},"nonce":"__tson"}'; yield "\n,\n[\n]\n]"; })(); @@ -37,7 +37,7 @@ test("deserialize variable chunk length", async () => { { const iterable = (async function* () { - await new Promise((resolve) => setTimeout(resolve, 1)); + await sleep(1); yield '[\n{"json":{"foo":"bar"},"nonce":"__tson"}\n,\n[\n]\n]'; })(); const result = await tson.parse(iterable); @@ -46,7 +46,7 @@ test("deserialize variable chunk length", async () => { { const iterable = (async function* () { - await new Promise((resolve) => setTimeout(resolve, 1)); + await sleep(1); yield '[\n{"json"'; yield ':{"foo":"b'; yield 'ar"},"nonce":"__tson"}\n,\n'; @@ -100,13 +100,13 @@ test("stringify async iterable + promise", async () => { }); async function* iterable() { - await new Promise((resolve) => setTimeout(resolve, 1)); + await sleep(1); yield 1n; - await new Promise((resolve) => setTimeout(resolve, 1)); + await sleep(1); yield 2n; yield 3n; - await new Promise((resolve) => setTimeout(resolve, 2)); + await sleep(1); yield 4n; yield 5n; } @@ -138,7 +138,7 @@ test("e2e: stringify async iterable and promise over the network", async () => { function createMockObj() { async function* generator() { for (const number of [1n, 2n, 3n, 4n, 5n]) { - await new Promise((resolve) => setTimeout(resolve, 1)); + await sleep(1); yield number; } } @@ -234,7 +234,7 @@ test("iterator error", async () => { for (let index = 0; index < 3; index++) { yield `item: ${index}`; - await new Promise((resolve) => setTimeout(resolve, 1)); + await sleep(1); } // resolve the deferred after crash diff --git a/src/async/handlers/tsonPromise.test.ts b/src/async/handlers/tsonPromise.test.ts index 6539248d..af02d7b8 100644 --- a/src/async/handlers/tsonPromise.test.ts +++ b/src/async/handlers/tsonPromise.test.ts @@ -14,6 +14,7 @@ import { sleep, waitError, } from "../../internals/testUtils.js"; +import { createPromise } from "../../internals/testUtils.js"; import { createTsonParseAsyncInner } from "../deserializeAsync.js"; import { mapIterable, @@ -21,19 +22,6 @@ import { } from "../iterableUtils.js"; import { TsonAsyncValueTuple } from "../serializeAsync.js"; -const createPromise = (result: () => T, wait = 1) => { - return new Promise((resolve, reject) => { - setTimeout(() => { - try { - const res = result(); - resolve(res); - } catch (err) { - reject(err); - } - }, wait); - }); -}; - const tsonError: TsonType< Error, { diff --git a/src/async/serializeAsync.test.ts b/src/async/serializeAsync.test.ts index 48b77e93..aaa354f0 100644 --- a/src/async/serializeAsync.test.ts +++ b/src/async/serializeAsync.test.ts @@ -1,6 +1,7 @@ import { expect, test } from "vitest"; import { tsonAsyncIterator, tsonBigint, tsonPromise } from "../index.js"; +import { sleep } from "../internals/testUtils.js"; import { createAsyncTsonSerialize, createTsonStringifyAsync, @@ -106,9 +107,9 @@ test("serialize async iterable", async () => { }); async function* iterable() { - await new Promise((resolve) => setTimeout(resolve, 1)); + await sleep(1); yield 42; - await new Promise((resolve) => setTimeout(resolve, 1)); + await sleep(1); yield 43; } @@ -163,7 +164,7 @@ test("stringify async iterable + promise", async () => { }); async function* iterable() { - await new Promise((resolve) => setTimeout(resolve, 1)); + await sleep(1); yield 1n; yield 2n; } diff --git a/src/internals/testUtils.ts b/src/internals/testUtils.ts index 560a1b22..0183ff5d 100644 --- a/src/internals/testUtils.ts +++ b/src/internals/testUtils.ts @@ -36,8 +36,7 @@ export const waitFor = async (fn: () => unknown) => { await fn(); return; } catch { - // wait 5ms - await new Promise((resolve) => setTimeout(resolve, 5)); + await sleep(5); } } }; @@ -90,3 +89,15 @@ export function createDeferred() { export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); +export const createPromise = (result: () => T, wait = 1) => { + return new Promise((resolve, reject) => { + setTimeout(() => { + try { + const res = result(); + resolve(res); + } catch (err) { + reject(err); + } + }, wait); + }); +};