Test drain directly

drain() is public on SerialBatches but was only exercised through shutdown.
Cover it on its own: it waits for in-flight work and leaves the queue open, so
a shutdown can drain before the watchers flush and still have that work
accepted, and it returns immediately when nothing is running.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lcj4iQ3fBxMwAu2rf4zLbC
This commit is contained in:
Michael Glass 2026-08-31 16:38:19 +02:00
parent 5302f308fc
commit 3148fad1a1
No known key found for this signature in database
GPG key ID: 25FC9DFE17C802D3

View file

@ -153,3 +153,36 @@ it('reports a rejected initial barrier once and settles', async () => {
expect(calls).toEqual([])
expect(errors).toEqual([new Error('initial build failed')])
})
it('drains in-flight work but stays open for more', async () => {
let release!: () => void
let canFinish = new Promise<void>((resolve) => (release = resolve))
let calls: string[][] = []
let queue = serializeBatches<string>(async (batch) => {
calls.push(batch)
if (calls.length === 1) await canFinish
})
void queue.push(['first'])
await Promise.resolve()
let drained = queue.drain()
release()
await drained
expect(calls).toEqual([['first']])
// Draining must not close the queue — a shutdown drains before the watchers
// have flushed what they collected, and that work still has to be accepted.
await queue.push(['after-drain'])
expect(calls).toEqual([['first'], ['after-drain']])
})
it('drain returns immediately when nothing is in flight', async () => {
let calls: string[][] = []
let queue = serializeBatches<string>(async (batch) => {
calls.push(batch)
})
await queue.drain()
expect(calls).toEqual([])
})