Skip to content

Commit f0c6c66

Browse files
committed
test(client): a service ensure did not start survives slow probes, and a hanging one is reported and left running
1 parent d98f156 commit f0c6c66

3 files changed

Lines changed: 68 additions & 23 deletions

File tree

‎packages/client/test/fixture/service.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ const server = Bun.serve({
7575
return new Response(null, { status: 404 })
7676
requests += 1
7777
if (mode === "starting") await writeFile(registration + ".status-request", "")
78+
// A busy or booting service: its first `delay` probes answer after the client gave up on them.
79+
if (mode === "slow" && requests <= Number(delay)) {
80+
await appendFile(registration + ".requests", process.pid + "\n")
81+
await Bun.sleep(300)
82+
}
7883
if (mode === "hanging" || controlled === "hang") {
7984
await appendFile(registration + ".requests", process.pid + "\n")
8085
return new Promise<Response>(() => {})

‎packages/client/test/promise-service.test.ts‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,26 +203,46 @@ test("reports a bounded contender stderr tail with native promises", async () =>
203203
expect(error.message.length).toBeLessThan(9_000)
204204
}, 10_000)
205205

206-
test("evicts an unresponsive registered service before starting its replacement", async () => {
206+
test("a registered service it did not start that answers probes slowly survives ensure", async () => {
207+
await using fixture = await serviceFixture()
208+
const registration = fixture.registration
209+
const existing = fixture.spawn("slow", "5")
210+
await fixture.waitForFile()
211+
const original = await Bun.file(registration).json()
212+
213+
const endpoint = await ensure({ file: registration, version: "test", command: fixture.command("record-start") })
214+
215+
expect((await Bun.file(registration + ".requests").text()).trim().split("\n")).toHaveLength(5)
216+
expect(endpoint.url).toBe(original.url)
217+
expect(existing.exitCode).toBe(null)
218+
expect(await fixture.signal()).toBeUndefined()
219+
expect(await Bun.file(registration + ".started").exists()).toBe(false)
220+
})
221+
222+
test("a hanging registered service it did not start is waited for, then reported and left running", async () => {
207223
await using fixture = await serviceFixture()
208224
const registration = fixture.registration
209225
const existing = fixture.spawn("hanging")
210226
await fixture.waitForFile()
211227
const original = await Bun.file(registration).json()
228+
const waits: string[] = []
212229

213-
const endpoint = await ensure({
230+
const error = await ensure({
214231
file: registration,
215232
version: "test",
216-
command: fixture.command("delayed", "10"),
217-
})
218-
const replacement = await Bun.file(registration).json()
219-
fixture.track(replacement.pid)
233+
command: fixture.command("record-start"),
234+
onWait: (url) => waits.push(url),
235+
}).catch((error: unknown) => error)
220236

221-
expect((await Bun.file(registration + ".requests").text()).trim().split("\n")).toHaveLength(3)
222-
expect(await existing.exited).toBe(stoppedExitCode)
223-
expect(replacement.pid).not.toBe(original.pid)
224-
expect(endpoint.url).toBe(replacement.url)
225-
})
237+
expect(error).toBeInstanceOf(Error)
238+
expect(String(error)).toContain(`Background service at ${original.url} (pid ${original.pid}) did not answer`)
239+
expect(String(error)).toContain("left running")
240+
expect(waits).toEqual([original.url])
241+
expect(existing.exitCode).toBe(null)
242+
expect(await fixture.signal()).toBeUndefined()
243+
expect(await Bun.file(registration).json()).toEqual(original)
244+
expect(await Bun.file(registration + ".started").exists()).toBe(false)
245+
}, 10_000)
226246

227247
test("recovers when the starting service it waits for is stopped", async () => {
228248
await using fixture = await serviceFixture()

‎packages/client/test/service.test.ts‎

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,29 +175,49 @@ test("reports a failed registered service without spawning", async () => {
175175
expect(process.exitCode).toBe(null)
176176
})
177177

178-
test("evicts an unresponsive registered service before starting its replacement", async () => {
178+
test("a registered service it did not start that answers probes slowly survives ensure", async () => {
179+
await using fixture = await serviceFixture()
180+
const registration = fixture.registration
181+
const existing = fixture.spawn("slow", "5")
182+
await fixture.waitForFile()
183+
const original = await Bun.file(registration).json()
184+
185+
const endpoint = await run(ensure({ file: registration, version: "test", command: fixture.command("record-start") }))
186+
187+
expect((await Bun.file(registration + ".requests").text()).trim().split("\n")).toHaveLength(5)
188+
expect(endpoint.url).toBe(original.url)
189+
expect(existing.exitCode).toBe(null)
190+
expect(await fixture.signal()).toBeUndefined()
191+
expect(await Bun.file(registration + ".started").exists()).toBe(false)
192+
expect(await status(endpoint.url)).toMatchObject({ version: "test", pid: original.pid })
193+
})
194+
195+
test("a hanging registered service it did not start is waited for, then reported and left running", async () => {
179196
await using fixture = await serviceFixture()
180197
const registration = fixture.registration
181198
const existing = fixture.spawn("hanging")
182199
await fixture.waitForFile()
183200
const original = await Bun.file(registration).json()
201+
const waits: string[] = []
184202

185-
const endpoint = await run(
203+
const error = await run(
186204
ensure({
187205
file: registration,
188206
version: "test",
189-
command: fixture.command("delayed", "10"),
207+
command: fixture.command("record-start"),
208+
onWait: (url) => waits.push(url),
190209
}),
191-
)
192-
const replacement = await Bun.file(registration).json()
193-
fixture.track(replacement.pid)
210+
).catch((error: unknown) => error)
194211

195-
expect((await Bun.file(registration + ".requests").text()).trim().split("\n")).toHaveLength(3)
196-
expect(await existing.exited).toBe(stoppedExitCode)
197-
expect(replacement.pid).not.toBe(original.pid)
198-
expect(endpoint.url).toBe(replacement.url)
199-
expect(await status(endpoint.url)).toMatchObject({ version: "test", pid: replacement.pid })
200-
})
212+
expect(error).toBeInstanceOf(Error)
213+
expect(String(error)).toContain(`Background service at ${original.url} (pid ${original.pid}) did not answer`)
214+
expect(String(error)).toContain("left running")
215+
expect(waits).toEqual([original.url])
216+
expect(existing.exitCode).toBe(null)
217+
expect(await fixture.signal()).toBeUndefined()
218+
expect(await Bun.file(registration).json()).toEqual(original)
219+
expect(await Bun.file(registration + ".started").exists()).toBe(false)
220+
}, 10_000)
201221

202222
test("signals an unresponsive registered service process", async () => {
203223
await using fixture = await serviceFixture()

0 commit comments

Comments
 (0)