Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions __tests__/adapters/app/lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {})
Expand Down
10 changes: 9 additions & 1 deletion __tests__/adapters/app/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
7 changes: 6 additions & 1 deletion __tests__/server/cronInterval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
45 changes: 39 additions & 6 deletions __tests__/server/stores.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> }
client.emit('error', new Error('ECONNRESET'))
client.emit('error', 'string error')
Expand Down Expand Up @@ -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<void> }).__redisConnect
Expand Down Expand Up @@ -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<typeof vi.fn> }

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)
Expand Down
Loading