cwtch/server/metrics/metrics_test.go

70 lines
1.6 KiB
Go

package metrics
import (
"runtime"
"testing"
"time"
)
func TestCounter(t *testing.T) {
starttime := time.Now()
c := NewCounter()
for i := 0; i < 100; i++ {
go func() {
c.Add(1)
}()
}
time.Sleep(2 * time.Second)
val := c.Count()
if val != 100 {
t.Errorf("counter count was not 100")
}
counterStart := c.GetStarttime()
if counterStart.Sub(starttime) > time.Millisecond {
t.Error("counter's starttime was innaccurate")
}
}
func TestMonitorHistory(t *testing.T) {
value := float64(60 * 24 * 7 * 4 * 12)
numGoRoutinesStart := runtime.NumGoroutine()
mh := NewMonitorHistory(Count, func() float64 { return value })
time.Sleep(1 * time.Second)
mh.Stop()
time.Sleep(1 * time.Second)
minutes := mh.Minutes()
if minutes[0] != value {
t.Errorf("Monitor minutes's first minutes slot had the wrong value. Expected: %f Actual: %f", value, minutes[0])
}
hours := mh.Hours()
if hours[0] != value/60 {
t.Errorf("Monitor hour's first hour slot had the wrong value. Expected: %f Actual: %f", value, hours[0])
}
days := mh.Days()
if days[0] != value/60/24 {
t.Errorf("Monitor day's first day slot had the wrong value. Expected: %f Actual: %f", value/24, days[0])
}
weeks := mh.Weeks()
if weeks[0] != value/60/24/7 {
t.Errorf("Monitor week's first day slot had the wrong value. Expected: %f Actual: %f", value/24/7, weeks[0])
}
months := mh.Months()
if months[0] != 12 {
t.Errorf("Monitor months's first day slot had the wrong value. Expected: %d Actual: %f", 12, months[0])
}
if numGoRoutinesStart != runtime.NumGoroutine() {
t.Errorf("Monitor hour did not stop")
}
}