github.com/sandwich-go/boost@v1.3.29/singleflight/single_test.go (about) 1 package singleflight 2 3 import ( 4 . "github.com/smartystreets/goconvey/convey" 5 "sync" 6 "testing" 7 "time" 8 ) 9 10 func TestSingleFlight(t *testing.T) { 11 Convey("test single flight", t, func() { 12 var do1 = func() (interface{}, error) { 13 time.Sleep(100 * time.Millisecond) 14 return 1, nil 15 } 16 var do2 = func() (interface{}, error) { 17 time.Sleep(100 * time.Millisecond) 18 return 2, nil 19 } 20 21 var key = "key" 22 var g = New() 23 var wg sync.WaitGroup 24 wg.Add(2) 25 var ret1, ret2 interface{} 26 go func() { 27 ret1, _ = g.Do(key, do1) 28 wg.Done() 29 }() 30 go func() { 31 ret2, _ = g.Do(key, do2) 32 wg.Done() 33 }() 34 wg.Wait() 35 So(ret1, ShouldEqual, ret2) 36 }) 37 }