Improve error handling for TestSyncAvatars().

This commit is contained in:
Gucheng Wang 2021-12-02 15:17:17 +08:00
parent 1b511f62d1
commit 3fcf1c444b
6 changed files with 26 additions and 10 deletions

View File

@ -16,6 +16,7 @@ package casdoor
import (
"fmt"
"time"
"github.com/casdoor/casdoor-go-sdk/auth"
"xorm.io/core"
@ -207,7 +208,8 @@ func UpdateUser(owner string, name string, user *auth.User) bool {
affected, err = updateUser(owner, name, user)
if err != nil {
times += 1
if times >= 5 {
time.Sleep(3 * time.Second)
if times >= 10 {
panic(err)
}
} else {

View File

@ -95,7 +95,11 @@ func syncAvatarForUser(user *auth.User) string {
}
}
avatarUrl := uploadDiscuzxAvatar(username, fileBytes, fileExt)
avatarUrl, err := uploadDiscuzxAvatar(username, fileBytes, fileExt)
if err != nil {
panic(err)
}
return avatarUrl
}
@ -126,6 +130,10 @@ func updateDefaultAvatarForUser(user *auth.User) string {
fileExt = path.Ext(newUrl)
avatarUrl := uploadDiscuzxAvatar(username, fileBytes, fileExt)
avatarUrl, err := uploadDiscuzxAvatar(username, fileBytes, fileExt)
if err != nil {
panic(err)
}
return avatarUrl
}

View File

@ -24,7 +24,7 @@ import (
"github.com/casdoor/casdoor-go-sdk/auth"
)
var SyncAvatarsConcurrency = 50
var SyncAvatarsConcurrency = 20
func TestSyncAvatars(t *testing.T) {
object.InitConfig()

View File

@ -26,11 +26,11 @@ import (
"github.com/casbin/casnode/service"
)
func uploadDiscuzxAvatar(username string, fileBytes []byte, fileExt string) string {
func uploadDiscuzxAvatar(username string, fileBytes []byte, fileExt string) (string, error) {
username = url.QueryEscape(username)
memberId := fmt.Sprintf("%s/%s", CasdoorOrganization, username)
fileUrl, _ := service.UploadFileToStorageSafe(memberId, "avatar", "uploadDiscuzxAvatar", fmt.Sprintf("avatar/%s%s", memberId, fileExt), fileBytes, "", "")
return fileUrl
fileUrl, err := service.UploadFileToStorageSafe(memberId, "avatar", "uploadDiscuzxAvatar", fmt.Sprintf("avatar/%s%s", memberId, fileExt), fileBytes, "", "")
return fileUrl, err
}
func convertImageToPng(imageBytes []byte) ([]byte, string, error) {

View File

@ -99,7 +99,8 @@ func getRedirectUrl(url string) string {
_, err = client.Do(req)
if err != nil {
times += 1
if times >= 5 {
time.Sleep(3 * time.Second)
if times >= 10 {
panic(err)
}
} else {

View File

@ -14,7 +14,11 @@
package service
import "github.com/casdoor/casdoor-go-sdk/auth"
import (
"time"
"github.com/casdoor/casdoor-go-sdk/auth"
)
// UploadFileToStorage uploads a file to the path, returns public URL
func UploadFileToStorage(user string, tag string, parent string, fullFilePath string, fileBytes []byte) (string, error) {
@ -30,7 +34,8 @@ func UploadFileToStorageSafe(user string, tag string, parent string, fullFilePat
fileUrl, _, err = auth.UploadResourceEx(user, tag, parent, fullFilePath, fileBytes, createdTime, description)
if err != nil {
times += 1
if times >= 5 {
time.Sleep(3 * time.Second)
if times >= 10 {
return "", err
}
} else {