github.com/suiyunonghen/DxCommonLib@v0.5.3/sync/sharecalls_test.go (about) 1 package sync 2 3 import ( 4 "fmt" 5 "os" 6 "sync" 7 "testing" 8 "time" 9 ) 10 11 func TestShareCalls_ShareCall(t *testing.T) { 12 var calls ShareCalls 13 var wg sync.WaitGroup 14 ntime := time.Now() 15 wg.Add(20) 16 for i := 0;i<10;i++{ 17 go func(index int) { 18 shareId := index % 5 19 result,cacheValue,_ := calls.ShareCall(shareId, func(i ...interface{}) (interface{}, error) { 20 time.Sleep(time.Second) 21 return 30*index,nil 22 }) 23 wg.Done() 24 fmt.Fprintf(os.Stdout, "%d Intfunc(%d)=(%v,%v)\r\n",index,shareId,result,cacheValue) 25 }(i) 26 27 go func(index int) { 28 KeyResutl,KeyCacheValue,_ := calls.ShareCallByKey("Key001", func(i ...interface{}) (interface{}, error) { 29 time.Sleep(time.Second) 30 return "返回的是Key",nil 31 }) 32 wg.Done() 33 fmt.Fprintf(os.Stdout,"Keyfunc(%d)=(%v,%v)\r\n",index,KeyResutl,KeyCacheValue) 34 }(i) 35 } 36 37 wg.Wait() 38 fmt.Println("执行时长:", time.Now().Sub(ntime)) 39 40 }