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
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ concurrency:
jobs:
build-test:
runs-on: ubuntu-latest
# the App server's Redis-backed stores (__tests__/adapters/app/redis.test.ts) run against a real server
services:
redis:
image: redis:7-alpine
ports: ['6379:6379']
options: --health-cmd "redis-cli ping" --health-interval 5s --health-timeout 3s --health-retries 10
env:
REDIS_TEST_URL: redis://127.0.0.1:6379
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
Expand Down
90 changes: 90 additions & 0 deletions __tests__/adapters/app/lock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { describe, expect, it } from 'vitest'

import { memoryLocks, pullRequestKey } from '../../../src/adapters/app/lock'

function gate() {
let open!: () => void
const opened = new Promise<void>((resolve) => {
open = resolve
})
return { open, opened }
}

describe('memoryLocks', () => {
it('runs two calls on one key one after the other, in order', async () => {
const locks = memoryLocks()
const order: string[] = []
const first = gate()

const a = locks.withLock('k', async () => {
order.push('a:start')
await first.opened
order.push('a:end')
return 'a'
})
const b = locks.withLock('k', async () => {
order.push('b:start')
return 'b'
})
await new Promise(resolve => setTimeout(resolve, 10))
expect(order).toEqual(['a:start'])

first.open()
expect(await Promise.all([a, b])).toEqual(['a', 'b'])
expect(order).toEqual(['a:start', 'a:end', 'b:start'])
})

it('runs different keys at the same time', async () => {
const locks = memoryLocks()
const order: string[] = []
const first = gate()

const a = locks.withLock('k1', async () => {
order.push('a:start')
await first.opened
})
const b = locks.withLock('k2', async () => {
order.push('b:start')
})
await b
expect(order).toEqual(['a:start', 'b:start'])
first.open()
await a
})

it('releases the lock when the holder throws, and the next holder still runs', async () => {
const locks = memoryLocks()

const failed = locks.withLock('k', async () => {
throw new Error('boom')
})
const next = locks.withLock('k', async () => 'ran')

await expect(failed).rejects.toThrow('boom')
await expect(next).resolves.toBe('ran')
})

it('forgets a key once its last holder is done', async () => {
const locks = memoryLocks()
await locks.withLock('k', async () => {})
// a fresh chain: this would hang if the previous one were still awaited incorrectly
await expect(locks.withLock('k', async () => 'again')).resolves.toBe('again')
})
})

describe('pullRequestKey', () => {
const repository = { full_name: 'o/r', name: 'r', owner: { login: 'o' } }

it('keys a pull request event by repository and number', () => {
expect(pullRequestKey({ id: 'd', eventName: 'pull_request', payload: { repository, pull_request: { number: 7 } } })).toBe('o/r#7')
})

it('keys an issue event the same way', () => {
expect(pullRequestKey({ id: 'd', eventName: 'issue_comment', payload: { repository, issue: { number: 3 } } })).toBe('o/r#3')
})

it('is undefined for an event about no issue, or without a repository', () => {
expect(pullRequestKey({ id: 'd', eventName: 'push', payload: { repository } })).toBeUndefined()
expect(pullRequestKey({ id: 'd', eventName: 'issues', payload: { issue: { number: 1 } } })).toBeUndefined()
})
})
139 changes: 139 additions & 0 deletions __tests__/adapters/app/redis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import type { RedisLike } from '../../../src/adapters/app/redis'
import process from 'node:process'
import { createClient } from 'redis'
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'

import { redisDeliveryStore, redisLocks } from '../../../src/adapters/app/redis'
import * as sleepModule from '../../../src/utils/sleep'

/** an in-memory SET NX / EVAL that is good enough for the two scripts the stores use */
function fakeRedis(): RedisLike & { store: Map<string, string>, calls: string[] } {
const store = new Map<string, string>()
const calls: string[] = []
return {
store,
calls,
async set(key, value, options) {
calls.push(`set ${key} ${JSON.stringify(options)}`)
if (options.NX && store.has(key)) {
return null
}
store.set(key, value)
return 'OK'
},
async eval(_script, { keys, arguments: args }) {
calls.push(`eval ${keys[0]}`)
if (store.get(keys[0]!) === args[0]) {
store.delete(keys[0]!)
return 1
}
return 0
},
}
}

