2018-06-21 18:29:02 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCounter(t *testing.T) {
|
|
|
|
starttime := time.Now()
|
|
|
|
c := NewCounter()
|
|
|
|
|
2020-01-13 22:11:00 +00:00
|
|
|
max := 100
|
|
|
|
done := make(chan bool, max)
|
|
|
|
|
|
|
|
// slightly stress test atomic nature of metric by flooding with threads Add()ing
|
|
|
|
for i := 0; i < max; i++ {
|
2018-06-21 18:29:02 +00:00
|
|
|
go func() {
|
|
|
|
c.Add(1)
|
2020-01-13 22:11:00 +00:00
|
|
|
done <- true
|
2018-06-21 18:29:02 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-01-13 22:11:00 +00:00
|
|
|
for i := 0; i < max; i++ {
|
2020-02-03 18:46:15 +00:00
|
|
|
<-done
|
2020-01-13 22:11:00 +00:00
|
|
|
}
|
2018-06-21 18:29:02 +00:00
|
|
|
|
|
|
|
val := c.Count()
|
|
|
|
if val != 100 {
|
|
|
|
t.Errorf("counter count was not 100")
|
|
|
|
}
|
|
|
|
|
|
|
|
counterStart := c.GetStarttime()
|
|
|
|
|
|
|
|
if counterStart.Sub(starttime) > time.Millisecond {
|
2020-09-28 18:46:18 +00:00
|
|
|
t.Errorf("counter's starttime was innaccurate %v", counterStart.Sub(starttime))
|
2018-06-21 18:29:02 +00:00
|
|
|
}
|
|
|
|
}
|