|
1 | 1 | import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; |
2 | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
3 | 3 | import { assert, it } from "@effect/vitest"; |
| 4 | +import * as Clock from "effect/Clock"; |
4 | 5 | import * as ConfigProvider from "effect/ConfigProvider"; |
5 | 6 | import * as Effect from "effect/Effect"; |
6 | 7 | import * as Layer from "effect/Layer"; |
@@ -80,6 +81,24 @@ const respondWithStatus = |
80 | 81 | (request) => |
81 | 82 | Effect.succeed(HttpClientResponse.fromWeb(request, new Response(null, { status }))); |
82 | 83 |
|
| 84 | +/** |
| 85 | + * Answers 503 for `minutes` after the first send, then 204. `answers` holds each |
| 86 | + * answer with its time in ms after the first send (the test clock runs on across tests). |
| 87 | + */ |
| 88 | +const failingFor = (minutes: number) => { |
| 89 | + const answers: Array<{ readonly at: number; readonly status: number }> = []; |
| 90 | + let firstSend: number | undefined; |
| 91 | + const respond: Respond = (request) => |
| 92 | + Effect.flatMap(Clock.currentTimeMillis, (now) => { |
| 93 | + firstSend ??= now; |
| 94 | + const at = now - firstSend; |
| 95 | + const status = at < minutes * 60_000 ? 503 : 204; |
| 96 | + answers.push({ at, status }); |
| 97 | + return respondWithStatus(status)(request); |
| 98 | + }); |
| 99 | + return { respond, answers }; |
| 100 | +}; |
| 101 | + |
83 | 102 | const recordTwentyEvents = Effect.gen(function* () { |
84 | 103 | const analytics = yield* AnalyticsService.AnalyticsService; |
85 | 104 | for (let index = 0; index < 20; index += 1) { |
@@ -88,11 +107,12 @@ const recordTwentyEvents = Effect.gen(function* () { |
88 | 107 | }); |
89 | 108 |
|
90 | 109 | /** |
91 | | - * Runs `record` (20 events by default), then lets ten minutes of background |
| 110 | + * Runs `record` (20 events by default), then lets `minutes` of background |
92 | 111 | * flushes run against a client that answers every send with `respond`. |
93 | 112 | * Returns the sent batches. |
94 | 113 | */ |
95 | | -const sendForTenMinutes = ( |
| 114 | +const sendFor = ( |
| 115 | + minutes: number, |
96 | 116 | respond: Respond, |
97 | 117 | record: Effect.Effect< |
98 | 118 | void, |
@@ -137,7 +157,7 @@ const sendForTenMinutes = ( |
137 | 157 |
|
138 | 158 | yield* Effect.gen(function* () { |
139 | 159 | yield* record; |
140 | | - for (let second = 0; second < 600; second += 1) { |
| 160 | + for (let second = 0; second < minutes * 60; second += 1) { |
141 | 161 | yield* TestClock.adjust("1 second"); |
142 | 162 | } |
143 | 163 | }).pipe(Effect.provide(runtimeLayer)); |
@@ -147,7 +167,8 @@ const sendForTenMinutes = ( |
147 | 167 | it.layer(NodeServices.layer)("AnalyticsService test", (it) => { |
148 | 168 | it.effect("client owner reports cannot use up the server's owner cap", () => |
149 | 169 | Effect.gen(function* () { |
150 | | - const batches = yield* sendForTenMinutes( |
| 170 | + const batches = yield* sendFor( |
| 171 | + 10, |
151 | 172 | respondWithStatus(204), |
152 | 173 | Effect.gen(function* () { |
153 | 174 | const analytics = yield* AnalyticsService.AnalyticsService; |
@@ -170,6 +191,36 @@ it.layer(NodeServices.layer)("AnalyticsService test", (it) => { |
170 | 191 | }), |
171 | 192 | ); |
172 | 193 |
|
| 194 | + it.effect("keeps a failed batch through a 20-minute outage and sends it once after", () => |
| 195 | + Effect.gen(function* () { |
| 196 | + const ingest = failingFor(20); |
| 197 | + const batches = yield* sendFor(40, ingest.respond); |
| 198 | + const statuses = ingest.answers.map(({ status }) => status); |
| 199 | + assert.equal(statuses.at(-1), 204); |
| 200 | + assert.equal(statuses.filter((status) => status === 204).length, 1); |
| 201 | + const sentUuids = new Set(batches.map((batch) => batch.map(({ uuid }) => uuid).join())); |
| 202 | + assert.equal(sentUuids.size, 1); |
| 203 | + assert.equal(batches[0]?.length, 20); |
| 204 | + }), |
| 205 | + ); |
| 206 | + |
| 207 | + it.effect("drops a batch that keeps failing for 30 minutes", () => |
| 208 | + Effect.gen(function* () { |
| 209 | + const ingest = failingFor(Infinity); |
| 210 | + yield* sendFor(60, ingest.respond); |
| 211 | + const lastSend = ingest.answers.at(-1)?.at ?? 0; |
| 212 | + assert.isAtLeast(lastSend, 30 * 60_000); |
| 213 | + assert.isBelow(lastSend, 36 * 60_000); |
| 214 | + }), |
| 215 | + ); |
| 216 | + |
| 217 | + it.effect("drops a batch the endpoint rejects without sending it again", () => |
| 218 | + Effect.gen(function* () { |
| 219 | + const batches = yield* sendFor(10, respondWithStatus(400)); |
| 220 | + assert.equal(batches.length, 1); |
| 221 | + }), |
| 222 | + ); |
| 223 | + |
173 | 224 | it.effect("flush drains all buffered events across multiple batches", () => |
174 | 225 | Effect.gen(function* () { |
175 | 226 | const capturedRequests: Array<RecordedBatchRequest> = []; |
|