|
1 | 1 | import { it } from "node:test"; |
2 | 2 | import assert from "node:assert"; |
3 | | -import { run, suspend, sleep, until, spawn, resource } from "effection"; |
| 3 | +import { run, suspend, sleep, until, spawn, resource, ensure } from "effection"; |
4 | 4 | import * as fs from "node:fs/promises"; |
5 | 5 | import path from "node:path"; |
6 | 6 | import os from "node:os"; |
@@ -41,12 +41,12 @@ it("restarts services on watched file change and restarts dependents", async () |
41 | 41 |
|
42 | 42 | try { |
43 | 43 | const services = yield* op(); |
44 | | - // subscribe to the immediate serviceUpdates stream and wait for the first update |
45 | | - if (!services.serviceUpdates) |
46 | | - throw new Error("serviceUpdates not available"); |
47 | | - const subscription = yield* services.serviceUpdates; |
| 44 | + // subscribe to the immediate raw serviceChanges stream and wait for the first update |
| 45 | + if (!services.serviceChanges) |
| 46 | + throw new Error("serviceChanges not available"); |
| 47 | + const subscription = yield* services.serviceChanges; |
48 | 48 |
|
49 | | - // wait for the first update (will occur after the test touches the file) |
| 49 | + // wait for the first raw update (will occur after the test touches the file) |
50 | 50 | const first = yield* subscription.next(); |
51 | 51 | updates.push(String((first.value as { service: string }).service)); |
52 | 52 | } catch (e) { |
@@ -210,3 +210,84 @@ it("restarts transitive dependents when watched service changes", async () => { |
210 | 210 | assert(startCounts.b >= 2, "b should have been restarted as dependent"); |
211 | 211 | assert(startCounts.c >= 2, "c should have been restarted as dependent of b"); |
212 | 212 | }); |
| 213 | + |
| 214 | +it("debounces rapid changes per service", async () => { |
| 215 | + const prefix = path.join(os.tmpdir(), "sim-watch-debounce-"); |
| 216 | + const dir = await fs.mkdtemp(prefix); |
| 217 | + const trigger = path.join(dir, "trigger.txt"); |
| 218 | + await fs.writeFile(trigger, "initial"); |
| 219 | + |
| 220 | + const updates: string[] = []; |
| 221 | + let rawCount = 0; |
| 222 | + |
| 223 | + await run(function* () { |
| 224 | + yield* spawn(function* () { |
| 225 | + const op = useServiceGraph( |
| 226 | + { |
| 227 | + a: { |
| 228 | + watch: [dir], |
| 229 | + operation: resource<void>(function* (provide) { |
| 230 | + yield* provide(); |
| 231 | + }), |
| 232 | + }, |
| 233 | + }, |
| 234 | + { watch: true, watchDebounce: 150 } |
| 235 | + ); |
| 236 | + |
| 237 | + try { |
| 238 | + const services = yield* op(); |
| 239 | + if (!services.serviceUpdates || !services.serviceChanges) |
| 240 | + throw new Error("service streams not available"); |
| 241 | + const debSub = yield* services.serviceUpdates; |
| 242 | + const rawSub = yield* services.serviceChanges; |
| 243 | + |
| 244 | + // collect debounced updates |
| 245 | + yield* spawn(function* () { |
| 246 | + while (true) { |
| 247 | + const n = yield* debSub.next(); |
| 248 | + if (n.done) break; |
| 249 | + updates.push((n.value as { service: string }).service); |
| 250 | + } |
| 251 | + }); |
| 252 | + |
| 253 | + // count raw updates (should reflect every write) |
| 254 | + yield* spawn(function* () { |
| 255 | + while (true) { |
| 256 | + const n = yield* rawSub.next(); |
| 257 | + if (n.done) break; |
| 258 | + if ((n.value as { service: string }).service === "a") rawCount++; |
| 259 | + } |
| 260 | + }); |
| 261 | + } catch (e) { |
| 262 | + throw e; |
| 263 | + } |
| 264 | + |
| 265 | + yield* suspend(); |
| 266 | + }); |
| 267 | + |
| 268 | + // ensure watcher attached |
| 269 | + yield* sleep(0); |
| 270 | + |
| 271 | + // write multiple times rapidly |
| 272 | + yield* until(fs.writeFile(trigger, "changed-1")); |
| 273 | + yield* sleep(10); |
| 274 | + yield* until(fs.writeFile(trigger, "changed-2")); |
| 275 | + yield* sleep(10); |
| 276 | + yield* until(fs.writeFile(trigger, "changed-3")); |
| 277 | + |
| 278 | + yield* ensure(() => until(fs.rm(dir, { recursive: true, force: true }))); |
| 279 | + // wait longer than debounce window |
| 280 | + yield* sleep(300); |
| 281 | + }); |
| 282 | + |
| 283 | + // we expect the rapid writes to coalesce: there should be at least one |
| 284 | + // raw and at least one debounced update, and debounced updates should be |
| 285 | + // fewer than the number of writes (3) |
| 286 | + assert(rawCount >= 1, `expected at least 1 raw update, got ${rawCount}`); |
| 287 | + assert(updates.length >= 1, "expected at least one debounced update"); |
| 288 | + const aCount = updates.filter((u) => u === "a").length; |
| 289 | + assert( |
| 290 | + aCount < 3, |
| 291 | + `expected debounced updates to be fewer than writes (3), got ${aCount}` |
| 292 | + ); |
| 293 | +}); |
0 commit comments