From 377a60474e019810efe9151de1bd4fb215424b7f Mon Sep 17 00:00:00 2001 From: BanTanger <88583317+BanTanger@users.noreply.github.com> Date: Thu, 20 Jul 2023 12:43:44 +0800 Subject: [PATCH] feat: add test function about 1000 users join group send msg (#124) Signed-off-by: BanTanger --- testv3/funcation/account.go | 19 +++- testv3/funcation/config.go | 24 +++- testv3/funcation/conversation.go | 17 --- testv3/funcation/listener.go | 29 +++-- testv3/funcation/login.go | 26 ++++- testv3/funcation/reliability.go | 187 ------------------------------- testv3/funcation/sendMessage.go | 61 ---------- testv3/funcation/sendmsg.go | 14 +++ testv3/funcation/struct.go | 16 ++- testv3/funcation/util.go | 69 ++++-------- testv3/login_test.go | 14 +++ testv3/msg_test.go | 140 +++++++++++++++++++++++ testv3/register_test.go | 46 ++++++-- 13 files changed, 309 insertions(+), 353 deletions(-) delete mode 100644 testv3/funcation/conversation.go delete mode 100644 testv3/funcation/reliability.go delete mode 100644 testv3/funcation/sendMessage.go diff --git a/testv3/funcation/account.go b/testv3/funcation/account.go index 8a521ea14..a993d4991 100644 --- a/testv3/funcation/account.go +++ b/testv3/funcation/account.go @@ -1,3 +1,17 @@ +// Copyright © 2023 OpenIM SDK. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package funcation import ( @@ -11,6 +25,7 @@ import ( func RegisterOne(uid, nickname, faceurl string) (bool, error) { InitContext(uid) + // config.UserID = uid res, err := checkUserAccount(uid) if err != nil { return false, err @@ -49,7 +64,7 @@ func checkUserAccount(uid string) (bool, error) { var getAccountCheckResp userPB.AccountCheckResp getAccountCheckReq.CheckUserIDs = []string{uid} for { - err := util.ApiPost(ctx, "/user/account_check", &getAccountCheckReq, &getAccountCheckResp) + err := util.ApiPost(ctx, ACCOUNT_CHECK, &getAccountCheckReq, &getAccountCheckResp) if err != nil { return false, err } @@ -75,7 +90,7 @@ func registerUserAccount(uid, nickname, faceurl string) bool { req.Users = []*sdkws.UserInfo{{UserID: uid, Nickname: nickname, FaceURL: faceurl}} req.Secret = Secret for { - err := util.ApiPost(ctx, "/user/user_register", &req, nil) + err := util.ApiPost(ctx, USER_REGISTER, &req, nil) if err != nil { log.Error("post failed ,continue ", err.Error(), REGISTERADDR) time.Sleep(100 * time.Millisecond) diff --git a/testv3/funcation/config.go b/testv3/funcation/config.go index 4b87833b5..b1552d1e3 100644 --- a/testv3/funcation/config.go +++ b/testv3/funcation/config.go @@ -36,10 +36,11 @@ var Config = sdk_struct.IMConfig{ // log and token var ( - rotateCount = uint(0) - LogLevel = uint32(6) - PlatformID = int32(1) - Secret = "tuoyun" + rotateCount = uint(0) + LogLevel = uint32(6) + PlatformID = int32(1) + Secret = "tuoyun" + // Secret = "openIM123" IsLogStandardOutput = true isLogJson = false LogName = "" @@ -56,7 +57,10 @@ var ( ) func init() { - AdminToken, _ = getToken("openIM123456") + AllLoginMgr = make(map[string]*CoreNode) + + AdminUID := "openIM123456" + AdminToken, _ = GetToken(AdminUID) // init log if err := imLog.InitFromConfig( "open-im-sdk-core", LogName, int(LogLevel), IsLogStandardOutput, isLogJson, LogFilePath, rotateCount); err != nil { @@ -67,7 +71,8 @@ func init() { // system var ( // TESTIP = "59.36.173.89" - TESTIP = "203.56.175.233" + TESTIP = "203.56.175.233" + // TESTIP = "43.154.157.177" APIADDR = "http://" + TESTIP + ":10002" WSADDR = "ws://" + TESTIP + ":10001" REGISTERADDR = APIADDR + "/auth/user_register" @@ -95,3 +100,10 @@ var sendMsgClient = 0 var ( testConversation conversationCallBack ) + +// route +var ( + RPC_USER_TOKEN = "/auth/user_token" + ACCOUNT_CHECK = "/user/account_check" + USER_REGISTER = "/user/user_register" +) diff --git a/testv3/funcation/conversation.go b/testv3/funcation/conversation.go deleted file mode 100644 index 83770ec9f..000000000 --- a/testv3/funcation/conversation.go +++ /dev/null @@ -1,17 +0,0 @@ -// @Author BanTanger 2023/7/11 10:27 -package funcation - -import ( - "open_im_sdk/open_im_sdk" - "open_im_sdk/pkg/sdk_params_callback" - "open_im_sdk/pkg/utils" -) - -func GetConversation(uid, conversationID, startMsgID string, count int) { - var params sdk_params_callback.GetAdvancedHistoryMessageListParams - params.UserID = AllLoginMgr[uid].UserID - params.ConversationID = conversationID - params.StartClientMsgID = startMsgID - params.Count = count - open_im_sdk.GetAdvancedHistoryMessageList(&testConversation, utils.OperationIDGenerator(), utils.StructToJsonString(params)) -} diff --git a/testv3/funcation/listener.go b/testv3/funcation/listener.go index 7c40d65de..3cfbc7cbe 100644 --- a/testv3/funcation/listener.go +++ b/testv3/funcation/listener.go @@ -22,35 +22,35 @@ import ( "time" ) -func (t *testInitLister) OnUserTokenExpired() { +func (t *initLister) OnUserTokenExpired() { log.Info("", utils.GetSelfFuncName()) } -func (t *testInitLister) OnConnecting() { +func (t *initLister) OnConnecting() { log.Info("", utils.GetSelfFuncName()) } -func (t *testInitLister) OnConnectSuccess() { +func (t *initLister) OnConnectSuccess() { log.Info("", utils.GetSelfFuncName()) } -func (t *testInitLister) OnConnectFailed(ErrCode int32, ErrMsg string) { +func (t *initLister) OnConnectFailed(ErrCode int32, ErrMsg string) { log.Info("", utils.GetSelfFuncName(), ErrCode, ErrMsg) } -func (t *testInitLister) OnKickedOffline() { +func (t *initLister) OnKickedOffline() { log.Error("", utils.GetSelfFuncName(), "kick!!!!!!!!!!!!!!!!!!!!") os.Exit(-1) } -func (t *testInitLister) OnSelfInfoUpdated(info string) { +func (t *initLister) OnSelfInfoUpdated(info string) { log.Info("", utils.GetSelfFuncName()) } -func (t *testInitLister) OnSuccess() { +func (t *initLister) OnSuccess() { log.Info("", utils.GetSelfFuncName()) } -func (t *testInitLister) OnError(code int32, msg string) { +func (t *initLister) OnError(code int32, msg string) { log.Info("", utils.GetSelfFuncName(), code, msg) } @@ -69,12 +69,12 @@ func (b *BaseSuccessFailed) OnSuccess(data string) { func (c *conversationCallBack) OnError(errCode int32, errMsg string) { // TODO implement me - panic("implement me") + // panic("implement me") } func (c *conversationCallBack) OnSuccess(data string) { // TODO implement me - panic("implement me") + // panic("implement me") } func (c *conversationCallBack) OnNewConversation(conversationList string) { @@ -87,7 +87,7 @@ func (c *conversationCallBack) OnConversationChanged(conversationList string) { func (m *MsgListenerCallBak) OnRecvOfflineNewMessage(message string) { // TODO implement me - panic("implement me") + // panic("implement me") } func (m *MsgListenerCallBak) OnRecvNewMessage(message string) { @@ -113,7 +113,6 @@ func (testGroupListener) OnGroupMemberAdded(callbackInfo string) { } func (testGroupListener) OnGroupMemberDeleted(callbackInfo string) { log.Info(utils.OperationIDGenerator(), utils.GetSelfFuncName(), callbackInfo) - } func (testGroupListener) OnGroupApplicationAdded(callbackInfo string) { @@ -194,7 +193,7 @@ func (m *MsgListenerCallBak) OnMsgDeleted(s string) {} func (m *MsgListenerCallBak) OnRecvOfflineNewMessages(messageList string) { // TODO implement me - panic("implement me") + // panic("implement me") } func (m *MsgListenerCallBak) OnRecvMessageExtensionsAdded(msgID string, reactionExtensionList string) { @@ -223,11 +222,11 @@ func (userCallback) OnSelfInfoUpdated(callbackData string) { } func (c *conversationCallBack) OnRecvMessageExtensionsChanged(msgID string, reactionExtensionList string) { - panic("implement me") + // panic("implement me") } func (c *conversationCallBack) OnRecvMessageExtensionsDeleted(msgID string, reactionExtensionKeyList string) { - panic("implement me") + // panic("implement me") } func (c *conversationCallBack) OnSyncServerProgress(progress int) { diff --git a/testv3/funcation/login.go b/testv3/funcation/login.go index ef066fd28..fca636515 100644 --- a/testv3/funcation/login.go +++ b/testv3/funcation/login.go @@ -1,3 +1,17 @@ +// Copyright © 2023 OpenIM SDK. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package funcation import ( @@ -18,29 +32,29 @@ func LoginOne(uid string) bool { // 返回值:成功登录和失败登录的 uidList func LoginBatch(uidList []string) ([]string, []string) { var successList, failList []string - for i, uid := range uidList { + for _, uid := range uidList { if LoginOne(uid) == true { - successList[i] = uid + successList = append(successList, uid) } else { - failList[i] = uid + failList = append(failList, uid) } } return successList, failList } func collectToken(uid string) { - token, _ := getToken(uid) + token, _ := GetToken(uid) coreMgrLock.Lock() defer coreMgrLock.Unlock() AllLoginMgr[uid] = &CoreNode{Token: token, UserID: uid} } func initAndLogin(uid, token string) bool { - var testinit testInitLister + var init initLister lg := new(login.LoginMgr) - lg.InitSDK(Config, &testinit) + lg.InitSDK(Config, &init) log.Info(uid, "new login ", lg) AllLoginMgr[uid].Mgr = lg log.Info(uid, "InitSDK ", Config, "index mgr", uid, lg) diff --git a/testv3/funcation/reliability.go b/testv3/funcation/reliability.go deleted file mode 100644 index dcfd4b52f..000000000 --- a/testv3/funcation/reliability.go +++ /dev/null @@ -1,187 +0,0 @@ -package funcation - -import ( - "math/rand" - "open_im_sdk/open_im_sdk" - "open_im_sdk/pkg/log" - "open_im_sdk/pkg/sdk_params_callback" - "open_im_sdk/pkg/utils" - "os" - "runtime" - "strconv" - "sync" - "time" -) - -func RegisterReliabilityUser(id int, timeStamp string) { - userID := GenUid(id, "reliability_"+timeStamp) - // Register(userID) - token, _ := RunGetToken(userID) - coreMgrLock.Lock() - defer coreMgrLock.Unlock() - AllLoginMgr[strconv.Itoa(id)] = &CoreNode{Token: token, UserID: userID} -} - -func ReliabilityTest(msgNumOneClient int, intervalSleepMS int, randSleepMaxSecond int, clientNum int) { - msgNumInOneClient = msgNumOneClient - var wg sync.WaitGroup - // 注册 - wg.Add(clientNum) - for i := 0; i < clientNum; i++ { - go func(idx int) { - RegisterReliabilityUser(idx, utils.Int64ToString(time.Now().Unix())) - wg.Done() - }(i) - } - wg.Wait() - - log.Warn("", "RegisterReliabilityUser finished, clientNum: ", clientNum) - log.Warn("", " init, login, send msg, start ") - rand.Seed(time.Now().UnixNano()) - - // 用户立刻登录发消息 - wg.Add(clientNum) - for i := 0; i < clientNum; i++ { - rdSleep := rand.Intn(randSleepMaxSecond) + 1 - isSend := 0 // 消息是否成功发送控制量 - if isSend == 0 { - go func(idx int) { - log.Warn("", " send msg flag true ", idx) - ReliabilityOne(idx, rdSleep, true, intervalSleepMS) - wg.Done() - }(i) - sendMsgClient++ - } else { - go func(idx int) { - log.Warn("", " send msg flag false ", idx) - ReliabilityOne(idx, rdSleep, false, intervalSleepMS) - wg.Done() - }(i) - } - } - wg.Wait() - log.Warn("send msg finish, CheckReliabilityResult") - time.Sleep(time.Duration(3000) * time.Second) - - // 所有成员拉取自己的会话是否有更新 - for i := 0; i < clientNum; i++ { - var params sdk_params_callback.GetAdvancedHistoryMessageListParams - params.UserID = AllLoginMgr[strconv.Itoa(i)].UserID - // params.ConversationID = "si_7788_7789" - // params.StartClientMsgID = "83ca933d559d0374258550dd656a661c" - params.Count = 20 - open_im_sdk.GetAdvancedHistoryMessageList(&testConversation, utils.OperationIDGenerator(), utils.StructToJsonString(params)) - } - - // for { - // // 消息异步落库可能出现延迟,每隔五秒再检查一次 - // if CheckReliabilityResult(msgNumOneClient, clientNum) { - // log.Warn("", "CheckReliabilityResult ok, exit") - // os.Exit(0) - // return - // } else { - // log.Warn("", "CheckReliabilityResult failed , wait.... ") - // } - // time.Sleep(time.Duration(5) * time.Second) - // } -} - -func ReliabilityOne(index int, beforeLoginSleep int, isSendMsg bool, intervalSleepMS int) { - // time.Sleep(time.Duration(beforeLoginSleep) * time.Second) - strMyUid := AllLoginMgr[strconv.Itoa(index)].UserID - token := AllLoginMgr[strconv.Itoa(index)].Token - // ReliabilityInitAndLogin(index, strMyUid, token) - log.Info("", "login ok client num: ", len(AllLoginMgr)) - log.Warn("start One", index, beforeLoginSleep, isSendMsg, strMyUid, token, WSADDR, APIADDR) - - msgnum := msgNumInOneClient - uidNum := len(AllLoginMgr) - rand.Seed(time.Now().UnixNano()) - if msgnum == 0 { - os.Exit(0) - } - if !isSendMsg { - // Msgwg.Done() - } else { - for i := 0; i < msgnum; i++ { - var r int - // time.Sleep(time.Duration(intervalSleepMS) * time.Millisecond) - // 互相发送消息,非自己 - for { - r = rand.Intn(uidNum) - if r == index { - continue - } else { - break - } - } - recvId := AllLoginMgr[strconv.Itoa(r)].UserID - idx := strconv.FormatInt(int64(i), 10) - for { - if runtime.NumGoroutine() > MaxNumGoroutine { - time.Sleep(time.Duration(intervalSleepMS) * time.Millisecond) - log.Warn("", "NumGoroutine > max ", runtime.NumGoroutine(), MaxNumGoroutine) - continue - } else { - break - } - } - DoTestSendMsg(index, strMyUid, recvId, "", idx) - } - // Msgwg.Done() - } -} - -func CheckReliabilityResult(msgNumOneClient int, clientNum int) bool { - log.Info("", "start check map send -> map recv") - sameNum := 0 - - // 消息数量不一致说明出现丢失 - if len(SendSuccAllMsg)+len(SendFailedAllMsg) != msgNumOneClient*clientNum { - log.Warn("", utils.GetSelfFuncName(), " send msg success number: ", len(SendSuccAllMsg), - " send msg failed number: ", len(SendFailedAllMsg), " all: ", msgNumOneClient*clientNum) - return false - } - - for ksend, _ := range SendSuccAllMsg { - _, ok := RecvAllMsg[ksend] // RecvAllMsg 的初始化何时? - if ok { - sameNum++ - } else { - // 埋点日志,第 ksend 个消息数据 本地和服务器不一致 - log.Error("", "check failed not in recv ", ksend) - log.Error("", "send failed num: ", len(SendFailedAllMsg), - " send success num: ", len(SendSuccAllMsg), " recv num: ", len(RecvAllMsg)) - return false - } - } - log.Info("", "check map send -> map recv ok ", sameNum) - maxCostMsgID := "" - minCostTime := int64(1000000) - maxCostTime := int64(0) - totalCostTime := int64(0) - for ksend, vsend := range SendSuccAllMsg { - krecv, ok := RecvAllMsg[ksend] - if ok { - sameNum++ - costTime := krecv.RecvTime - vsend.SendTime - totalCostTime += costTime - if costTime > maxCostTime { - maxCostMsgID = ksend - maxCostTime = costTime - } - if minCostTime > costTime { - minCostTime = costTime - } - } - } - - log.Warn("", "need send msg num : ", sendMsgClient*msgNumInOneClient) - log.Warn("", "send msg succ num ", len(SendSuccAllMsg)) - log.Warn("", "send msg failed num ", len(SendFailedAllMsg)) - log.Warn("", "recv msg succ num ", len(RecvAllMsg)) - log.Warn("", "minCostTime: ", minCostTime, "ms, maxCostTime: ", maxCostTime, "ms, average cost time: ", - totalCostTime/(int64(sendMsgClient*msgNumInOneClient)), "ms", " maxCostMsgID: ", maxCostMsgID) - - return true -} diff --git a/testv3/funcation/sendMessage.go b/testv3/funcation/sendMessage.go deleted file mode 100644 index 1089ae0e5..000000000 --- a/testv3/funcation/sendMessage.go +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright © 2023 OpenIM SDK. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package funcation - -import ( - "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" - "open_im_sdk/pkg/log" - "open_im_sdk/pkg/utils" - "strconv" -) - -func init() { - AllLoginMgr = make(map[string]*CoreNode) - SendSuccAllMsg = make(map[string]*SendRecvTime) -} - -var SendSuccAllMsg map[string]*SendRecvTime // msgid->send+recv: -var SendFailedAllMsg map[string]string -var RecvAllMsg map[string]*SendRecvTime // msgid->send+recv - -// 基准函数不应该做模拟,这一部分逻辑应该放在 test 文件中自行模拟 -func DoTestSendMsg(index int, sendId, recvID string, groupID string, idx string) { - m := "test msg " + sendId + ":" + recvID + ":" + idx - operationID := utils.OperationIDGenerator() - log.Info(operationID, "CreateTextMessage conv: ", AllLoginMgr[strconv.Itoa(index)].Mgr.Conversation(), "index: ", index) - s, err := AllLoginMgr[strconv.Itoa(index)].Mgr.Conversation().CreateTextMessage(ctx, m) - if err != nil { - log.Error(operationID, "CreateTextMessage", err) - return - } - - testSendMsg := TestSendMsgCallBack{ - OperationID: operationID, - sendTime: utils.GetCurrentTimestampByMill(), - sendID: sendId, - recvID: recvID, - groupID: groupID, - msgID: s.ClientMsgID, - } - o := sdkws.OfflinePushInfo{Title: "title", Desc: "desc"} - - log.Info(operationID, "SendMessage", sendId, recvID, groupID, testSendMsg.msgID, index) - // 如果 recvID 为空 代表发送群聊消息,反之 - AllLoginMgr[strconv.Itoa(index)].Mgr.Conversation().SendMessage(ctx, s, recvID, groupID, &o) - SendMsgMapLock.Lock() - defer SendMsgMapLock.Unlock() - x := SendRecvTime{SendTime: utils.GetCurrentTimestampByMill()} - SendSuccAllMsg[testSendMsg.msgID] = &x -} diff --git a/testv3/funcation/sendmsg.go b/testv3/funcation/sendmsg.go index ddbcc9f7b..509d80980 100644 --- a/testv3/funcation/sendmsg.go +++ b/testv3/funcation/sendmsg.go @@ -1,3 +1,17 @@ +// Copyright © 2023 OpenIM SDK. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + // @Author BanTanger 2023/7/10 15:30:00 package funcation diff --git a/testv3/funcation/struct.go b/testv3/funcation/struct.go index 28cea9dc5..d0b3eb4cd 100644 --- a/testv3/funcation/struct.go +++ b/testv3/funcation/struct.go @@ -1,3 +1,17 @@ +// Copyright © 2023 OpenIM SDK. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + // @Author BanTanger 2023/7/10 16:01 package funcation @@ -20,7 +34,7 @@ type BaseSuccessFailed struct { time time.Time } -type testInitLister struct { +type initLister struct { } type conversationCallBack struct { diff --git a/testv3/funcation/util.go b/testv3/funcation/util.go index d9816d1be..530052264 100644 --- a/testv3/funcation/util.go +++ b/testv3/funcation/util.go @@ -1,51 +1,39 @@ +// Copyright © 2023 OpenIM SDK. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package funcation import ( "context" authPB "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/auth" - "net" "open_im_sdk/internal/util" "open_im_sdk/pkg/ccontext" "open_im_sdk/pkg/log" "open_im_sdk/pkg/utils" "open_im_sdk/sdk_struct" - "os" - "strconv" - "time" ) -func GenUid(uid int, prefix string) string { - if getMyIP() == "" { - log.Error("", "getMyIP() failed, exit ") - os.Exit(1) - } - UidPrefix := getMyIP() + "_" + prefix + "_" - return UidPrefix + strconv.FormatInt(int64(uid), 10) -} - -func getMyIP() string { - addrs, err := net.InterfaceAddrs() - if err != nil { - log.Error("", "InterfaceAddrs failed ", err.Error()) - os.Exit(1) - return "" - } - for _, address := range addrs { - if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { - if ipnet.IP.To4() != nil { - return ipnet.IP.String() - } - } - } - return "" -} - -func getToken(uid string) (string, int64) { +func GetToken(uid string) (string, int64) { InitContext(uid) config.Token = "" - req := authPB.UserTokenReq{PlatformID: PlatformID, UserID: uid, Secret: Secret} + req := authPB.UserTokenReq{ + PlatformID: PlatformID, + UserID: uid, + Secret: Secret, + } resp := authPB.UserTokenResp{} - err := util.ApiPost(ctx, "/auth/user_token", &req, &resp) + err := util.ApiPost(ctx, RPC_USER_TOKEN, &req, &resp) if err != nil { log.Error(req.UserID, "ApiPost failed ", err.Error(), TOKENADDR, req) return "", 0 @@ -55,21 +43,6 @@ func getToken(uid string) (string, int64) { return resp.Token, resp.ExpireTimeSeconds } -func RunGetToken(strMyUid string) (string, int64) { - var token string - var exprie int64 - for true { - token, exprie = getToken(strMyUid) - if token == "" { - time.Sleep(time.Duration(100) * time.Millisecond) - continue - } else { - break - } - } - return token, exprie -} - func InitContext(uid string) context.Context { config = ccontext.GlobalConfig{ UserID: uid, diff --git a/testv3/login_test.go b/testv3/login_test.go index 8c5f6a0f6..243af5043 100644 --- a/testv3/login_test.go +++ b/testv3/login_test.go @@ -1,3 +1,17 @@ +// Copyright © 2023 OpenIM SDK. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + // @Author BanTanger 2023/7/10 12:30:00 package testv3 diff --git a/testv3/msg_test.go b/testv3/msg_test.go index 77641e9d1..8f84957a3 100644 --- a/testv3/msg_test.go +++ b/testv3/msg_test.go @@ -1,3 +1,17 @@ +// Copyright © 2023 OpenIM SDK. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + // @Author BanTanger 2023/7/10 15:30:00 package testv3 @@ -5,6 +19,7 @@ import ( "context" "fmt" "open_im_sdk/pkg/ccontext" + "open_im_sdk/pkg/constant" "open_im_sdk/pkg/utils" "open_im_sdk/sdk_struct" "open_im_sdk/testv3/funcation" @@ -201,3 +216,128 @@ func Test_SendMsgByGroup(t *testing.T) { // funcation.GetConversation(userA, conversationID, message.ClientMsgID, count) time.Sleep(10 * time.Second) } + +// 十万人群聊测试 +func Test_SendMsgByGroup2(t *testing.T) { + funcation.AllLoginMgr = make(map[string]*funcation.CoreNode) + count := 100000 + var uid []string + for i := 1; i <= count; i++ { + uid = append(uid, fmt.Sprintf("register_test_%v", i)) + } + funcation.LoginBatch(uid) + groupID := "3747979639" + for i := 1; i <= count; i++ { + operationID := utils.OperationIDGenerator() + ctx := ccontext.WithInfo(context.Background(), &ccontext.GlobalConfig{ + UserID: uid[i], + Token: funcation.AllLoginMgr[uid[i]].Token, + }) + ctx = ccontext.WithOperationID(ctx, operationID) + ctx = ccontext.WithSendMessageCallback(ctx, funcation.TestSendMsgCallBack{ + OperationID: operationID, + }) + + time.Sleep(time.Duration(200) * time.Millisecond) + msg := fmt.Sprintf("%v send to %v message by %d ", uid[i], groupID, i) + funcation.SendMsg(ctx, uid[i], "", groupID, msg) + log.ZDebug(ctx, "msg prefix", "msg", msg) + } +} + +// 十万人群聊测试 +func Test_SendMsgByGroup3(t *testing.T) { + funcation.AllLoginMgr = make(map[string]*funcation.CoreNode) + count := 1 + // groupID := "3747979639" + groupID := "3686091035" + for i := 1; i <= count; i++ { + uid := fmt.Sprintf("register_test_%v", i) + funcation.LoginOne(uid) + operationID := utils.OperationIDGenerator() + ctx := ccontext.WithInfo(context.Background(), &ccontext.GlobalConfig{ + UserID: uid, + Token: funcation.AllLoginMgr[uid].Token, + IMConfig: funcation.Config, + }) + ctx = ccontext.WithOperationID(ctx, operationID) + ctx = ccontext.WithSendMessageCallback(ctx, funcation.TestSendMsgCallBack{ + OperationID: operationID, + }) + + funcation.AllLoginMgr[uid].Mgr.Conversation().GetOneConversation(ctx, 2, uid) + + time.Sleep(time.Duration(200) * time.Millisecond) + msg := fmt.Sprintf("%v send to %v message by %d ", uid, groupID, i) + funcation.SendMsg(ctx, uid, "", groupID, msg) + log.ZDebug(ctx, "msg prefix", "msg", msg) + } +} + +// 管理员邀请群成员进行群聊测试 +func Test_SendMsgByGroup_One(t *testing.T) { + funcation.AllLoginMgr = make(map[string]*funcation.CoreNode) + uid := "register_test_1002" + groupID := "226506521" + // groupID := "4238406538" + // groupID := "2592040611" + funcation.LoginOne(uid) + + ctx := getCtx(uid) + time.Sleep(time.Duration(200) * time.Millisecond) + + // 管理员邀请进群 + funcation.AllLoginMgr[uid].Mgr.Group().JoinGroup(ctx, groupID, "", constant.JoinBySearch) + + msg := fmt.Sprintf("%v send to %v message", uid, groupID) + funcation.SendMsg(ctx, uid, "", groupID, msg) + log.ZDebug(ctx, "msg prefix", "msg", msg) +} + +// 管理员邀请大量群成员进行群聊测试 +func Test_SendMsgByGroup_Batch(t *testing.T) { + count := 1000 + + // groupID := "780048154" + // groupID := "3159824577" + groupID := "4269768429" + var uidList []string + for i := 0; i <= count; i++ { + uid := fmt.Sprintf("register_test_%v", i+1) + uidList = append(uidList, uid) + } + // 管理员批量邀请进群 + adminUID := "openIM123456" + funcation.LoginOne(adminUID) + ctx := getCtx(adminUID) + err := funcation.AllLoginMgr[adminUID].Mgr.Group().InviteUserToGroup(ctx, groupID, "", uidList) + if err != nil { + t.Error("invite user fails") + return + } + + for i := 0; i <= count; i++ { + uid := uidList[i] + funcation.LoginOne(uid) + ctx := getCtx(uid) + // time.Sleep(time.Duration(200) * time.Millisecond) + + msg := fmt.Sprintf("%v send to %v message", uid, groupID) + funcation.SendMsg(ctx, uid, "", groupID, msg) + log.ZDebug(ctx, "msg prefix", "msg", msg) + } +} + +func getCtx(uid string) context.Context { + operationID := utils.OperationIDGenerator() + ctx := ccontext.WithInfo(context.Background(), &ccontext.GlobalConfig{ + UserID: uid, + Token: funcation.AllLoginMgr[uid].Token, + IMConfig: funcation.Config, + }) + ctx = ccontext.WithOperationID(ctx, operationID) + ctx = ccontext.WithSendMessageCallback(ctx, funcation.TestSendMsgCallBack{ + OperationID: operationID, + }) + return ctx +} diff --git a/testv3/register_test.go b/testv3/register_test.go index 504df2c10..b32fae275 100644 --- a/testv3/register_test.go +++ b/testv3/register_test.go @@ -1,12 +1,26 @@ +// Copyright © 2023 OpenIM SDK. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + // @Author BanTanger 2023/7/9 15:30:00 package testv3 import ( - "open_im_sdk/pkg/utils" + "fmt" + "open_im_sdk/pkg/log" "open_im_sdk/testv3/funcation" - "strconv" + "sync" "testing" - "time" ) func Test_RegisterOne(t *testing.T) { @@ -24,16 +38,28 @@ func Test_RegisterOne(t *testing.T) { } func Test_RegisterBatch(t *testing.T) { - count := 100 + count := 10000 var users []funcation.Users - for i := 0; i < count; i++ { - users = append(users, funcation.Users{ - Uid: funcation.GenUid(i, "register_test_"+utils.Int64ToString(time.Now().Unix())), - Nickname: "register_test_" + strconv.FormatInt(int64(i), 10), - FaceUrl: "", - }) + var wg sync.WaitGroup + wg.Add(count) + for i := 1; i <= count; i++ { + go func(i int) { + users = append(users, funcation.Users{ + Uid: fmt.Sprintf("register_%d", i), + Nickname: fmt.Sprintf("register_%d", i), + FaceUrl: "", + }) + wg.Done() + }(i) } + wg.Wait() + log.Info("users length", len(users)) success, fail := funcation.RegisterBatch(users) t.Log(success) t.Log(fail) } + +func Test_getToken(t *testing.T) { + token, _ := funcation.GetToken("123456") + t.Log(token) +}