From f452bc980a8268bc1391bf6f6d03f587ead6c01c Mon Sep 17 00:00:00 2001 From: Evan Borgstrom Date: Wed, 25 Sep 2019 09:58:55 +0800 Subject: [PATCH] Add test to prove the data race --- backoff_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/backoff_test.go b/backoff_test.go index d1c9845..3f49f0a 100644 --- a/backoff_test.go +++ b/backoff_test.go @@ -2,6 +2,7 @@ package backoff import ( "reflect" + "sync" "testing" "time" ) @@ -120,6 +121,26 @@ func TestCopy(t *testing.T) { 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) { if actual < low { t.Fatalf("Got %s, Expecting >= %s", actual, low)