Skip to content

Commit

Permalink
metrics/circonus: fix data races in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbourgon committed May 26, 2016
1 parent c3c0b7a commit 4a6b483
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions metrics/circonus/circonus_test.go
Expand Up @@ -33,13 +33,16 @@ func TestCounter(t *testing.T) {
var (
name = "test_counter"
value uint64
mtx sync.Mutex
)
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type postCounter struct {
Value uint64 `json:"_value"` // reverse-engineered
}
m := map[string]postCounter{}
json.NewDecoder(r.Body).Decode(&m)
mtx.Lock()
defer mtx.Unlock()
value = m[name].Value
}))
defer s.Close()
Expand Down Expand Up @@ -67,6 +70,8 @@ func TestCounter(t *testing.T) {

onceStart.Do(func() { circonusgometrics.Start() })
if err := within(time.Second, func() bool {
mtx.Lock()
defer mtx.Unlock()
return value > 0
}); err != nil {
t.Fatalf("error collecting results: %v", err)
Expand All @@ -84,13 +89,16 @@ func TestGauge(t *testing.T) {
var (
name = "test_gauge"
value float64
mtx sync.Mutex
)
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type postGauge struct {
Value float64 `json:"_value"`
}
m := map[string]postGauge{}
json.NewDecoder(r.Body).Decode(&m)
mtx.Lock()
defer mtx.Unlock()
value = m[name].Value
}))
defer s.Close()
Expand All @@ -110,6 +118,8 @@ func TestGauge(t *testing.T) {
onceStart.Do(func() { circonusgometrics.Start() })

if err := within(time.Second, func() bool {
mtx.Lock()
defer mtx.Unlock()
return value > 0.0
}); err != nil {
t.Fatalf("error collecting results: %v", err)
Expand All @@ -127,6 +137,7 @@ func TestHistogram(t *testing.T) {
var (
name = "test_histogram"
result []string
mtx sync.Mutex
onceDecode sync.Once
)
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -136,6 +147,8 @@ func TestHistogram(t *testing.T) {
onceDecode.Do(func() {
m := map[string]postHistogram{}
json.NewDecoder(r.Body).Decode(&m)
mtx.Lock()
defer mtx.Unlock()
result = m[name].Value
})
}))
Expand All @@ -158,6 +171,8 @@ func TestHistogram(t *testing.T) {
onceStart.Do(func() { circonusgometrics.Start() })

if err := within(time.Second, func() bool {
mtx.Lock()
defer mtx.Unlock()
return len(result) > 0
}); err != nil {
t.Fatalf("error collecting results: %v", err)
Expand Down

0 comments on commit 4a6b483

Please sign in to comment.