mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-26 13:13:05 +00:00
fix(test): DEBT-FLAKES-C100 — silence review sample 10→30 + ensureLeasedConsent ON CONFLICT idempotent (Closes #553)
This commit is contained in:
parent
2920aa9d63
commit
ba7badd5cf
3 changed files with 35 additions and 13 deletions
16
docs/changelog/2026-08-24-debt-flakes-c100.md
Normal file
16
docs/changelog/2026-08-24-debt-flakes-c100.md
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
# DEBT-FLAKES-C100 — fix flakes pré-existentes do C100 (Closes #553)
|
||||||
|
|
||||||
|
Duas flaky tests do pipeline de sugestões de campanha (E11) e do maquinário de
|
||||||
|
lease de consentimento, ambas pré-existentes ao PR C100:
|
||||||
|
|
||||||
|
1. **campaignSuggestions silence review** — amostra de 10 municípios insuficiente
|
||||||
|
sob carga paralela (P(fail) nominal 0,17%). Amostra ampliada para 30
|
||||||
|
(P(fail) ≈ 7e-9) com mensagem de diagnóstico.
|
||||||
|
|
||||||
|
2. **testDatabaseLease concurrent ensures** — TOCTOU find→create em
|
||||||
|
`ensureLeasedConsent` permitia 2 creates sob advisory lock extremo.
|
||||||
|
Tornado idempotente via `INSERT … ON CONFLICT (key) DO NOTHING` +
|
||||||
|
find, eliminando a corrida independente do lock.
|
||||||
|
|
||||||
|
Mudanças: `tests/helpers/testDatabaseLease.ts`,
|
||||||
|
`tests/int/campaignSuggestions.int.spec.ts`.
|
||||||
|
|
@ -457,6 +457,16 @@ export const ensureLeasedConsent = async (
|
||||||
// hitting the UNIQUE constraint. Autocommit is safe: we hold the
|
// hitting the UNIQUE constraint. Autocommit is safe: we hold the
|
||||||
// exclusive lease, so no live renamer can be mid-flight.
|
// exclusive lease, so no live renamer can be mid-flight.
|
||||||
await purgeOrphanedConsentRenames((query) => payload.db.drizzle.execute(query))
|
await purgeOrphanedConsentRenames((query) => payload.db.drizzle.execute(query))
|
||||||
|
// Idempotent upsert: INSERT … ON CONFLICT (key) DO NOTHING eliminates the
|
||||||
|
// TOCTOU race between the find and the create — even if two ensures slip
|
||||||
|
// past the advisory lock under extreme load, the UNIQUE consent_key_idx
|
||||||
|
// guarantees exactly one row. The subsequent find always returns the
|
||||||
|
// committed row (created by this INSERT or a prior one).
|
||||||
|
await payload.db.drizzle.execute(sql`
|
||||||
|
INSERT INTO "consent" ("key", "text", "updated_at", "created_at")
|
||||||
|
VALUES (${consentKey}, ${fixtureConsentText}, now(), now())
|
||||||
|
ON CONFLICT ("key") DO NOTHING
|
||||||
|
`)
|
||||||
const existing = await payload.find({
|
const existing = await payload.find({
|
||||||
collection: 'consent',
|
collection: 'consent',
|
||||||
where: { key: { equals: consentKey } },
|
where: { key: { equals: consentKey } },
|
||||||
|
|
@ -464,16 +474,10 @@ export const ensureLeasedConsent = async (
|
||||||
sort: 'id',
|
sort: 'id',
|
||||||
depth: 0,
|
depth: 0,
|
||||||
})
|
})
|
||||||
if (existing.docs[0]) return existing.docs[0]
|
if (!existing.docs[0]) {
|
||||||
|
throw new Error(`ensureLeasedConsent: consent key "${consentKey}" not found after upsert.`)
|
||||||
return payload.create({
|
}
|
||||||
collection: 'consent',
|
return existing.docs[0]
|
||||||
data: {
|
|
||||||
key: consentKey,
|
|
||||||
text: fixtureConsentText,
|
|
||||||
},
|
|
||||||
depth: 0,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
export const withLeasedConsent = async <Result>(
|
export const withLeasedConsent = async <Result>(
|
||||||
|
|
|
||||||
|
|
@ -276,9 +276,11 @@ describe('loadMunicipalitySuggestions (E11)', () => {
|
||||||
|
|
||||||
// A quiet município cannot be staged — quietness means NO artifact-anchored
|
// A quiet município cannot be staged — quietness means NO artifact-anchored
|
||||||
// pattern fires — so claim through the allocator until one qualifies
|
// pattern fires — so claim through the allocator until one qualifies
|
||||||
// (measured: 205 of 435 with zeroed rows).
|
// (measured: 205 of 435 with zeroed rows → P(fail in 30) ≈ 0.53^30 ≈ 7e-9).
|
||||||
|
// 30 attempts keeps the flake probability negligible even under parallel load
|
||||||
|
// where the effective quiet rate may dip below the nominal 47%.
|
||||||
let municipality: Municipality | null = null
|
let municipality: Municipality | null = null
|
||||||
for (let attempt = 0; attempt < 10; attempt += 1) {
|
for (let attempt = 0; attempt < 30; attempt += 1) {
|
||||||
const candidate = await fixtures.getMunicipality()
|
const candidate = await fixtures.getMunicipality()
|
||||||
await normalizeMunicipality(candidate, { priority: 'alta' })
|
await normalizeMunicipality(candidate, { priority: 'alta' })
|
||||||
fixtures.touchMunicipality(candidate.id)
|
fixtures.touchMunicipality(candidate.id)
|
||||||
|
|
@ -289,7 +291,7 @@ describe('loadMunicipalitySuggestions (E11)', () => {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!municipality) throw new Error('Nenhum município silencioso em 10 alocações.')
|
if (!municipality) throw new Error('Nenhum município silencioso em 30 alocações.')
|
||||||
|
|
||||||
const author = await fixtures.createCampaignUser('coordinator')
|
const author = await fixtures.createCampaignUser('coordinator')
|
||||||
await fixtures.createMunicipalityUpdate({
|
await fixtures.createMunicipalityUpdate({
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue