Commit 2714005a authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

internal/singleflight: deflake test

Fixes #11475

Change-Id: Ibaedbb732bb1b9f062bd5af7b866ec4758c724a7
Reviewed-on: https://go-review.googlesource.com/11770Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
parent f11109fb
...@@ -42,11 +42,13 @@ func TestDoErr(t *testing.T) { ...@@ -42,11 +42,13 @@ func TestDoErr(t *testing.T) {
func TestDoDupSuppress(t *testing.T) { func TestDoDupSuppress(t *testing.T) {
var g Group var g Group
c := make(chan string) c := make(chan string, 1)
var calls int32 var calls int32
fn := func() (interface{}, error) { fn := func() (interface{}, error) {
atomic.AddInt32(&calls, 1) atomic.AddInt32(&calls, 1)
return <-c, nil v := <-c
c <- v // pump; make available for any future calls
return v, nil
} }
const n = 10 const n = 10
...@@ -54,20 +56,21 @@ func TestDoDupSuppress(t *testing.T) { ...@@ -54,20 +56,21 @@ func TestDoDupSuppress(t *testing.T) {
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done()
v, err, _ := g.Do("key", fn) v, err, _ := g.Do("key", fn)
if err != nil { if err != nil {
t.Errorf("Do error: %v", err) t.Errorf("Do error: %v", err)
return
} }
if v.(string) != "bar" { if s, _ := v.(string); s != "bar" {
t.Errorf("got %q; want %q", v, "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" c <- "bar"
wg.Wait() wg.Wait()
if got := atomic.LoadInt32(&calls); got != 1 { if got := atomic.LoadInt32(&calls); got <= 0 || got >= n {
t.Errorf("number of calls = %d; want 1", got) t.Errorf("number of calls = %d; want over 0 and less than n", got)
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment