Skip to content

Commit

Permalink
test: timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Natoandro committed Jan 16, 2025
1 parent 7838471 commit 6487117
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/typegate/src/runtimes/patterns/worker_manager/pooling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,10 @@ export class WaitQueueWithTimeout<W> implements WaitQueue<W> {
#cancelNextEntry() {
this.#queue.shift()!.cancellationHandler();
}

[Symbol.dispose]() {
if (this.#timerId != null) {
clearTimeout(this.#timerId);
}
}
}
56 changes: 52 additions & 4 deletions tests/patterns/worker_manager/pooling_test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,72 @@
// 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<number>();

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<number>(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");
}
});

0 comments on commit 6487117

Please sign in to comment.