From 6487117289aa2aebf45b622efdbaaf6201a4c1e6 Mon Sep 17 00:00:00 2001 From: Natoandro Date: Thu, 16 Jan 2025 12:37:26 +0300 Subject: [PATCH] test: timeout --- .../patterns/worker_manager/pooling.ts | 6 ++ tests/patterns/worker_manager/pooling_test.ts | 56 +++++++++++++++++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/typegate/src/runtimes/patterns/worker_manager/pooling.ts b/src/typegate/src/runtimes/patterns/worker_manager/pooling.ts index 859a230ab..aa245e8d9 100644 --- a/src/typegate/src/runtimes/patterns/worker_manager/pooling.ts +++ b/src/typegate/src/runtimes/patterns/worker_manager/pooling.ts @@ -93,4 +93,10 @@ export class WaitQueueWithTimeout implements WaitQueue { #cancelNextEntry() { this.#queue.shift()!.cancellationHandler(); } + + [Symbol.dispose]() { + if (this.#timerId != null) { + clearTimeout(this.#timerId); + } + } } diff --git a/tests/patterns/worker_manager/pooling_test.ts b/tests/patterns/worker_manager/pooling_test.ts index 43983aceb..2b2fa9205 100644 --- a/tests/patterns/worker_manager/pooling_test.ts +++ b/tests/patterns/worker_manager/pooling_test.ts @@ -1,8 +1,12 @@ // Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0. // SPDX-License-Identifier: MPL-2.0 -import { createSimpleWaitQueue } from "@metatype/typegate/runtimes/patterns/worker_manager/pooling.ts"; -import { assert, assertEquals, assertFalse } from "@std/assert"; +import { + createSimpleWaitQueue, + WaitQueueWithTimeout, +} from "@metatype/typegate/runtimes/patterns/worker_manager/pooling.ts"; +import { assert, assertEquals, assertFalse, assertRejects } from "@std/assert"; +import { delay } from "@std/async/delay"; Deno.test("simple wait queue", (t) => { const queue = createSimpleWaitQueue(); @@ -10,15 +14,59 @@ Deno.test("simple wait queue", (t) => { const history: number[] = []; assertFalse(queue.shift(() => 1)); + + queue.push((v) => history.push(v), () => {}); + assertEquals(history.length, 0); + assert(queue.shift(() => 2)); + assertEquals(history.length, 1); + assertEquals(history[0], 2); + + assertFalse(queue.shift(() => 1)); + + queue.push((v) => history.push(v), () => {}); + assertEquals(history.length, 1); + assert(queue.shift(() => 3)); + assertEquals(history.length, 2); + assertEquals(history[1], 3); +}); + +Deno.test.only("wait queue with timeout", async (t) => { + using queue = new WaitQueueWithTimeout(100); + + const history: number[] = []; + + assertFalse(queue.shift(() => 1)); + queue.push((v) => history.push(v), () => {}); assertEquals(history.length, 0); - queue.shift(() => 2); + assert(queue.shift(() => 2)); assertEquals(history.length, 1); assertEquals(history[0], 2); + assertFalse(queue.shift(() => 1)); + queue.push((v) => history.push(v), () => {}); assertEquals(history.length, 1); - queue.shift(() => 3); + assert(queue.shift(() => 3)); assertEquals(history.length, 2); assertEquals(history[1], 3); + + { + const resolvers = Promise.withResolvers(); + queue.push(resolvers.resolve, () => resolvers.reject(new Error("timeout"))); + assertEquals(history.length, 2); + await assertRejects( + () => + Promise.race([ + delay(100).then(() => false), + resolvers.promise.then(() => true), + ]), + Error, + "timeout", + ); + await delay(100); + // await delay(100); + // assertEquals(history.length, 2); + // await assertRejects(() => resolvers.promise, "timeout"); + } });