File size: 4,192 Bytes
6aa76ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { describe, expect, it, vi } from "vitest";

import { createCronTriggerController } from "@hermes/shared";

function deferred<T>() {
  let resolve!: (value: T) => void;
  let reject!: (error: unknown) => void;
  const promise = new Promise<T>((res, rej) => {
    resolve = res;
    reject = rej;
  });

  return { promise, reject, resolve };
}

describe("createCronTriggerController", () => {
  it("announces immediately and coalesces the same job while it is running", async () => {
    const request = deferred<string>();
    const order: string[] = [];
    const action = vi.fn(() => {
      order.push("action");
      return request.promise;
    });
    const onStarted = vi.fn(() => order.push("started"));
    const onRunningChange = vi.fn();
    const controller = createCronTriggerController(onRunningChange);

    const first = controller.run("profile-a:job-1", action, onStarted);
    const duplicate = await controller.run("profile-a:job-1", action, onStarted);

    expect(action).toHaveBeenCalledTimes(1);
    expect(onStarted).toHaveBeenCalledTimes(1);
    expect(order).toEqual(["started", "action"]);
    expect(onRunningChange).toHaveBeenNthCalledWith(1, "profile-a:job-1", true);
    expect(duplicate).toEqual({ started: false, value: null });

    request.resolve("done");
    await expect(first).resolves.toEqual({ started: true, value: "done" });
    expect(onRunningChange).toHaveBeenLastCalledWith("profile-a:job-1", false);
  });

  it("releases the job after failure so a retry can start", async () => {
    const request = deferred<void>();
    const controller = createCronTriggerController();

    const failed = controller.run("job-1", () => request.promise);
    request.reject(new Error("failed"));

    await expect(failed).rejects.toThrow("failed");
    await expect(controller.run("job-1", async () => "retried")).resolves.toEqual({
      started: true,
      value: "retried",
    });
  });

  it("releases the job when the immediate feedback callback fails", async () => {
    const controller = createCronTriggerController();

    await expect(
      controller.run("job-1", async () => "not-called", () => {
        throw new Error("toast failed");
      }),
    ).rejects.toThrow("toast failed");

    await expect(controller.run("job-1", async () => "retried")).resolves.toEqual({
      started: true,
      value: "retried",
    });
  });

  it("allows the same job id in different profiles to run concurrently", async () => {
    const defaultRequest = deferred<string>();
    const workRequest = deferred<string>();
    const defaultAction = vi.fn(() => defaultRequest.promise);
    const workAction = vi.fn(() => workRequest.promise);
    const controller = createCronTriggerController();

    const defaultRun = controller.run("default:job-1", defaultAction);
    const workRun = controller.run("work:job-1", workAction);

    expect(defaultAction).toHaveBeenCalledTimes(1);
    expect(workAction).toHaveBeenCalledTimes(1);

    defaultRequest.resolve("default");
    workRequest.resolve("work");

    await expect(defaultRun).resolves.toEqual({ started: true, value: "default" });
    await expect(workRun).resolves.toEqual({ started: true, value: "work" });
  });

  it("releases the job when the running-state callback fails", async () => {
    const controller = createCronTriggerController((_key, running) => {
      if (running) throw new Error("state callback failed");
    });

    await expect(controller.run("job-1", async () => "not-called")).rejects.toThrow(
      "state callback failed",
    );
    expect(controller.isRunning("job-1")).toBe(false);
  });

  it("releases the job before the stopped-state callback runs", async () => {
    const action = vi.fn(async () => "done");
    const controller = createCronTriggerController((_key, running) => {
      if (!running) throw new Error("stopped callback failed");
    });

    await expect(controller.run("job-1", action)).rejects.toThrow("stopped callback failed");
    expect(controller.isRunning("job-1")).toBe(false);

    await expect(controller.run("job-1", action)).rejects.toThrow("stopped callback failed");
    expect(action).toHaveBeenCalledTimes(2);
  });
});