Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

HookInboxStore.test.ts

252 lines10.0 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3import * as Clock from "effect/Clock";
4import * as DateTime from "effect/DateTime";
5import * as Duration from "effect/Duration";
6import * as Effect from "effect/Effect";
7import * as SqlClient from "effect/sql/SqlClient";
8import * as TestClock from "effect/testing/TestClock";
9​
10import * as HookInboxStore from "./HookInboxStore.ts";
11​
12const BASE_URL = "https://env.example.test/";
13​
14const hook = (id: string, overrides: Partial<HookInboxStore.HeldHook> = {}) =>
15 Effect.map(Clock.currentTimeMillis, (now): HookInboxStore.HeldHook => ({
16 id,
17 receivedAt: DateTime.formatIso(DateTime.makeUnsafe(now)),
18 method: "POST",
19 rawHookId: "hook-1",
20 hookKey: "hook-1",
21 rawToken: "tok%2Fen",
22 query: "a=1",
23 headers: { "content-type": "application/json", "x-sig": "s" },
24 body: new Uint8Array([0, 255, 10]),
25 ...overrides,
26 }));
27​
28const deliverer = (outcome: (hook: HookInboxStore.HeldHook) => HookInboxStore.DeliveryOutcome) => {
29 const sent: Array<{ readonly baseUrl: string; readonly hook: HookInboxStore.HeldHook }> = [];
30 const send = (baseUrl: string, held: HookInboxStore.HeldHook) =>
31 Effect.sync(() => {
32 sent.push({ baseUrl, hook: held });
33 return outcome(held);
34 });
35 return { sent, send };
36};
37​
38const withInbox = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
39 HookInboxStore.migrate.pipe(
40 Effect.andThen(effect),
41 Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" })),
42 );
43​
44describe("HookInboxStore", () => {
45 it.effect("delivers held requests oldest first, byte for byte", () =>
46 withInbox(
47 Effect.gen(function* () {
48 const first = yield* hook("first");
49 expect(yield* HookInboxStore.hold(first, BASE_URL)).not.toBeNull();
50 yield* HookInboxStore.hold(yield* hook("second"), BASE_URL);
51 const { sent, send } = deliverer(() => "delivered");
52 expect(yield* HookInboxStore.deliverDue(send)).toBeNull();
53 expect(sent.map((entry) => entry.hook.id)).toEqual(["first", "second"]);
54 expect(sent[0]).toEqual({ baseUrl: BASE_URL, hook: first });
55 expect(yield* HookInboxStore.deliverDue(send)).toBeNull();
56 expect(sent).toHaveLength(2);
57 }),
58 ),
59 );
60​
61 it.effect("keeps a request the environment did not take and backs off", () =>
62 withInbox(
63 Effect.gen(function* () {
64 yield* HookInboxStore.hold(yield* hook("first"), BASE_URL);
65 yield* HookInboxStore.hold(yield* hook("second"), BASE_URL);
66 const offline = deliverer(() => "unreachable");
67 const now = yield* Clock.currentTimeMillis;
68 expect(yield* HookInboxStore.deliverDue(offline.send)).toBe(now + 10_000);
69 expect(offline.sent.map((entry) => entry.hook.id)).toEqual(["first"]);
70 expect(yield* HookInboxStore.deliverDue(offline.send)).toBe(now + 10_000);
71​
72 const online = deliverer(() => "delivered");
73 expect(yield* HookInboxStore.deliverDue(online.send)).toBeNull();
74 expect(online.sent.map((entry) => entry.hook.id)).toEqual(["first", "second"]);
75 }),
76 ),
77 );
78​
79 it.effect("lets other hooks through while one hook's environment is busy", () =>
80 withInbox(
81 Effect.gen(function* () {
82 const stuck = { rawHookId: "stuck", hookKey: "stuck" };
83 yield* HookInboxStore.hold(yield* hook("stuck-1", stuck), BASE_URL);
84 yield* HookInboxStore.hold(yield* hook("other-1"), BASE_URL);
85 yield* HookInboxStore.hold(yield* hook("stuck-2", stuck), BASE_URL);
86 const busy = deliverer((held) => (held.hookKey === "stuck" ? "busy" : "delivered"));
87 const now = yield* Clock.currentTimeMillis;
88 expect(yield* HookInboxStore.deliverDue(busy.send)).toBe(now);
89 expect(busy.sent.map((entry) => entry.hook.id)).toEqual(["stuck-1", "other-1"]);
90 expect(yield* HookInboxStore.deliverDue(busy.send)).toBe(now + 30_000);
91​
92 const drained = deliverer(() => "delivered");
93 expect(yield* HookInboxStore.deliverDue(drained.send)).toBeNull();
94 expect(drained.sent.map((entry) => entry.hook.id)).toEqual(["stuck-1", "stuck-2"]);
95 }),
96 ),
97 );
98​
99 it.effect("reaches a hook queued behind busy hooks' full backlogs in the same run", () =>
100 withInbox(
101 Effect.gen(function* () {
102 for (const name of ["stuck-a", "stuck-b"]) {
103 const stuck = { rawHookId: name, hookKey: name };
104 for (let index = 0; index < HookInboxStore.HOOK_INBOX_MAX_PER_HOOK; index++) {
105 yield* HookInboxStore.hold(yield* hook(`${name}-${index}`, stuck), BASE_URL);
106 }
107 }
108 yield* HookInboxStore.hold(yield* hook("other-1"), BASE_URL);
109 const busy = deliverer((held) => (held.hookKey.startsWith("stuck") ? "busy" : "delivered"));
110 yield* HookInboxStore.deliverDue(busy.send);
111 expect(busy.sent.map((entry) => entry.hook.id)).toEqual([
112 "stuck-a-0",
113 "stuck-b-0",
114 "other-1",
115 ]);
116 }),
117 ),
118 );
119​
120 it.effect("drops a held request it cannot read instead of stalling on it", () =>
121 withInbox(
122 Effect.gen(function* () {
123 yield* HookInboxStore.hold(yield* hook("broken"), BASE_URL);
124 yield* HookInboxStore.hold(yield* hook("fine"), BASE_URL);
125 const sql = yield* SqlClient.SqlClient;
126 yield* sql`UPDATE held_hooks SET headers = 'not json' WHERE id = 'broken'`;
127 const { sent, send } = deliverer(() => "delivered");
128 expect(yield* HookInboxStore.deliverDue(send)).toBeNull();
129 expect(sent.map((entry) => entry.hook.id)).toEqual(["fine"]);
130 }),
131 ),
132 );
133​
134 it.effect("retries every 10 s for 3 minutes, then backs off to 10 minutes", () =>
135 Effect.gen(function* () {
136 const delays = yield* Effect.forEach(
137 [1, 18, 19, 20, 21, 23, 24, 40],
138 HookInboxStore.retryDelayMs,
139 );
140 expect(delays).toEqual([10_000, 10_000, 30_000, 60_000, 120_000, 480_000, 600_000, 600_000]);
141 }),
142 );
143​
144 it.effect("waking resets the backoff and reports whether anything waits", () =>
145 withInbox(
146 Effect.gen(function* () {
147 expect(yield* HookInboxStore.wake(BASE_URL)).toBe(false);
148 yield* HookInboxStore.hold(yield* hook("first"), "https://old.example.test/");
149 const offline = deliverer(() => "unreachable");
150 yield* HookInboxStore.deliverDue(offline.send);
151 yield* HookInboxStore.deliverDue(offline.send);
152​
153 for (let failure = 0; failure < 20; failure++) {
154 yield* HookInboxStore.deliverDue(offline.send);
155 }
156 const now = yield* Clock.currentTimeMillis;
157 expect(yield* HookInboxStore.deliverDue(offline.send)).toBeGreaterThan(now + 60_000);
158​
159 // A wake means its tunnel just connected, which Cloudflare may not
160 // route to for a few minutes: back to retrying every 10 s, sent to
161 // where the environment is now.
162 expect(yield* HookInboxStore.wake(BASE_URL)).toBe(true);
163 expect(yield* HookInboxStore.deliverDue(offline.send)).toBe(now + 10_000);
164 expect(offline.sent.at(-1)?.baseUrl).toBe(BASE_URL);
165 }),
166 ),
167 );
168​
169 it.effect("starts the schedule over once the inbox drains", () =>
170 withInbox(
171 Effect.gen(function* () {
172 yield* HookInboxStore.hold(yield* hook("old"), BASE_URL);
173 const offline = deliverer(() => "unreachable");
174 for (let failure = 0; failure < 30; failure++) {
175 yield* HookInboxStore.deliverDue(offline.send);
176 }
177 yield* TestClock.adjust(Duration.hours(25));
178 expect(yield* HookInboxStore.deliverDue(offline.send)).toBeNull();
179​
180 const now = yield* Clock.currentTimeMillis;
181 expect(yield* HookInboxStore.hold(yield* hook("new"), BASE_URL)).toBe(now + 10_000);
182 }),
183 ),
184 );
185​
186 it.effect("drops requests older than 24 hours", () =>
187 withInbox(
188 Effect.gen(function* () {
189 yield* HookInboxStore.hold(yield* hook("old"), BASE_URL);
190 yield* TestClock.adjust(Duration.hours(1));
191 yield* HookInboxStore.hold(yield* hook("new"), BASE_URL);
192 yield* TestClock.adjust(Duration.minutes(23 * 60 + 1));
193 const { sent, send } = deliverer(() => "delivered");
194 yield* HookInboxStore.deliverDue(send);
195 expect(sent.map((entry) => entry.hook.id)).toEqual(["new"]);
196 }),
197 ),
198 );
199​
200 it.effect("refuses requests past the per-hook cap", () =>
201 withInbox(
202 Effect.gen(function* () {
203 for (let index = 0; index < HookInboxStore.HOOK_INBOX_MAX_PER_HOOK; index++) {
204 expect(yield* HookInboxStore.hold(yield* hook(`a-${index}`), BASE_URL)).not.toBeNull();
205 }
206 expect(yield* HookInboxStore.hold(yield* hook("one-too-many"), BASE_URL)).toBeNull();
207 const other = yield* hook("other", { rawHookId: "hook-2", hookKey: "hook-2" });
208 expect(yield* HookInboxStore.hold(other, BASE_URL)).not.toBeNull();
209 }),
210 ),
211 );
212​
213 it.effect("refuses a request that would pass the byte cap", () =>
214 withInbox(
215 Effect.gen(function* () {
216 const big = new Uint8Array(HookInboxStore.HOOK_INBOX_MAX_BYTES - 10);
217 expect(
218 yield* HookInboxStore.hold(yield* hook("big", { body: big }), BASE_URL),
219 ).not.toBeNull();
220 const small = new Uint8Array(11);
221 expect(
222 yield* HookInboxStore.hold(
223 yield* hook("small", { rawHookId: "x", hookKey: "x", body: small }),
224 BASE_URL,
225 ),
226 ).toBeNull();
227 }),
228 ),
229 );
230​
231 it.effect("stores a request id once", () =>
232 withInbox(
233 Effect.gen(function* () {
234 const first = yield* hook("same");
235 expect(yield* HookInboxStore.hold(first, BASE_URL)).not.toBeNull();
236 expect(yield* HookInboxStore.hold(first, BASE_URL)).toBeNull();
237 }),
238 ),
239 );
240​
241 it.effect("clear drops everything held", () =>
242 withInbox(
243 Effect.gen(function* () {
244 yield* HookInboxStore.hold(yield* hook("first"), BASE_URL);
245 yield* HookInboxStore.clear;
246 const { sent, send } = deliverer(() => "delivered");
247 expect(yield* HookInboxStore.deliverDue(send)).toBeNull();
248 expect(sent).toHaveLength(0);
249 }),
250 ),
251 );
252});