describe('redisDeliveryStore', () => {
it('reports an id the first time as new and every later time as seen, with the ttl', async () => {
const client = fakeRedis()
const store = redisDeliveryStore(client, 123)

expect(await store.seen('a')).toBe(false)
expect(await store.seen('a')).toBe(true)
expect(await store.seen('b')).toBe(false)
expect(client.calls[0]).toBe('set prow:delivery:a {"NX":true,"EX":123}')
})
})

describe('redisLocks', () => {
it('holds the key while fn runs and releases it by token afterwards', async () => {
const client = fakeRedis()
const locks = redisLocks(client, { ttlMs: 500 })

await locks.withLock('o/r#1', async () => {
expect(client.store.get('prow:lock:o/r#1')).toMatch(/^\d+-/)
})

expect(client.store.has('prow:lock:o/r#1')).toBe(false)
expect(client.calls.at(-1)).toBe('eval prow:lock:o/r#1')
})

it('waits for a held lock, then takes it', async () => {
const client = fakeRedis()
const sleep = vi.spyOn(sleepModule, 'sleep').mockImplementation(async () => {
client.store.delete('prow:lock:k')
})
client.store.set('prow:lock:k', 'someone-else')

await expect(redisLocks(client, { retryMs: 5 }).withLock('k', async () => 'got it')).resolves.toBe('got it')
expect(sleep).toHaveBeenCalledWith(5)
})

it('does not release a lock another holder took over after the ttl', async () => {
const client = fakeRedis()
const locks = redisLocks(client)

await locks.withLock('k', async () => {
client.store.set('prow:lock:k', 'a-new-holder')
})

expect(client.store.get('prow:lock:k')).toBe('a-new-holder')
})

it('gives up after waitMs when the lock never frees, and still releases on a throwing fn', async () => {
const client = fakeRedis()
vi.spyOn(sleepModule, 'sleep').mockResolvedValue(undefined)
client.store.set('prow:lock:k', 'stuck')

await expect(redisLocks(client, { waitMs: 1, retryMs: 1 }).withLock('k', async () => {})).rejects.toThrow(/could not lock k within 1ms/)

client.store.clear()
await expect(redisLocks(client).withLock('k', async () => {
throw new Error('boom')
})).rejects.toThrow('boom')
expect(client.store.has('prow:lock:k')).toBe(false)
})
})

// a real server: build-test provides one as a service container and sets REDIS_TEST_URL
describe.skipIf(!process.env.REDIS_TEST_URL)('against a redis server', () => {
const a = createClient({ url: process.env.REDIS_TEST_URL })
const b = createClient({ url: process.env.REDIS_TEST_URL })
const prefix = `t${Date.now()}`

beforeAll(async () => {
await Promise.all([a.connect(), b.connect()])
})
afterAll(async () => {
await Promise.all([a.close(), b.close()])
})

it('dedupes a delivery across two clients', async () => {
expect(await redisDeliveryStore(a).seen(`${prefix}-d`)).toBe(false)
expect(await redisDeliveryStore(b).seen(`${prefix}-d`)).toBe(true)
})

it('excludes two clients from one pull request at a time', async () => {
const order: string[] = []
const la = redisLocks(a, { retryMs: 5 })
const lb = redisLocks(b, { retryMs: 5 })
await Promise.all([
la.withLock(`${prefix}-pr`, async () => {
order.push('a:start')
await new Promise(resolve => setTimeout(resolve, 60))
order.push('a:end')
}),
(async () => {
await new Promise(resolve => setTimeout(resolve, 10))
await lb.withLock(`${prefix}-pr`, async () => {
order.push('b:start')
})
})(),
])
expect(order).toEqual(['a:start', 'a:end', 'b:start'])
})

it('frees a lock whose holder vanished once the ttl passes', async () => {
await a.set(`prow:lock:${prefix}-dead`, 'gone', { NX: true, PX: 80 })
await expect(redisLocks(b, { retryMs: 10, waitMs: 1000 }).withLock(`${prefix}-dead`, async () => 'recovered')).resolves.toBe('recovered')
})
})
Loading
Loading