Files
open-im-server/internal/tools/msg_test.go
T

328 lines
11 KiB
Go
Raw Normal View History

2023-03-03 17:42:26 +08:00
package tools
2022-11-01 16:54:23 +08:00
import (
2022-12-26 10:25:42 +08:00
"context"
2023-04-28 18:33:33 +08:00
"strconv"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
2023-03-21 12:28:21 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2023-03-08 16:35:18 +08:00
"go.mongodb.org/mongo-driver/bson"
2023-04-28 18:33:33 +08:00
"google.golang.org/protobuf/proto"
2022-12-26 10:25:42 +08:00
2023-03-16 10:46:06 +08:00
unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation"
2022-12-26 10:25:42 +08:00
2022-11-01 16:54:23 +08:00
"testing"
2022-11-18 16:59:31 +08:00
"time"
2022-11-01 16:54:23 +08:00
)
2023-05-05 21:30:32 +08:00
func GenMsgDoc(startSeq, stopSeq, delSeq, index int64, conversationID string) *unRelationTb.MsgDocModel {
msgDoc := &unRelationTb.MsgDocModel{DocID: conversationID + strconv.Itoa(int(index))}
2023-05-23 10:49:30 +08:00
for i := 0; i < 5000; i++ {
msgDoc.Msg = append(msgDoc.Msg, unRelationTb.MsgInfoModel{SendTime: 0, Msg: []byte{}})
}
2022-12-26 10:25:42 +08:00
for i := startSeq; i <= stopSeq; i++ {
2023-02-09 20:36:34 +08:00
msg := sdkws.MsgData{
2022-12-26 10:25:42 +08:00
SendID: "sendID1",
RecvID: "recvID1",
GroupID: "",
ClientMsgID: "xxx",
ServerMsgID: "xxx",
SenderPlatformID: 1,
SenderNickname: "testNickName",
SenderFaceURL: "testFaceURL",
SessionType: 1,
MsgFrom: 100,
ContentType: 101,
2023-03-08 16:35:18 +08:00
Content: []byte("testFaceURL"),
2023-03-03 17:42:26 +08:00
Seq: i,
2022-12-26 10:25:42 +08:00
SendTime: time.Now().Unix(),
CreateTime: time.Now().Unix(),
Status: 1,
}
bytes, _ := proto.Marshal(&msg)
2023-03-08 16:35:18 +08:00
var sendTime int64
if i <= delSeq {
sendTime = 10000
} else {
sendTime = utils.GetCurrentTimestampByMill()
}
2023-05-23 10:49:30 +08:00
msgDoc.Msg[i-1] = unRelationTb.MsgInfoModel{SendTime: int64(sendTime), Msg: bytes}
2022-11-18 16:59:31 +08:00
}
2023-03-08 16:35:18 +08:00
return msgDoc
2022-11-18 16:59:31 +08:00
}
2023-03-08 16:35:18 +08:00
func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) {
operationID := "test"
rdb, err := cache.NewRedis()
if err != nil {
return
}
mgo, err := unrelation.NewMongo()
if err != nil {
return
}
2023-05-04 12:11:29 +08:00
cacheModel := cache.NewMsgCacheModel(rdb)
2023-03-08 16:35:18 +08:00
mongoClient := mgo.GetDatabase().Collection(unRelationTb.MsgDocModel{}.TableName())
ctx := context.Background()
2023-03-23 10:39:03 +08:00
ctx = mcontext.SetOperationID(ctx, operationID)
2023-05-05 21:30:32 +08:00
2022-11-18 16:59:31 +08:00
testUID1 := "test_del_id1"
2023-05-05 21:30:32 +08:00
var conversationID string
conversationID = utils.GetConversationIDBySessionType(constant.SuperGroupChatType, testUID1)
_, err = mongoClient.DeleteOne(ctx, bson.M{"doc_id": conversationID + ":" + strconv.Itoa(0)})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("DeleteOne failed")
return
}
2023-05-05 21:30:32 +08:00
err = cacheModel.SetMaxSeq(ctx, conversationID, 600)
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("SetUserMaxSeq failed")
2022-11-18 18:16:44 +08:00
}
2023-05-05 21:30:32 +08:00
msgDoc := GenMsgDoc(1, 600, 200, 0, conversationID)
2023-03-08 16:35:18 +08:00
if _, err := mongoClient.InsertOne(ctx, msgDoc); err != nil {
2023-05-05 21:30:32 +08:00
t.Error("InsertOne failed", conversationID)
2023-03-08 16:35:18 +08:00
}
msgTools, err := InitMsgTool()
if err != nil {
t.Error("init failed")
return
}
2023-05-18 16:22:59 +08:00
msgTools.ClearConversationsMsg(ctx, []string{conversationID})
2023-05-05 21:30:32 +08:00
minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err := msgTools.msgDatabase.GetConversationMinMaxSeqInMongoAndCache(ctx, conversationID)
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed")
return
}
2023-05-18 16:22:59 +08:00
if maxSeqCache != maxSeqMongo {
2023-05-05 21:30:32 +08:00
t.Error("checkMaxSeqWithMongo failed", conversationID)
2022-11-01 16:54:23 +08:00
}
2023-03-08 16:35:18 +08:00
if minSeqMongo != minSeqCache {
t.Error("minSeqMongo != minSeqCache", minSeqMongo, minSeqCache)
}
if minSeqCache != 201 {
t.Error("test1 is not the same", "minSeq:", minSeqCache, "targetSeq", 201)
}
/////// uid2
testUID2 := "test_del_id2"
2023-05-05 21:30:32 +08:00
conversationID = utils.GetConversationIDBySessionType(constant.SuperGroupChatType, testUID2)
_, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(0)})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("delete failed")
}
2023-05-05 21:30:32 +08:00
_, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(1)})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("delete failed")
}
2023-05-05 21:30:32 +08:00
err = cacheModel.SetMaxSeq(ctx, conversationID, 7000)
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("SetUserMaxSeq failed")
}
2023-05-05 21:30:32 +08:00
msgDoc = GenMsgDoc(1, 4999, 5000, 0, conversationID)
msgDoc2 := GenMsgDoc(5000, 7000, 6000, 1, conversationID)
2023-03-08 16:35:18 +08:00
if _, err := mongoClient.InsertOne(ctx, msgDoc); err != nil {
t.Error("InsertOne failed", testUID1)
}
if _, err := mongoClient.InsertOne(ctx, msgDoc2); err != nil {
t.Error("InsertOne failed", testUID1)
}
2023-05-18 16:22:59 +08:00
msgTools.ClearConversationsMsg(ctx, []string{conversationID})
2023-05-05 21:30:32 +08:00
minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err = msgTools.msgDatabase.GetConversationMinMaxSeqInMongoAndCache(ctx, conversationID)
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed")
return
}
2023-05-18 16:22:59 +08:00
if maxSeqCache != maxSeqMongo {
2023-05-05 21:30:32 +08:00
t.Error("checkMaxSeqWithMongo failed", conversationID)
2023-03-08 16:35:18 +08:00
}
if minSeqMongo != minSeqCache {
t.Error("minSeqMongo != minSeqCache", minSeqMongo, minSeqCache)
}
if minSeqCache != 6001 {
t.Error("test1 is not the same", "minSeq:", minSeqCache, "targetSeq", 201)
}
/////// uid3
testUID3 := "test_del_id3"
2023-05-05 21:30:32 +08:00
conversationID = utils.GetConversationIDBySessionType(constant.SuperGroupChatType, testUID3)
_, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(0)})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("delete failed")
}
2023-05-05 21:30:32 +08:00
err = cacheModel.SetMaxSeq(ctx, conversationID, 4999)
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("SetUserMaxSeq failed")
}
2023-05-05 21:30:32 +08:00
msgDoc = GenMsgDoc(1, 4999, 5000, 0, conversationID)
2023-03-08 16:35:18 +08:00
if _, err := mongoClient.InsertOne(ctx, msgDoc); err != nil {
2023-05-05 21:30:32 +08:00
t.Error("InsertOne failed", conversationID)
2023-03-08 16:35:18 +08:00
}
2023-05-18 16:22:59 +08:00
msgTools.ClearConversationsMsg(ctx, []string{conversationID})
2023-05-05 21:30:32 +08:00
minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err = msgTools.msgDatabase.GetConversationMinMaxSeqInMongoAndCache(ctx, conversationID)
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed")
return
}
2023-05-18 16:22:59 +08:00
if maxSeqCache != maxSeqMongo {
2023-05-05 21:30:32 +08:00
t.Error("checkMaxSeqWithMongo failed", conversationID)
2023-03-08 16:35:18 +08:00
}
if minSeqMongo != minSeqCache {
t.Error("minSeqMongo != minSeqCache", minSeqMongo, minSeqCache)
}
if minSeqCache != 5000 {
t.Error("test1 is not the same", "minSeq:", minSeqCache, "targetSeq", 201)
}
//// uid4
testUID4 := "test_del_id4"
2023-05-05 21:30:32 +08:00
conversationID = utils.GetConversationIDBySessionType(constant.SuperGroupChatType, testUID4)
_, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(0)})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("delete failed")
}
2023-05-05 21:30:32 +08:00
_, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(1)})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("delete failed")
}
2023-05-05 21:30:32 +08:00
_, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(2)})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("delete failed")
}
2023-05-05 21:30:32 +08:00
err = cacheModel.SetMaxSeq(ctx, conversationID, 12000)
msgDoc = GenMsgDoc(1, 4999, 5000, 0, conversationID)
msgDoc2 = GenMsgDoc(5000, 9999, 10000, 1, conversationID)
msgDoc3 := GenMsgDoc(10000, 12000, 11000, 2, conversationID)
2023-03-08 16:35:18 +08:00
if _, err := mongoClient.InsertOne(ctx, msgDoc); err != nil {
2023-05-05 21:30:32 +08:00
t.Error("InsertOne failed", conversationID)
2023-03-08 16:35:18 +08:00
}
if _, err := mongoClient.InsertOne(ctx, msgDoc2); err != nil {
2023-05-05 21:30:32 +08:00
t.Error("InsertOne failed", conversationID)
2023-03-08 16:35:18 +08:00
}
if _, err := mongoClient.InsertOne(ctx, msgDoc3); err != nil {
2023-05-05 21:30:32 +08:00
t.Error("InsertOne failed", conversationID)
2023-03-08 16:35:18 +08:00
}
2023-05-18 16:22:59 +08:00
msgTools.ClearConversationsMsg(ctx, []string{conversationID})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed")
return
}
2023-05-05 21:30:32 +08:00
minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err = msgTools.msgDatabase.GetConversationMinMaxSeqInMongoAndCache(ctx, conversationID)
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed")
return
}
2023-05-18 16:22:59 +08:00
if maxSeqCache != maxSeqMongo {
2023-05-05 21:30:32 +08:00
t.Error("checkMaxSeqWithMongo failed", conversationID)
2023-03-08 16:35:18 +08:00
}
if minSeqMongo != minSeqCache {
t.Error("minSeqMongo != minSeqCache", minSeqMongo, minSeqCache)
}
if minSeqCache != 5000 {
t.Error("test1 is not the same", "minSeq:", minSeqCache)
}
testUID5 := "test_del_id5"
2023-05-05 21:30:32 +08:00
conversationID = utils.GetConversationIDBySessionType(constant.SuperGroupChatType, testUID5)
_, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(0)})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("delete failed")
}
2023-05-05 21:30:32 +08:00
_, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(1)})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("delete failed")
}
2023-05-05 21:30:32 +08:00
err = cacheModel.SetMaxSeq(ctx, conversationID, 9999)
msgDoc = GenMsgDoc(1, 4999, 5000, 0, conversationID)
msgDoc2 = GenMsgDoc(5000, 9999, 10000, 1, conversationID)
2023-03-08 16:35:18 +08:00
if _, err := mongoClient.InsertOne(ctx, msgDoc); err != nil {
2023-05-05 21:30:32 +08:00
t.Error("InsertOne failed", conversationID)
2023-03-08 16:35:18 +08:00
}
if _, err := mongoClient.InsertOne(ctx, msgDoc2); err != nil {
2023-05-05 21:30:32 +08:00
t.Error("InsertOne failed", conversationID)
2023-03-08 16:35:18 +08:00
}
2023-05-18 16:22:59 +08:00
msgTools.ClearConversationsMsg(ctx, []string{conversationID})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed")
return
}
2023-05-05 21:30:32 +08:00
minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err = msgTools.msgDatabase.GetConversationMinMaxSeqInMongoAndCache(ctx, conversationID)
2022-12-26 10:25:42 +08:00
if err != nil {
2023-03-08 16:35:18 +08:00
t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed")
return
}
2023-05-18 16:22:59 +08:00
if maxSeqCache != maxSeqMongo {
2023-05-05 21:30:32 +08:00
t.Error("checkMaxSeqWithMongo failed", conversationID)
2023-03-08 16:35:18 +08:00
}
if minSeqMongo != minSeqCache {
t.Error("minSeqMongo != minSeqCache", minSeqMongo, minSeqCache)
}
if minSeqCache != 10000 {
t.Error("test1 is not the same", "minSeq:", minSeqCache)
}
testUID6 := "test_del_id6"
2023-05-05 21:30:32 +08:00
conversationID = utils.GetConversationIDBySessionType(constant.SuperGroupChatType, testUID6)
_, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(0)})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("delete failed")
}
2023-05-05 21:30:32 +08:00
_, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(1)})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("delete failed")
}
2023-05-05 21:30:32 +08:00
_, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(2)})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("delete failed")
}
2023-05-05 21:30:32 +08:00
_, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(3)})
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("delete failed")
}
2023-05-05 21:30:32 +08:00
msgDoc = GenMsgDoc(1, 4999, 5000, 0, conversationID)
msgDoc2 = GenMsgDoc(5000, 9999, 10000, 1, conversationID)
msgDoc3 = GenMsgDoc(10000, 14999, 13000, 2, conversationID)
msgDoc4 := GenMsgDoc(15000, 19999, 0, 3, conversationID)
2023-03-08 16:35:18 +08:00
if _, err := mongoClient.InsertOne(ctx, msgDoc); err != nil {
t.Error("InsertOne failed", testUID4)
}
if _, err := mongoClient.InsertOne(ctx, msgDoc2); err != nil {
t.Error("InsertOne failed", testUID4)
}
if _, err := mongoClient.InsertOne(ctx, msgDoc3); err != nil {
t.Error("InsertOne failed", testUID4)
}
if _, err := mongoClient.InsertOne(ctx, msgDoc4); err != nil {
t.Error("InsertOne failed", testUID4)
}
2023-05-05 21:30:32 +08:00
minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err = msgTools.msgDatabase.GetConversationMinMaxSeqInMongoAndCache(ctx, conversationID)
2023-03-08 16:35:18 +08:00
if err != nil {
t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed")
return
}
2023-05-18 16:22:59 +08:00
if maxSeqCache != maxSeqMongo {
2023-05-05 21:30:32 +08:00
t.Error("checkMaxSeqWithMongo failed", conversationID)
2023-03-08 16:35:18 +08:00
}
if minSeqMongo != minSeqCache {
t.Error("minSeqMongo != minSeqCache", minSeqMongo, minSeqCache)
}
if minSeqCache != 13001 {
t.Error("test1 is not the same", "minSeq:", minSeqCache)
2022-11-01 16:54:23 +08:00
}
}