staros/os_unix_test.go

37 lines
1.1 KiB
Go
Raw Permalink Normal View History

//go:build linux
// +build linux
package staros
import (
"testing"
"time"
)
func TestCPUUsageOverDurationGuardsZeroOrNegativeWindow(t *testing.T) {
if got := cpuUsageOverDuration(10, 0); got != 0 {
t.Fatalf("expected zero-window cpu usage to clamp to 0, got %v", got)
}
if got := cpuUsageOverDuration(10, -time.Millisecond); got != 0 {
t.Fatalf("expected negative-window cpu usage to clamp to 0, got %v", got)
}
if got := cpuUsageOverDuration(-1, time.Second); got != 0 {
t.Fatalf("expected negative delta cpu usage to clamp to 0, got %v", got)
}
if got := cpuUsageOverDuration(0.5, time.Second); got != 50 {
t.Fatalf("expected normal cpu usage calculation, got %v", got)
}
}
func TestCPUUsagePercentGuardsInvalidSamples(t *testing.T) {
if got := cpuUsagePercent(1, 0); got != 0 {
t.Fatalf("expected zero total ticks to clamp to 0, got %v", got)
}
if got := cpuUsagePercent(-1, 4); got != 0 {
t.Fatalf("expected negative busy ticks to clamp to 0, got %v", got)
}
if got := cpuUsagePercent(1, 4); got != 25 {
t.Fatalf("expected normal cpu percent calculation, got %v", got)
}
}