Add test to expose #275

Signed-off-by: beorn7 <beorn@soundcloud.com>
This commit is contained in:
beorn7 2018-09-07 18:58:14 +02:00
parent 7858729281
commit 1e08f788cf
1 changed files with 39 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import (
"math" "math"
"math/rand" "math/rand"
"reflect" "reflect"
"runtime"
"sort" "sort"
"sync" "sync"
"testing" "testing"
@ -346,3 +347,41 @@ func TestBuckets(t *testing.T) {
t.Errorf("linear buckets: got %v, want %v", got, want) t.Errorf("linear buckets: got %v, want %v", got, want)
} }
} }
func TestHistogramAtomicObserve(t *testing.T) {
var (
quit = make(chan struct{})
his = NewHistogram(HistogramOpts{
Buckets: []float64{0.5, 10, 20},
})
)
defer func() { close(quit) }()
go func() {
for {
select {
case <-quit:
return
default:
his.Observe(1)
}
}
}()
for i := 0; i < 100; i++ {
m := &dto.Metric{}
if err := his.Write(m); err != nil {
t.Fatal("unexpected error writing histogram:", err)
}
h := m.GetHistogram()
if h.GetSampleCount() != uint64(h.GetSampleSum()) ||
h.GetSampleCount() != h.GetBucket()[1].GetCumulativeCount() {
t.Fatalf(
"inconsistent counts in histogram: count=%d sum=%f bucket=%d",
h.GetSampleCount(), h.GetSampleSum(), h.GetBucket()[1].GetCumulativeCount(),
)
}
runtime.Gosched()
}
}