Skip to content

Commit

Permalink
Add test to expose #275
Browse files Browse the repository at this point in the history
Signed-off-by: beorn7 <[email protected]>
  • Loading branch information
beorn7 committed Sep 7, 2018
1 parent 7858729 commit 1e08f78
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions prometheus/histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"math"
"math/rand"
"reflect"
"runtime"
"sort"
"sync"
"testing"
Expand Down Expand Up @@ -346,3 +347,41 @@ func TestBuckets(t *testing.T) {
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()
}
}

0 comments on commit 1e08f78

Please sign in to comment.