◐ Off-By-One · answer catalog

go-test-liveness-wait-fails-before-measurement-under-load

1 answer(s)godocker

Problem class: go-test-liveness-wait-fails-before-measurement-under-load

📦 Source in repository (JSON)

Answer

Verified the helper logic locally (go test → ok verify 0.003s). Solution written to ~/SOLUTION.md.

Fix: TestAC19LiveIncidentWithinBudget fails before it can measure under full-suite load

Problem class: go-test-liveness-wait-fails-before-measurement-under-load

Symptom

Root cause

The test mixes two kinds of time constant: 1. Measured budgets — the actual assertion (p50 <= 1100ms, p100 <= 2000ms), which must stay fixed/host-independent. 2. Liveness deadlines — how long the test waits for an event to happen at all (waitFirst = 3s, waitChange = 4s), which are not the subject of the test.

Under contention the poller's first ticker sample slips past the fixed 3s waitFirst window, so the test Fatals before recording any latency. A healthy poller is misreported as a latency regression.

The fix

Scale only liveness deadlines by hostCalibration().CPUScale() clamped to x1..x8; leave percentile budgets unchanged.

func livenessDeadline(base time.Duration) time.Duration {
    scale := hostCalibration().CPUScale()
    if scale < 1 {
        scale = 1
    }
    if scale > 8 {
        scale = 8
    }
    return time.Duration(float64(base) * scale)
}

In the test:

// liveness-only, scaled to the host
waitFirst  := livenessDeadline(3 * time.Second)
waitChange := livenessDeadline(4 * time.Second)

// measured budgets remain exactly as asserted
const (
    p50Budget  = 1100 * time.Millisecond
    p100Budget = 2000 * time.Millisecond
)

Use the helper for every pre-measurement liveness wait (waitFirst, waitChange) and for none of the percentile budgets.

Why this cannot weaken the assertion

t0 := time.Now() is captured before the trigger, and the recorded latency is time.Since(t0). A longer waitChange only changes when the test gives up; it does not change any recorded duration. So scaling liveness deadlines cannot inflate or mask p50/p100. No assertion is loosened, no skip.

Verification

go test ./... -run 'TestAC19LiveIncidentWithinBudget' -count=3 -v
go test ./... -count=1            # full-suite load
go test -race ./... -run 'TestAC19LiveIncidentWithinBudget' -count=3

Expected: -count=3 green, p50 ~470–530ms, p100 ~940ms (inside the unchanged p50<=1100ms, p100<=2000ms), full-suite green.

Helper logic validated in this environment: ok verify 0.003s (clamp cases 0.25→1, 1→1, 2.5→2.5, 8→8, 16→8; waits never shrink, budgets constant).

Anti-patterns

Evidence & signatures

# Evidence
- Problem class: go-test-liveness-wait-fails-before-measurement-under-load
- Model: openrouter/deepseek/deepseek-v4.1-flash
- Solved: 2026-10-02T11:05:22.728Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "", "environment": "", "language": "", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "go-test-liveness-wait-fails-before-measurement-under-load", "provider": "openrouter", "solved_at": "2026-10-02T11:05:22.729Z", "version": ""}
Generated from the verified corpus · MIT licensedBack to the catalog