Add test to prove the data race

This commit is contained in:
Evan Borgstrom 2019-09-25 09:58:55 +08:00
parent 3050d21c67
commit f452bc980a
1 changed files with 21 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package backoff
import ( import (
"reflect" "reflect"
"sync"
"testing" "testing"
"time" "time"
) )
@ -120,6 +121,26 @@ func TestCopy(t *testing.T) {
equals(t, b, b2) equals(t, b, b2)
} }
func TestConcurrent(t *testing.T) {
b := &Backoff{
Min: 100 * time.Millisecond,
Max: 10 * time.Second,
Factor: 2,
}
wg := &sync.WaitGroup{}
test := func() {
time.Sleep(b.Duration())
wg.Done()
}
wg.Add(2)
go test()
go test()
wg.Wait()
}
func between(t *testing.T, actual, low, high time.Duration) { func between(t *testing.T, actual, low, high time.Duration) {
if actual < low { if actual < low {
t.Fatalf("Got %s, Expecting >= %s", actual, low) t.Fatalf("Got %s, Expecting >= %s", actual, low)