Skip to content

Commit 62cf836

Browse files
avalleteclaude
andauthored
test(stack): fix 1 flaky Mailpit test and share the stall guard (supabase#7318)
## TL;DR The Mailpit launch test hung on Podman without saying where. It now fails within its timeout, naming the phase that stalled and Mailpit's output. The detached stall guard it uses is now one shared test helper, `failOnStall`, which the Realtime and native owner-logs tests also use. - `Mail.integration.test.ts › launches the real Mailpit recipe and serves its HTTP endpoint`: on Podman, the test hit the 30s test timeout with no diagnostic. The error frame pointed at `sendTestEmail`, but this test never sends mail, so that frame is misleading. The test waits on start, readiness and a `/readyz` request. Container readiness may wait up to 60s, longer than the test timeout, and the `/readyz` request has no bound. So a stall in any of the three looked the same. The whole test scope now runs under one 25s guard inside the 30s test timeout, and a `Ref` tracks the current phase. The test also collects the recipe's log stream, so a stall fails with `MailStallError` naming the phase and showing Mailpit's output so far. - `packages/stack/tests/stall-guard.ts` adds `failOnStall(duration, onStall)`. It joins the guarded effect from a detached fiber, so the stall error surfaces at the deadline instead of waiting for teardown, and it interrupts the stalled fiber in the background so scoped cleanup still runs. `Mail`, `Realtime` (supabase#7359) and `Owner.logs` (supabase#7434) integration tests now share it instead of carrying three copies; each keeps its own stall message. ## ref spotted in: - https://github.com/supabase/cli/actions/runs/39920135558/job/120303098411 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent cf193a1 commit 62cf836

4 files changed

Lines changed: 116 additions & 83 deletions

File tree

‎packages/stack/src/Owner.logs.integration.test.ts‎

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as LogStore from "./host/LogStore.ts";
2626
import * as StackNamespace from "./StackNamespace.ts";
2727
import type { SavedStack } from "./StackNamespace.ts";
2828
import { selection } from "../tests/pins.ts";
29+
import { failOnStall } from "../tests/stall-guard.ts";
2930

3031
const services = Layer.merge(NodeServices.layer, NodeHttpClient.layerNodeHttp);
3132

@@ -108,39 +109,31 @@ type NativeMailPhase = {
108109
};
109110

110111
/** Fails a stalled native Mailpit step within the test timeout, naming the step and its persisted output. */
111-
const guardNativeMail =
112-
(phase: Ref.Ref<NativeMailPhase>) =>
113-
<A, E, R>(effect: Effect.Effect<A, E, R>) =>
114-
Effect.flatMap(Effect.forkDetach(effect), (fiber) =>
115-
Fiber.join(fiber).pipe(
116-
Effect.onInterrupt(() => Effect.forkDetach(Fiber.interrupt(fiber))),
117-
Effect.timeoutOrElse({
118-
duration: "25 seconds",
119-
orElse: () =>
120-
Effect.gen(function* () {
121-
const { step, id, history } = yield* Ref.get(phase);
122-
const output =
123-
id === undefined || history === undefined
124-
? []
125-
: yield* history(id).pipe(
126-
Stream.filter(isOutput),
127-
Stream.runCollect,
128-
Effect.timeoutOrElse({
129-
duration: "2 seconds",
130-
orElse: () => Effect.succeed([]),
131-
}),
132-
Effect.orElseSucceed(() => []),
133-
);
134-
const text = Array.from(output, (record) => record.text ?? "")
135-
.join("")
136-
.slice(-2_000);
137-
return yield* new NativeMailStallError({
138-
message: `native Mailpit ${step}${id === undefined ? "" : ` (${id})`} did not finish within 25 seconds; output:\n${text || "(none)"}`,
139-
});
140-
}),
141-
}),
142-
),
143-
);
112+
const guardNativeMail = (phase: Ref.Ref<NativeMailPhase>) =>
113+
failOnStall(
114+
"25 seconds",
115+
Effect.gen(function* () {
116+
const { step, id, history } = yield* Ref.get(phase);
117+
const output =
118+
id === undefined || history === undefined
119+
? []
120+
: yield* history(id).pipe(
121+
Stream.filter(isOutput),
122+
Stream.runCollect,
123+
Effect.timeoutOrElse({
124+
duration: "2 seconds",
125+
orElse: () => Effect.succeed([]),
126+
}),
127+
Effect.orElseSucceed(() => []),
128+
);
129+
const text = Array.from(output, (record) => record.text ?? "")
130+
.join("")
131+
.slice(-2_000);
132+
return yield* new NativeMailStallError({
133+
message: `native Mailpit ${step}${id === undefined ? "" : ` (${id})`} did not finish within 25 seconds; output:\n${text || "(none)"}`,
134+
});
135+
}),
136+
);
144137

145138
/** Follows from the oldest record until `count` launch markers are persisted. */
146139
const awaitLaunches = (records: Stream.Stream<LogRecord, StackError>, count: number) =>

‎packages/stack/src/services/Mail.integration.test.ts‎

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { NodeHttpClient, NodeServices } from "@effect/platform-node";
22
import { describe, expect, it } from "@effect/vitest";
3-
import { Effect, Exit, FileSystem, Layer, Path } from "effect";
3+
import { Data, Effect, Exit, FileSystem, Layer, Path, Ref, Stream } from "effect";
44
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
55
import { makeStandaloneService } from "../../tests/standalone-service.ts";
66
import { makeServiceRecipe } from "./Catalog.ts";
77
import { engineTarget, testEngine } from "../../tests/engine-target.ts";
88
import { httpHost } from "../../tests/helpers/endpoint.ts";
99
import { selection } from "../../tests/pins.ts";
10+
import { failOnStall } from "../../tests/stall-guard.ts";
1011

1112
const options = (root: string) => ({
1213
stackId: "catalog-mail",
@@ -59,31 +60,63 @@ const sendTestEmail = (
5960
yield* HttpClientResponse.filterStatusOk(response);
6061
});
6162

63+
class MailStallError extends Data.TaggedError("MailStallError")<{ readonly message: string }> {}
64+
65+
const mailStall = (duration: `${number} seconds`, phase: Ref.Ref<string>, logs: Ref.Ref<string>) =>
66+
failOnStall(
67+
duration,
68+
Effect.all([Ref.get(phase), Ref.get(logs)]).pipe(
69+
Effect.flatMap(([stalled, output]) =>
70+
Effect.fail(
71+
new MailStallError({
72+
message: `Mailpit ${stalled} did not finish within ${duration}; output:\n${output || "(none)"}`,
73+
}),
74+
),
75+
),
76+
),
77+
);
78+
6279
describe("service catalog", () => {
6380
it.live("launches the real Mailpit recipe and serves its HTTP endpoint", () =>
64-
Effect.scoped(
65-
Effect.gen(function* () {
66-
const fs = yield* FileSystem.FileSystem;
67-
const client = yield* HttpClient.HttpClient;
68-
const root = yield* fs.makeTempDirectoryScoped({ prefix: "catalog-mail-" });
69-
const recipe = yield* makeServiceRecipe(
70-
{ service: "mail", pin: selection.mail, config: {} },
71-
dockerOptions(root),
72-
);
73-
const instance = yield* makeStandaloneService(recipe.definition, {
74-
id: "mail",
75-
config: recipe.creation,
76-
});
77-
yield* instance.start;
78-
yield* instance.ready;
79-
const endpoint = yield* recipe.endpoint("http");
80-
const response = yield* client.execute(
81-
HttpClientRequest.get(`http://${httpHost(endpoint)}:${endpoint.port}/readyz`),
82-
);
83-
expect(response.status).toBe(200);
84-
yield* instance.stop;
85-
}),
86-
).pipe(Effect.provide(Layer.merge(NodeServices.layer, NodeHttpClient.layerNodeHttp))),
81+
Effect.gen(function* () {
82+
const phase = yield* Ref.make("setup");
83+
const logs = yield* Ref.make("");
84+
yield* Effect.scoped(
85+
Effect.gen(function* () {
86+
const fs = yield* FileSystem.FileSystem;
87+
const client = yield* HttpClient.HttpClient;
88+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "catalog-mail-" });
89+
const recipe = yield* makeServiceRecipe(
90+
{ service: "mail", pin: selection.mail, config: {} },
91+
dockerOptions(root),
92+
);
93+
const decoders = { stdout: new TextDecoder(), stderr: new TextDecoder() };
94+
yield* Stream.fromSubscription(yield* recipe.logs).pipe(
95+
Stream.runForEach(({ stream, bytes }) =>
96+
Ref.update(logs, (text) => text + decoders[stream].decode(bytes, { stream: true })),
97+
),
98+
Effect.forkScoped({ startImmediately: true }),
99+
);
100+
const instance = yield* makeStandaloneService(recipe.definition, {
101+
id: "mail",
102+
config: recipe.creation,
103+
});
104+
yield* Ref.set(phase, "start");
105+
yield* instance.start;
106+
yield* Ref.set(phase, "readiness");
107+
yield* instance.ready;
108+
yield* Ref.set(phase, "/readyz request");
109+
const endpoint = yield* recipe.endpoint("http");
110+
const response = yield* client.execute(
111+
HttpClientRequest.get(`http://${httpHost(endpoint)}:${endpoint.port}/readyz`),
112+
);
113+
expect(response.status).toBe(200);
114+
yield* Ref.set(phase, "stop");
115+
yield* instance.stop;
116+
yield* Ref.set(phase, "scope teardown");
117+
}),
118+
).pipe(mailStall("25 seconds", phase, logs));
119+
}).pipe(Effect.provide(Layer.merge(NodeServices.layer, NodeHttpClient.layerNodeHttp))),
87120
);
88121

89122
it.live(

‎packages/stack/src/services/Realtime.integration.test.ts‎

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NodeHttpClient, NodeServices } from "@effect/platform-node";
22
import { describe, expect, it } from "@effect/vitest";
3-
import { Data, Effect, Fiber, Layer, Redacted, Ref, Stream } from "effect";
3+
import { Data, Effect, Layer, Redacted, Ref, Stream } from "effect";
44
import { HttpClient, HttpClientRequest } from "effect/http";
55
import { makeStandaloneService } from "../../tests/standalone-service.ts";
66
import { makeServiceRecipe } from "./Catalog.ts";
@@ -10,6 +10,7 @@ import { makeDockerDatabaseRoot } from "../../tests/docker-fixture.ts";
1010
import { engineTarget, testEngine } from "../../tests/engine-target.ts";
1111
import { httpHost } from "../../tests/helpers/endpoint.ts";
1212
import { selection } from "../../tests/pins.ts";
13+
import { failOnStall } from "../../tests/stall-guard.ts";
1314

1415
const options = (root: string) => ({
1516
stackId: "catalog-realtime",
@@ -51,33 +52,24 @@ const makeLaunchTrace = Effect.gen(function* () {
5152
);
5253
});
5354
const enter = (service: string, step: string) => Ref.set(phase, { service, step });
54-
/** Reports a stall without waiting for the stalled scope's finalizers, which may hang too. */
55-
const guard =
56-
(duration: `${number} seconds`) =>
57-
<A, E, R>(effect: Effect.Effect<A, E, R>) =>
58-
Effect.flatMap(Effect.forkDetach(effect), (fiber) =>
59-
Fiber.join(fiber).pipe(
60-
Effect.onInterrupt(() => Effect.forkDetach(Fiber.interrupt(fiber))),
61-
Effect.timeoutOrElse({
62-
duration,
63-
orElse: () =>
64-
Effect.all([Ref.get(phase), Ref.get(outputs)]).pipe(
65-
Effect.flatMap(([{ service, step }, output]) =>
66-
Effect.fail(
67-
new CatalogStallError({
68-
message: [
69-
`${service} ${step} did not finish within ${duration}`,
70-
...(service === "database" ? [service] : [service, "database"]).map(
71-
(name) => `${name} output:\n${output.get(name) || "(none)"}`,
72-
),
73-
].join("\n"),
74-
}),
75-
),
55+
const guard = (duration: `${number} seconds`) =>
56+
failOnStall(
57+
duration,
58+
Effect.all([Ref.get(phase), Ref.get(outputs)]).pipe(
59+
Effect.flatMap(([{ service, step }, output]) =>
60+
Effect.fail(
61+
new CatalogStallError({
62+
message: [
63+
`${service} ${step} did not finish within ${duration}`,
64+
...(service === "database" ? [service] : [service, "database"]).map(
65+
(name) => `${name} output:\n${output.get(name) || "(none)"}`,
7666
),
77-
),
78-
}),
67+
].join("\n"),
68+
}),
69+
),
7970
),
80-
);
71+
),
72+
);
8173
return { capture, enter, guard };
8274
});
8375

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { type Duration, Effect, Fiber } from "effect";
2+
3+
/**
4+
* Fails with `onStall` once `duration` passes, without waiting for the stalled effect's finalizers,
5+
* which may hang too; the stalled fiber is interrupted in the background so its cleanup still runs.
6+
*/
7+
export const failOnStall =
8+
<E2, R2>(duration: Duration.Input, onStall: Effect.Effect<never, E2, R2>) =>
9+
<A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E | E2, R | R2> =>
10+
Effect.flatMap(Effect.forkDetach(effect), (fiber) =>
11+
Fiber.join(fiber).pipe(
12+
Effect.onInterrupt(() => Effect.forkDetach(Fiber.interrupt(fiber))),
13+
Effect.timeoutOrElse({ duration, orElse: () => onStall }),
14+
),
15+
);

0 commit comments

Comments
 (0)