diff --git a/gee-cache/day6-single-flight/geecache/singleflight/singleflight_test.go b/gee-cache/day6-single-flight/geecache/singleflight/singleflight_test.go index 450951a..44c865d 100644 --- a/gee-cache/day6-single-flight/geecache/singleflight/singleflight_test.go +++ b/gee-cache/day6-single-flight/geecache/singleflight/singleflight_test.go @@ -1,11 +1,14 @@ package singleflight import ( + "sync" "testing" + "time" ) func TestDo(t *testing.T) { var g Group + v, err := g.Do("key", func() (interface{}, error) { return "bar", nil }) @@ -14,3 +17,29 @@ func TestDo(t *testing.T) { t.Errorf("Do v = %v, error = %v", v, err) } } + +func TestGoDo(t *testing.T) { + var g Group + wg := sync.WaitGroup{} + + times := 10 + wg.Add(times) + for i := 0; i < times; i++ { + go func() interface{} { + v, err := g.Do("key", func() (interface{}, error) { + time.Sleep(time.Millisecond * 50) + return "bar", nil + }) + + wg.Done() + + if v != "bar" || err != nil { + t.Errorf("Do v = %v, error = %v", v, err) + } + + return 1 + }() + } + + wg.Wait() +} diff --git a/gee-cache/day7-proto-buf/geecache/singleflight/singleflight_test.go b/gee-cache/day7-proto-buf/geecache/singleflight/singleflight_test.go index 450951a..44c865d 100644 --- a/gee-cache/day7-proto-buf/geecache/singleflight/singleflight_test.go +++ b/gee-cache/day7-proto-buf/geecache/singleflight/singleflight_test.go @@ -1,11 +1,14 @@ package singleflight import ( + "sync" "testing" + "time" ) func TestDo(t *testing.T) { var g Group + v, err := g.Do("key", func() (interface{}, error) { return "bar", nil }) @@ -14,3 +17,29 @@ func TestDo(t *testing.T) { t.Errorf("Do v = %v, error = %v", v, err) } } + +func TestGoDo(t *testing.T) { + var g Group + wg := sync.WaitGroup{} + + times := 10 + wg.Add(times) + for i := 0; i < times; i++ { + go func() interface{} { + v, err := g.Do("key", func() (interface{}, error) { + time.Sleep(time.Millisecond * 50) + return "bar", nil + }) + + wg.Done() + + if v != "bar" || err != nil { + t.Errorf("Do v = %v, error = %v", v, err) + } + + return 1 + }() + } + + wg.Wait() +}