|
1 | 1 | import { NodeHttpClient, NodeServices } from "@effect/platform-node"; |
2 | 2 | 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"; |
4 | 4 | import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; |
5 | 5 | import { makeStandaloneService } from "../../tests/standalone-service.ts"; |
6 | 6 | import { makeServiceRecipe } from "./Catalog.ts"; |
7 | 7 | import { engineTarget, testEngine } from "../../tests/engine-target.ts"; |
8 | 8 | import { httpHost } from "../../tests/helpers/endpoint.ts"; |
9 | 9 | import { selection } from "../../tests/pins.ts"; |
| 10 | +import { failOnStall } from "../../tests/stall-guard.ts"; |
10 | 11 |
|
11 | 12 | const options = (root: string) => ({ |
12 | 13 | stackId: "catalog-mail", |
@@ -59,31 +60,63 @@ const sendTestEmail = ( |
59 | 60 | yield* HttpClientResponse.filterStatusOk(response); |
60 | 61 | }); |
61 | 62 |
|
| 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 | + |
62 | 79 | describe("service catalog", () => { |
63 | 80 | 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))), |
87 | 120 | ); |
88 | 121 |
|
89 | 122 | it.live( |
|
0 commit comments