Unverified Commit c1aa2003 authored by Hong Minhee's avatar Hong Minhee
Browse files

Tests for MemoryKvStore & InProcessMessageQueue

parent 00812d7f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@
    "@js-temporal/polyfill": "npm:@js-temporal/polyfill@^0.4.4",
    "@phensley/language-tag": "npm:@phensley/language-tag@^1.8.0",
    "@std/assert": "jsr:@std/assert@^0.220.1",
    "@std/async/delay": "jsr:@std/async@^0.220.1/delay",
    "@std/bytes": "jsr:@std/bytes@^0.220.1",
    "@std/collections": "jsr:@std/collections@^0.220.1",
    "@std/encoding": "jsr:@std/encoding@^0.220.1",

federation/kv.test.ts

0 → 100644
+23 −0
Original line number Diff line number Diff line
import { Temporal } from "@js-temporal/polyfill";
import { assertEquals } from "@std/assert";
import { MemoryKvStore } from "./kv.ts";

Deno.test("MemoryKvStore", async (t) => {
  const store = new MemoryKvStore();

  await t.step("set() & get()", async () => {
    await store.set(["foo", "bar"], "foobar");
    assertEquals(await store.get(["foo", "bar"]), "foobar");
    assertEquals(await store.get(["foo"]), undefined);

    await store.set(["foo", "baz"], "baz", {
      ttl: Temporal.Duration.from({ seconds: 0 }),
    });
    assertEquals(await store.get(["foo", "baz"]), undefined);
  });

  await t.step("delete()", async () => {
    await store.delete(["foo", "bar"]);
    assertEquals(await store.get(["foo", "bar"]), undefined);
  });
});

federation/mq.test.ts

0 → 100644
+52 −0
Original line number Diff line number Diff line
import { Temporal } from "@js-temporal/polyfill";
import { assertEquals, assertGreater } from "@std/assert";
import { delay } from "@std/async/delay";
import { InProcessMessageQueue } from "./mq.ts";

Deno.test("InProcessMessageQueue", async (t) => {
  const mq = new InProcessMessageQueue();

  const messages: string[] = [];
  mq.listen((message: string) => {
    messages.push(message);
  });

  await t.step("enqueue()", async () => {
    await mq.enqueue("Hello, world!");
  });

  await waitFor(() => messages.length > 0, 15_000);

  await t.step("listen()", () => {
    assertEquals(messages, ["Hello, world!"]);
  });

  let started = 0;
  await t.step("enqueue() with delay", async () => {
    started = Date.now();
    await mq.enqueue(
      "Delayed message",
      { delay: Temporal.Duration.from({ seconds: 3 }) },
    );
  });

  await waitFor(() => messages.length > 1, 15_000);

  await t.step("listen() with delay", () => {
    assertEquals(messages, ["Hello, world!", "Delayed message"]);
    assertGreater(Date.now() - started, 3_000);
  });
});

async function waitFor(
  predicate: () => boolean,
  timeoutMs: number,
): Promise<void> {
  const started = Date.now();
  while (!predicate()) {
    await delay(500);
    if (Date.now() - started > timeoutMs) {
      throw new Error("Timeout");
    }
  }
}