Files
usenet-indexer/tests/functional/leases.spec.ts
T
Daan MeijerandClaude Opus 5.5 aa3bd82445
Test and deploy / test (push) Successful in 54s
Test and deploy / edge (push) Successful in 2m4s
Test and deploy / deploy (push) Successful in 3m19s
At most two leases per group: a group's batches wait for each other, more workers only queue
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 15:28:42 +02:00

36 lines
1.7 KiB
TypeScript

import { test } from '@japa/runner'
import db from '@adonisjs/lucid/services/db'
import testUtils from '@adonisjs/core/services/test_utils'
import Leases from '#services/leases'
test.group('Leases', (group) => {
group.each.setup(() => testUtils.db().withGlobalTransaction())
test('at most two ranges of a group are leased at a time; a stale lease is taken over', async ({ assert }) => {
const [a] = await db.table('groups').insert({ name: 'alt.binaries.lease.a', priority: 1 }).returning('id')
const [b] = await db.table('groups').insert({ name: 'alt.binaries.lease.b', priority: 2 }).returning('id')
const range = async (groupId: number, start: number) => {
const [row] = await db.table('ranges').insert({ group_id: groupId, start_id: start, end_id: start + 99, next_id: start, priority: -1 }).returning('id')
return Number(row.id)
}
// Four of group a first in id order, then one of b.
const a1 = await range(a.id, 1)
const a2 = await range(a.id, 101)
await range(a.id, 201)
await range(a.id, 301)
const b1 = await range(b.id, 1)
const node = async (name: string) => Number((await db.table('nodes').insert({ name }).returning('id'))[0].id)
const leased = async (n: number) => (await Leases.lease(await node(`lease-${n}`)))?.range.id ?? null
assert.equal(await leased(1), a1)
assert.equal(await leased(2), a2)
// A third of group a would only wait on the group's lock: group b's.
assert.equal(await leased(3), b1)
assert.isNull(await leased(4))
// a1's worker went quiet: its lease is taken over (a2 alone counts).
await db.from('ranges').where('id', a1).update({ heartbeat_at: new Date(Date.now() - 24 * 3600_000) })
assert.equal(await leased(5), a1)
})
})