Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

1.3 KB
Raw Blame
import { expect, test } from "vitest";
import { createExclusiveAsyncRunner } from "./async-utils";

test("runs asynchronous work exclusively in call order", async () => {
  const runExclusive = createExclusiveAsyncRunner();
  const events: string[] = [];
  let releaseFirst: () => void = () => undefined;
  let markFirstStarted: () => void = () => undefined;
  const firstStarted = new Promise<void>((resolve) => {
    markFirstStarted = resolve;
  });
  const firstBlocked = new Promise<void>((resolve) => {
    releaseFirst = resolve;
  });

  const first = runExclusive(async () => {
    events.push("first:start");
    markFirstStarted();
    await firstBlocked;
    events.push("first:end");
  });
  const second = runExclusive(async () => {
    events.push("second");
  });
  await firstStarted;
  expect(events).toEqual(["first:start"]);

  releaseFirst();
  await Promise.all([first, second]);
  expect(events).toEqual(["first:start", "first:end", "second"]);
});

test("continues after a failed run", async () => {
  const runExclusive = createExclusiveAsyncRunner();

  await expect(
    runExclusive(async () => {
      throw new Error("failed");
    })
  ).rejects.toThrow("failed");
  await expect(runExclusive(async () => "complete")).resolves.toBe("complete");
});