代码原来的地址是:https://github.com/golang/groupcache/blob/master/singleflight/singleflight_test.go
片段如下:
<pre>
func TestDoDupSuppress(t *testing.T) {
var g Group
c:=make(chan string)
var calls int32
fn:= func() (interface{},error){
atomic.AddInt32(&calls,1)
return <-c,nil
}
const n=10
var wg sync.WaitGroup
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
v,err:=g.Do("key",fn)
if err != nil {
t.Errorf("Do error: %v",err)
}
if v.(string) != "bar" {
t.Errorf("got %q; want %q",v,"bar")
}
wg.Done()
}()
}
//不sleep会卡住,执行不下去,为什么呢
// 原来的注释是 “ let goroutines above block”
//time.Sleep(100*time.Millisecond)
c<-"bar"
wg.Wait()
if got := atomic.LoadInt32(&calls);got!=1 {
t.Errorf("number of calls = %d;want 1",got)
}
}
</pre>
有疑问加站长微信联系(非本文作者)