Fix TestMetricsSnapshot/uptime flakiness on Windows

Simplify uptime calculation to always enforce minimum of 1 second,
even if time.Since returns exactly 0 (can happen on Windows with
coarse timing). This makes the test deterministic.
This commit is contained in:
Steve Yegge
2025-11-02 09:56:42 -08:00
parent 9f9b8bbdc2
commit 1036b0b700
3 changed files with 7 additions and 5 deletions

View File

@@ -105,10 +105,10 @@ func (m *Metrics) Snapshot(activeConns int) MetricsSnapshot {
// Compute statistics outside the lock
uptime := time.Since(m.startTime)
// Round up uptime and enforce minimum of 1 second if any time has passed
// Round up uptime and enforce minimum of 1 second
// This prevents flaky tests on fast systems (especially Windows VMs)
uptimeSeconds := math.Ceil(uptime.Seconds())
if uptime > 0 && uptimeSeconds == 0 {
if uptimeSeconds == 0 {
uptimeSeconds = 1
}

View File

@@ -135,8 +135,10 @@ func TestMetricsSnapshot(t *testing.T) {
})
t.Run("uptime", func(t *testing.T) {
if snapshot.UptimeSeconds <= 0 {
t.Error("Expected positive uptime")
// The uptime calculation uses math.Ceil and ensures minimum 1 second
// if any time has passed. This should always be >= 1.
if snapshot.UptimeSeconds < 1 {
t.Errorf("Expected uptime >= 1, got %f", snapshot.UptimeSeconds)
}
})