作者 yangfu

fix token repeat fetch in concurrent env

... ... @@ -183,6 +183,11 @@ func (notify *GetuiNotification) GetAuthToken() (token string, err error) {
authMux.Lock()
defer authMux.Unlock()
// recheck
if authtoken != "" && expire.Unix() > time.Now().Unix() {
token = authtoken
return
}
url := notify.Url(notify.Options.AppId, authSign)
notify.Request = httplib.Post(strings.TrimSpace(url))
req := &AuthSignRequest{
... ...
... ... @@ -3,6 +3,7 @@ package getuiV2
import (
"openapi/pkg/infrastructure/push"
"openapi/pkg/infrastructure/utils"
"sync"
"testing"
)
... ... @@ -77,3 +78,29 @@ func TestGetuiPrd(t *testing.T) {
t.Fatal(err)
}
}
func TestGetAuthToken(t *testing.T) {
var wg sync.WaitGroup
round := 100
notification := &GetuiNotification{}
var tokenMap = make(map[string]string)
notification.Init(
push.DebugModule(true),
push.AppId("WgrbaaStTk7JElrXOCgUg6"),
push.AppKey("FG5lbqVrHa5rS9NVfxNP7"),
push.AppMasterSecret("FW3jMNLJrRARYKv2iqA5H5"),
)
for i := 0; i < round; i++ {
wg.Add(1)
go func() {
defer wg.Done()
token, _ := notification.GetAuthToken()
tokenMap[token] = token
}()
}
wg.Wait()
if len(tokenMap) > 1 {
t.Fatalf("token want 1 get %v", len(tokenMap))
}
}
... ...