diff options
author | Brad Fitzpatrick <bradfitz@golang.org> | 2015-06-30 08:32:02 -0700 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2015-06-30 22:55:59 +0000 |
commit | 2714005a249a0ee03df6c64317b8a2c9ded65808 (patch) | |
tree | d7a9cdc1aa2fb20c34e0c825d93c337a37cfa2e3 | |
parent | f11109fbc93bf2f31b5a948cc29c920b31184011 (diff) | |
download | go-2714005a249a0ee03df6c64317b8a2c9ded65808.tar.gz go-2714005a249a0ee03df6c64317b8a2c9ded65808.zip |
internal/singleflight: deflake test
Fixes #11475
Change-Id: Ibaedbb732bb1b9f062bd5af7b866ec4758c724a7
Reviewed-on: https://go-review.googlesource.com/11770
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r-- | src/internal/singleflight/singleflight_test.go | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/internal/singleflight/singleflight_test.go b/src/internal/singleflight/singleflight_test.go index 30ba7f7ab4..890ab62e03 100644 --- a/src/internal/singleflight/singleflight_test.go +++ b/src/internal/singleflight/singleflight_test.go @@ -42,11 +42,13 @@ func TestDoErr(t *testing.T) { func TestDoDupSuppress(t *testing.T) { var g Group - c := make(chan string) + c := make(chan string, 1) var calls int32 fn := func() (interface{}, error) { atomic.AddInt32(&calls, 1) - return <-c, nil + v := <-c + c <- v // pump; make available for any future calls + return v, nil } const n = 10 @@ -54,20 +56,21 @@ func TestDoDupSuppress(t *testing.T) { for i := 0; i < n; i++ { wg.Add(1) go func() { + defer wg.Done() v, err, _ := g.Do("key", fn) if err != nil { t.Errorf("Do error: %v", err) + return } - if v.(string) != "bar" { - t.Errorf("got %q; want %q", v, "bar") + if s, _ := v.(string); s != "bar" { + t.Errorf("Do = %T %v; want %q", v, v, "bar") } - wg.Done() }() } - time.Sleep(100 * time.Millisecond) // let goroutines above block + time.Sleep(10 * time.Millisecond) // let some goroutines above block in Do c <- "bar" wg.Wait() - if got := atomic.LoadInt32(&calls); got != 1 { - t.Errorf("number of calls = %d; want 1", got) + if got := atomic.LoadInt32(&calls); got <= 0 || got >= n { + t.Errorf("number of calls = %d; want over 0 and less than n", got) } } |