github.com/grafana/pyroscope@v1.18.0/pkg/util/refctr/refctr_test.go (about) 1 package refctr 2 3 import ( 4 "io" 5 "runtime" 6 "sync" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func Test_Counter(t *testing.T) { 13 const ( 14 workers = 100 15 cycles = 1000 16 ) 17 18 var r Counter 19 var wg sync.WaitGroup 20 var inits int64 21 22 wg.Add(workers) 23 for i := 0; i < workers; i++ { 24 go func() { 25 defer wg.Done() 26 for j := 0; j < cycles; j++ { 27 err := r.Inc(func() error { 28 if j%4 == 0 { 29 return io.EOF 30 } 31 inits++ 32 return nil 33 }) 34 if err != nil { 35 continue 36 } 37 // Let others touch r while 38 // it is initialized. 39 if j%workers == 0 { 40 runtime.Gosched() 41 } 42 r.Dec(func() { 43 inits-- 44 }) 45 } 46 }() 47 } 48 49 wg.Wait() 50 require.Zero(t, inits) 51 }