From 8d199803e5e4abae7ab33479674b8a770d20bdcf Mon Sep 17 00:00:00 2001 From: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Fri, 30 Oct 2026 01:27:49 -0400 Subject: [PATCH] test(server,app): pin shutdown, lock-tail cleanup and Redis lock ttl behaviour from #623 main()'s close() cleared the cron interval and stopped the listener, and memoryLocks only dropped a key's tail when it was still its own, without any test noticing either: Stryker reported both calls, the pending > 0 guard, the `tails.get(key) === settled` comparison, redisLocks' ttlMs default and createClient's url as survivors. Each is now asserted by a test that fails with the statement removed or inverted. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- __tests__/adapters/app/lock.test.ts | 31 ++++++++++++++++++ __tests__/adapters/app/redis.test.ts | 10 +++++- __tests__/server/cronInterval.test.ts | 7 ++++- __tests__/server/stores.test.ts | 45 +++++++++++++++++++++++---- 4 files changed, 85 insertions(+), 8 deletions(-) diff --git a/__tests__/adapters/app/lock.test.ts b/__tests__/adapters/app/lock.test.ts index f027e7bd..0305602f 100644 --- a/__tests__/adapters/app/lock.test.ts +++ b/__tests__/adapters/app/lock.test.ts @@ -64,6 +64,37 @@ describe('memoryLocks', () => { await expect(next).resolves.toBe('ran') }) + it('queues a caller that arrives after the first holder is done behind the one still waiting', async () => { + const locks = memoryLocks() + const order: string[] = [] + const first = gate() + const second = gate() + + const a = locks.withLock('k', async () => { + order.push('a:start') + await first.opened + }) + const b = locks.withLock('k', async () => { + order.push('b:start') + await second.opened + }) + first.open() + await a + await new Promise(resolve => setTimeout(resolve, 10)) + expect(order).toEqual(['a:start', 'b:start']) + + // a's cleanup must leave b's tail in place, or c would run alongside b + const c = locks.withLock('k', async () => { + order.push('c:start') + }) + await new Promise(resolve => setTimeout(resolve, 10)) + expect(order).toEqual(['a:start', 'b:start']) + + second.open() + await Promise.all([b, c]) + expect(order).toEqual(['a:start', 'b:start', 'c:start']) + }) + it('forgets a key once its last holder is done', async () => { const locks = memoryLocks() await locks.withLock('k', async () => {}) diff --git a/__tests__/adapters/app/redis.test.ts b/__tests__/adapters/app/redis.test.ts index 4d6ef6ac..1adb6db4 100644 --- a/__tests__/adapters/app/redis.test.ts +++ b/__tests__/adapters/app/redis.test.ts @@ -54,7 +54,15 @@ describe('redisLocks', () => { }) expect(client.store.has('prow:lock:o/r#1')).toBe(false) - expect(client.calls.at(-1)).toBe('eval prow:lock:o/r#1') + expect(client.calls).toEqual(['set prow:lock:o/r#1 {"NX":true,"PX":500}', 'eval prow:lock:o/r#1']) + }) + + it('takes the lock for 60 s unless told otherwise', async () => { + const client = fakeRedis() + + await redisLocks(client).withLock('k', async () => {}) + + expect(client.calls[0]).toBe('set prow:lock:k {"NX":true,"PX":60000}') }) it('waits for a held lock, then takes it', async () => { diff --git a/__tests__/server/cronInterval.test.ts b/__tests__/server/cronInterval.test.ts index 1326e33f..008fe7d7 100644 --- a/__tests__/server/cronInterval.test.ts +++ b/__tests__/server/cronInterval.test.ts @@ -63,9 +63,14 @@ describe('main() and PROW_CRON_INTERVAL', () => { await polled await vi.waitFor(() => expect(stdoutLines(write).filter(line => line.level === 'error')).toHaveLength(2)) expect(polls).toBe(2) + + // closing clears the interval: another period passes with no further run + await listening.close() + await vi.advanceTimersByTimeAsync(60_000) + expect(polls).toBe(2) } finally { - listening.close() + await listening.close() vi.useRealTimers() write.mockRestore() } diff --git a/__tests__/server/stores.test.ts b/__tests__/server/stores.test.ts index 7f8418b7..efc030db 100644 --- a/__tests__/server/stores.test.ts +++ b/__tests__/server/stores.test.ts @@ -54,6 +54,7 @@ describe('storesFromEnv', () => { expect(lines).toContainEqual({ level: 'info', message: 'redis: connected' }) const { createClient } = await import('redis') + expect(createClient).toHaveBeenLastCalledWith({ url: 'redis://x' }) const client = vi.mocked(createClient).mock.results.at(-1)!.value as { emit: (e: string, v: unknown) => void, close: () => Promise } client.emit('error', new Error('ECONNRESET')) client.emit('error', 'string error') @@ -85,11 +86,14 @@ describe('the server with stores', () => { const stores = redisStores('redis://x', () => {}) const app = buildApp(buildReceiver(env, stores, () => {}), stores) - expect((await app.fetch(new Request('https://prow.test/healthz'))).status).toBe(200) - expect((await app.fetch(new Request('https://prow.test/readyz'))).status).toBe(503) + const healthy = await app.fetch(new Request('https://prow.test/healthz')) + expect([healthy.status, await healthy.text()]).toEqual([200, 'ok']) + const notReady = await app.fetch(new Request('https://prow.test/readyz')) + expect([notReady.status, await notReady.text()]).toEqual([503, 'stores not ready']) connect() await stores.ready - expect((await app.fetch(new Request('https://prow.test/readyz'))).status).toBe(200) + const ready = await app.fetch(new Request('https://prow.test/readyz')) + expect([ready.status, await ready.text()]).toEqual([200, 'ok']) } finally { delete (globalThis as { __redisConnect?: Promise }).__redisConnect @@ -200,12 +204,41 @@ describe('the server with stores', () => { signals.emit('SIGTERM') await new Promise(resolve => setTimeout(resolve, 20)) expect(exit).not.toHaveBeenCalled() - const logged = () => write.mock.calls.map(call => JSON.parse(String(call[0])) as { message: string }) - expect(logged().some(line => /waiting up to 5000ms for 1 in-flight/.test(line.message))).toBe(true) + const logged = () => write.mock.calls.map(call => JSON.parse(String(call[0])) as { level: string, message: string }) + expect(logged().filter(line => /in-flight/.test(line.message))).toEqual([{ level: 'info', message: 'shutting down: waiting up to 5000ms for 1 in-flight deliveries', time: expect.any(String) }]) releaseHandler() await vi.waitFor(() => expect(exit).toHaveBeenCalledWith(0)) - expect(logged().at(-1)).toMatchObject({ message: 'shut down' }) + expect(logged().at(-1)).toEqual({ level: 'info', message: 'shut down', time: expect.any(String) }) + } + finally { + await listening.close(0) + write.mockRestore() + } + }) + + it('main() with nothing in flight stops listening and closes without waiting on a grace period', async () => { + const write = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) + const signals = new (await import('node:events')).EventEmitter() as unknown as NodeJS.Process + const exit = vi.fn() + ;(signals as unknown as { exit: typeof exit }).exit = exit + const port = 23000 + Math.floor(Math.random() * 1000) + const { passthrough } = await import('msw') + server.use(http.all(`http://127.0.0.1:${port}/*`, () => passthrough())) + const listening = main({ ...env, PORT: String(port), REDIS_URL: 'redis://x', PROW_SHUTDOWN_GRACE_MS: '60000' }, signals) + try { + await listening.ready + expect((await fetch(`http://127.0.0.1:${port}/healthz`)).status).toBe(200) + const { createClient } = await import('redis') + const client = vi.mocked(createClient).mock.results.at(-1)!.value as { close: ReturnType } + + signals.emit('SIGTERM') + await vi.waitFor(() => expect(exit).toHaveBeenCalledWith(0)) + + expect(client.close).toHaveBeenCalledOnce() + const logged = write.mock.calls.map(call => JSON.parse(String(call[0])) as { message: string }) + expect(logged.map(line => line.message)).toEqual(['redis: connected', `listening on :${port}`, 'shut down']) + await expect(fetch(`http://127.0.0.1:${port}/healthz`)).rejects.toThrow() } finally { await listening.close(0)