package main import ( "math" "testing" "time" ) func sampleAt(total, idle uint64, at time.Time) cpuProcSample { return cpuProcSample{total: total, idle: idle, at: at} } func TestCPUPercentFromSamplesMath(t *testing.T) { base := time.Unix(1700000000, 0) // 25% busy: 200 of 800 elapsed jiffies are non-idle. pct, usable := cpuPercentFromSamples( sampleAt(1000, 800, base), sampleAt(1800, 1400, base.Add(time.Second)), ) if !usable || math.Abs(pct-25) > 0.0001 { t.Fatalf("expected 25%% usable=true, got %.4f usable=%v", pct, usable) } // Fully idle: zero busy jiffies. pct, usable = cpuPercentFromSamples( sampleAt(1000, 900, base), sampleAt(2000, 1900, base.Add(time.Second)), ) if !usable || pct != 0 { t.Fatalf("expected 0%% usable=true, got %.4f usable=%v", pct, usable) } // Fully busy. pct, usable = cpuPercentFromSamples( sampleAt(1000, 900, base), sampleAt(2000, 900, base.Add(time.Second)), ) if !usable || pct != 100 { t.Fatalf("expected 100%% usable=true, got %.4f usable=%v", pct, usable) } // Counters went backwards (reboot / reset): unusable pair. if _, usable := cpuPercentFromSamples( sampleAt(2000, 1900, base), sampleAt(1000, 900, base.Add(time.Second)), ); usable { t.Fatalf("regressed counters must be reported unusable") } // Identical counters (no elapsed jiffies at all): rejected as an // unusable pair so the caller resamples rather than trusting a // zero-width window. if _, usable := cpuPercentFromSamples( sampleAt(1000, 900, base), sampleAt(1000, 900, base.Add(time.Second)), ); usable { t.Fatalf("identical samples must be reported unusable") } } func TestCurrentCPUPercentDeltasAgainstPreviousPoll(t *testing.T) { a := &Agent{} start := time.Now() first := a.currentCPUPercent() if first < 0 || first > 100 { t.Fatalf("first reading out of range: %.4f", first) } // First call has no previous sample, so it must have paid the fallback // double-read instead of returning instantly with a boot-average. if elapsed := time.Since(start); elapsed < cpuFallbackSampleDelay { t.Fatalf("first reading did not take the double-read path (took %v)", elapsed) } if a.lastCPUSample.at.IsZero() { t.Fatalf("first reading must seed the previous sample") } // A quick follow-up poll must delta against that sample (well below the // fallback delay, so it must NOT have double-read again). time.Sleep(cpuDeltaMinWindow + 20*time.Millisecond) secondStart := time.Now() second := a.currentCPUPercent() if elapsed := time.Since(secondStart); elapsed >= cpuFallbackSampleDelay { t.Fatalf("second reading should use the stored sample, not double-read (took %v)", elapsed) } if second < 0 || second > 100 { t.Fatalf("second reading out of range: %.4f", second) } // A stale previous sample (nobody polled past the max window) must be // discarded in favor of a fresh double-read. stale := a.lastCPUSample stale.at = time.Now().Add(-cpuDeltaMaxWindow - time.Minute) a.lastCPUSample = stale staleStart := time.Now() third := a.currentCPUPercent() if elapsed := time.Since(staleStart); elapsed < cpuFallbackSampleDelay { t.Fatalf("stale sample must trigger a fresh double-read (took %v)", elapsed) } if third < 0 || third > 100 { t.Fatalf("third reading out of range: %.4f", third) } if a.lastCPUSample.at.Before(time.Now().Add(-time.Minute)) { t.Fatalf("stale sample not replaced with a fresh one") } } func TestCurrentCPUPercentConcurrentPollsStaySane(t *testing.T) { a := &Agent{} done := make(chan float64, 4) for i := 0; i < 4; i++ { go func() { done <- a.currentCPUPercent() }() } for i := 0; i < 4; i++ { select { case pct := <-done: if pct < 0 || pct > 100 { t.Fatalf("concurrent reading out of range: %.4f", pct) } case <-time.After(10 * time.Second): t.Fatalf("concurrent poll deadlocked") } } if a.lastCPUSample.at.IsZero() { t.Fatalf("concurrent polls must leave a usable sample behind") } }