Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 7bca086

Browse files
committed
add race test for EWMA
1 parent 9073533 commit 7bca086

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

ewma_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package metrics
22

3-
import "testing"
3+
import (
4+
"math/rand"
5+
"sync"
6+
"testing"
7+
"time"
8+
)
49

510
func BenchmarkEWMA(b *testing.B) {
611
a := NewEWMA1()
@@ -23,6 +28,23 @@ func BenchmarkEWMAParallel(b *testing.B) {
2328
})
2429
}
2530

31+
// Just updating a EWMA in some goroutines to give the race detector a chance
32+
// to detect problems
33+
func TestEWMAConcurrency(t *testing.T) {
34+
rand.Seed(time.Now().Unix())
35+
a := NewEWMA1()
36+
wg := &sync.WaitGroup{}
37+
reps := 100
38+
for i := 0; i < reps; i++ {
39+
wg.Add(1)
40+
go func(ewma EWMA, wg *sync.WaitGroup) {
41+
a.Update(rand.Int63())
42+
wg.Done()
43+
}(a, wg)
44+
}
45+
wg.Wait()
46+
}
47+
2648
func TestEWMA1(t *testing.T) {
2749
a := NewEWMA1()
2850
a.Update(3)

0 commit comments

Comments
 (0)