package obs
import (
"testing"
"time"
)
// The sampler keeps a day and no more, and a fresh reading is real.
func TestMemoryRingKeepsADay(t *testing.T) {
var r memRing
now := time.Now()
for i := 30; i >= 0; i-- {
r.add(MemSample{At: now.Add(-time.Duration(i) * time.Hour), Heap: uint64(i)})
}
if len(r.samples) != 25 || r.samples[0].At.Before(now.Add(-MemoryKept)) {
t.Fatalf("FAIL: kept %d samples from %s", len(r.samples), r.samples[0].At)
}
if m := MemoryNow(); m.Heap == 0 {
t.Fatal("FAIL: a fresh reading has no heap")
}
t.Log("ok: a day of samples, and a live reading")
}
// The collection rate is per minute over the window asked for.
func TestGCRate(t *testing.T) {
now := time.Now()
s := []MemSample{{At: now.Add(-10 * time.Minute), GCs: 100}, {At: now, GCs: 400}}
if r := gcRate(s, 10*time.Minute); r < 29.9 || r > 30.1 {
t.Fatalf("FAIL: rate %.2f, want 30 a minute", r)
}
}