Files
open-im-server/internal/cron_task/clear_msg_test.go
T

109 lines
3.1 KiB
Go
Raw Normal View History

2022-11-01 16:54:23 +08:00
package cronTask
import (
"Open_IM/pkg/common/constant"
2022-11-18 16:59:31 +08:00
"Open_IM/pkg/common/db"
server_api_params "Open_IM/pkg/proto/sdk_ws"
2022-12-27 22:27:44 +08:00
"Open_IM/pkg/utils"
2022-12-26 10:25:42 +08:00
"context"
"fmt"
"strconv"
"github.com/go-redis/redis/v8"
"github.com/golang/protobuf/proto"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
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
)
2022-12-26 10:25:42 +08:00
var (
redisClient *redis.Client
mongoClient *mongo.Collection
)
func GenUserChat(startSeq, stopSeq, delSeq, index uint32, userID string) *db.UserChat {
2022-12-28 10:55:00 +08:00
chat := &db.UserChat{UID: userID + ":" + strconv.Itoa(int(index))}
for i := startSeq; i <= stopSeq; i++ {
2022-12-26 10:25:42 +08:00
msg := server_api_params.MsgData{
SendID: "sendID1",
RecvID: "recvID1",
GroupID: "",
ClientMsgID: "xxx",
ServerMsgID: "xxx",
SenderPlatformID: 1,
SenderNickname: "testNickName",
SenderFaceURL: "testFaceURL",
SessionType: 1,
MsgFrom: 100,
ContentType: 101,
Content: []byte("testFaceURL"),
Seq: uint32(i),
SendTime: time.Now().Unix(),
CreateTime: time.Now().Unix(),
Status: 1,
}
bytes, _ := proto.Marshal(&msg)
2022-12-27 22:27:44 +08:00
var sendTime int64
if i <= delSeq {
sendTime = 10000
} else {
sendTime = utils.GetCurrentTimestampByMill()
}
2022-12-26 10:25:42 +08:00
chat.Msg = append(chat.Msg, db.MsgInfo{SendTime: int64(sendTime), Msg: bytes})
2022-11-18 16:59:31 +08:00
}
2022-12-26 10:25:42 +08:00
return chat
2022-11-18 16:59:31 +08:00
}
2022-12-26 10:25:42 +08:00
func SetUserMaxSeq(userID string, seq int) error {
return redisClient.Set(context.Background(), "REDIS_USER_INCR_SEQ"+userID, seq, 0).Err()
}
2022-12-27 22:27:44 +08:00
func GetUserMinSeq(userID string) (uint64, error) {
key := "REDIS_USER_MIN_SEQ:" + userID
seq, err := redisClient.Get(context.Background(), key).Result()
return uint64(utils.StringToInt(seq)), err
}
2022-12-26 10:25:42 +08:00
func CreateChat(userChat *db.UserChat) error {
_, err := mongoClient.InsertOne(context.Background(), userChat)
return err
}
2022-11-18 18:09:43 +08:00
2022-12-26 10:25:42 +08:00
func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) {
2022-11-01 16:54:23 +08:00
operationID := getCronTaskOperationID()
2022-12-26 10:25:42 +08:00
redisClient = redis.NewClient(&redis.Options{
Addr: "127.0.0.1:16379",
Password: "openIM123", // no password set
2022-12-28 10:58:30 +08:00
DB: 0, // use default DB
2022-12-26 10:25:42 +08:00
})
mongoUri := fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
"root", "openIM123", "127.0.0.1:37017",
"openIM", 100)
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(mongoUri))
mongoClient = client.Database("openIM").Collection("msg")
2022-11-18 16:59:31 +08:00
testUID1 := "test_del_id1"
//testUID2 := "test_del_id2"
//testUID3 := "test_del_id3"
//testUID4 := "test_del_id4"
//testUID5 := "test_del_id5"
//testUID6 := "test_del_id6"
2022-12-26 10:25:42 +08:00
err = SetUserMaxSeq(testUID1, 600)
userChat := GenUserChat(1, 500, 200, 0, testUID1)
err = CreateChat(userChat)
if err := DeleteMongoMsgAndResetRedisSeq(operationID, testUID1); err != nil {
t.Error("checkMaxSeqWithMongo failed", testUID1)
2022-11-18 18:16:44 +08:00
}
2022-12-26 10:25:42 +08:00
if err := checkMaxSeqWithMongo(operationID, testUID1, constant.WriteDiffusion); err != nil {
t.Error("checkMaxSeqWithMongo failed", testUID1)
2022-11-01 16:54:23 +08:00
}
2022-12-27 22:27:44 +08:00
minSeq, err := GetUserMinSeq(testUID1)
2022-12-26 10:25:42 +08:00
if err != nil {
t.Error("err is not nil", testUID1, err.Error())
2022-11-01 16:54:23 +08:00
}
2022-12-27 22:27:44 +08:00
if minSeq != 201 {
2022-12-28 11:03:12 +08:00
t.Error("is not the same", "minSeq:", minSeq, "targetSeq", 201)
2022-12-27 22:27:44 +08:00
}
2022-11-01 16:54:23 +08:00
}