Prechádzať zdrojové kódy

add race test for Gauge

mihasya 7 rokov pred
rodič
commit
92920137f5
1 zmenil súbory, kde vykonal 19 pridanie a 0 odobranie
  1. 19 0
      gauge_test.go

+ 19 - 0
gauge_test.go

@@ -2,7 +2,10 @@ package metrics
 
 import (
 	"fmt"
+	"math/rand"
+	"sync"
 	"testing"
+	"time"
 )
 
 func BenchmarkGuage(b *testing.B) {
@@ -13,6 +16,22 @@ func BenchmarkGuage(b *testing.B) {
 	}
 }
 
+// exercise race detector
+func TestGaugeConcurrency(t *testing.T) {
+	rand.Seed(time.Now().Unix())
+	g := NewGauge()
+	wg := &sync.WaitGroup{}
+	reps := 100
+	for i := 0; i < reps; i++ {
+		wg.Add(1)
+		go func(g Gauge, wg *sync.WaitGroup) {
+			g.Update(rand.Int63())
+			wg.Done()
+		}(g, wg)
+	}
+	wg.Wait()
+}
+
 func TestGauge(t *testing.T) {
 	g := NewGauge()
 	g.Update(int64(47))