Skip to content

Commit 2ba80a6

Browse files
committed
test: verify packagist 30d insert race against real Postgres
Signed-off-by: anilb <epipav@gmail.com>
1 parent 5da5aba commit 2ba80a6

1 file changed

Lines changed: 146 additions & 0 deletions

File tree

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
2+
3+
import { getDbConnection } from '@crowd/database'
4+
5+
import type { QueryExecutor } from '../queryExecutor'
6+
import { pgpQx } from '../queryExecutor'
7+
8+
import { insertLast30dDownloadIfAbsent } from './downloadsLast30d'
9+
10+
// Integration test: hits the running packages-db. Skipped automatically when any of
11+
// the DB env vars are missing so unit-test runs in CI stay green.
12+
const HAVE_DB =
13+
!!process.env.CROWD_PACKAGES_DB_WRITE_HOST &&
14+
!!process.env.CROWD_PACKAGES_DB_PORT &&
15+
!!process.env.CROWD_PACKAGES_DB_USERNAME &&
16+
!!process.env.CROWD_PACKAGES_DB_DATABASE &&
17+
!!process.env.CROWD_PACKAGES_DB_PASSWORD
18+
19+
const FIXTURE_TAG = 'akrites-downloads-last-30d-fixture'
20+
21+
// insertLast30dDownloadIfAbsent is packagist's "first observation wins" guard for the
22+
// monthly rolling window: the presence check, insert, and packages mirror all happen
23+
// in one statement (ON CONFLICT DO NOTHING) so a genuine race between two callers for
24+
// the same purl+month can never let a later write silently overwrite an earlier one.
25+
describe.skipIf(!HAVE_DB)('insertLast30dDownloadIfAbsent — real packages-db', () => {
26+
let qx: QueryExecutor
27+
28+
async function cleanupFixtures(): Promise<void> {
29+
await qx.result(
30+
`DELETE FROM downloads_last_30d WHERE purl IN (
31+
SELECT purl FROM packages WHERE ingestion_source = $(tag))`,
32+
{ tag: FIXTURE_TAG },
33+
)
34+
await qx.result(`DELETE FROM packages WHERE ingestion_source = $(tag)`, { tag: FIXTURE_TAG })
35+
}
36+
37+
async function makePackage(purl: string): Promise<void> {
38+
await qx.result(
39+
`INSERT INTO packages (purl, ecosystem, namespace, name, registry_url, status, ingestion_source)
40+
VALUES ($(purl), 'packagist', 'fixture', $(purl), 'https://example.test', 'active', $(tag))`,
41+
{ purl, tag: FIXTURE_TAG },
42+
)
43+
}
44+
45+
beforeAll(async () => {
46+
const conn = await getDbConnection({
47+
host: process.env.CROWD_PACKAGES_DB_WRITE_HOST ?? '',
48+
port: parseInt(process.env.CROWD_PACKAGES_DB_PORT ?? '0', 10),
49+
database: process.env.CROWD_PACKAGES_DB_DATABASE ?? '',
50+
user: process.env.CROWD_PACKAGES_DB_USERNAME ?? '',
51+
password: process.env.CROWD_PACKAGES_DB_PASSWORD ?? '',
52+
})
53+
qx = pgpQx(conn)
54+
await cleanupFixtures()
55+
}, 30_000)
56+
57+
afterAll(async () => {
58+
if (qx) await cleanupFixtures()
59+
})
60+
61+
it('inserts the window and mirrors it to packages on the first call', async () => {
62+
const purl = `pkg:composer/${FIXTURE_TAG}/first-call`
63+
await makePackage(purl)
64+
65+
const changed = await insertLast30dDownloadIfAbsent(
66+
qx,
67+
purl,
68+
'2026-06-01',
69+
'2026-07-01',
70+
100,
71+
true,
72+
)
73+
74+
expect(changed).toEqual(
75+
expect.arrayContaining(['downloads_last_30d.start_date', 'downloads_last_30d.count']),
76+
)
77+
expect(changed).toContain('packages.downloads_last_30d')
78+
79+
const row = await qx.selectOne(`SELECT count FROM downloads_last_30d WHERE purl = $(purl)`, {
80+
purl,
81+
})
82+
expect(Number(row.count)).toBe(100)
83+
const pkg = await qx.selectOne(`SELECT downloads_last_30d FROM packages WHERE purl = $(purl)`, {
84+
purl,
85+
})
86+
expect(Number(pkg.downloads_last_30d)).toBe(100)
87+
})
88+
89+
it('does not overwrite an existing window — later call with a different count is a no-op', async () => {
90+
const purl = `pkg:composer/${FIXTURE_TAG}/second-call-loses`
91+
await makePackage(purl)
92+
93+
const first = await insertLast30dDownloadIfAbsent(
94+
qx,
95+
purl,
96+
'2026-06-01',
97+
'2026-07-01',
98+
100,
99+
true,
100+
)
101+
expect(first.length).toBeGreaterThan(0)
102+
103+
const second = await insertLast30dDownloadIfAbsent(
104+
qx,
105+
purl,
106+
'2026-06-01',
107+
'2026-07-01',
108+
999,
109+
true,
110+
)
111+
expect(second).toEqual([])
112+
113+
const row = await qx.selectOne(`SELECT count FROM downloads_last_30d WHERE purl = $(purl)`, {
114+
purl,
115+
})
116+
expect(Number(row.count)).toBe(100)
117+
const pkg = await qx.selectOne(`SELECT downloads_last_30d FROM packages WHERE purl = $(purl)`, {
118+
purl,
119+
})
120+
expect(Number(pkg.downloads_last_30d)).toBe(100)
121+
})
122+
123+
it('resolves a genuine race between concurrent callers to exactly one consistent winner', async () => {
124+
const purl = `pkg:composer/${FIXTURE_TAG}/concurrent-race`
125+
await makePackage(purl)
126+
127+
const [a, b] = await Promise.all([
128+
insertLast30dDownloadIfAbsent(qx, purl, '2026-06-01', '2026-07-01', 111, true),
129+
insertLast30dDownloadIfAbsent(qx, purl, '2026-06-01', '2026-07-01', 222, true),
130+
])
131+
132+
// exactly one of the two concurrent callers observes itself as the writer
133+
const winners = [a, b].filter((c) => c.length > 0)
134+
expect(winners).toHaveLength(1)
135+
136+
const row = await qx.selectOne(`SELECT count FROM downloads_last_30d WHERE purl = $(purl)`, {
137+
purl,
138+
})
139+
const pkg = await qx.selectOne(`SELECT downloads_last_30d FROM packages WHERE purl = $(purl)`, {
140+
purl,
141+
})
142+
// whichever value won, the window row and its packages mirror must agree
143+
expect(Number(pkg.downloads_last_30d)).toBe(Number(row.count))
144+
expect([111, 222]).toContain(Number(row.count))
145+
})
146+
})

0 commit comments

Comments
 (0)