diff --git a/internal/cron_task/clear_msg.go b/internal/cron_task/clear_msg.go index f6ea7cd1f..5ae3b16c6 100644 --- a/internal/cron_task/clear_msg.go +++ b/internal/cron_task/clear_msg.go @@ -100,13 +100,13 @@ func deleteMongoMsg(operationID string, ID string, index int64, delStruct *delMs if err != nil { return 0, err } - return delStruct.getSetMinSeq() + 1, nil + return delStruct.getSetMinSeq(), nil } log.NewDebug(operationID, "ID:", ID, "index:", index, "uid:", msgs.UID, "len:", len(msgs.Msg)) if len(msgs.Msg) > db.GetSingleGocMsgNum() { log.NewWarn(operationID, utils.GetSelfFuncName(), "msgs too large", len(msgs.Msg), msgs.UID) } - if msgs.Msg[len(msgs.Msg)-1].SendTime+(int64(config.Config.Mongo.DBRetainChatRecords)*24*60*60*1000) > utils.GetCurrentTimestampByMill() && msgListIsFull(msgs) { + if msgs.Msg[len(msgs.Msg)-1].SendTime+(int64(config.Config.Mongo.DBRetainChatRecords)*24*60*60*1000) < utils.GetCurrentTimestampByMill() && msgListIsFull(msgs) { delStruct.delUidList = append(delStruct.delUidList, msgs.UID) lastMsgPb := &server_api_params.MsgData{} err = proto.Unmarshal(msgs.Msg[len(msgs.Msg)-1].Msg, lastMsgPb) @@ -114,7 +114,8 @@ func deleteMongoMsg(operationID string, ID string, index int64, delStruct *delMs log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), len(msgs.Msg)-1, msgs.UID) return 0, utils.Wrap(err, "proto.Unmarshal failed") } - delStruct.minSeq = lastMsgPb.Seq + delStruct.minSeq = lastMsgPb.Seq + 1 + log.NewDebug(operationID, utils.GetSelfFuncName(), msgs.UID, "add to delUidList", "minSeq", lastMsgPb.Seq+1) } else { var hasMarkDelFlag bool for index, msg := range msgs.Msg { @@ -124,7 +125,7 @@ func deleteMongoMsg(operationID string, ID string, index int64, delStruct *delMs log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), len(msgs.Msg)-1, msgs.UID) return 0, utils.Wrap(err, "proto.Unmarshal failed") } - if utils.GetCurrentTimestampByMill() > msg.SendTime+(int64(config.Config.Mongo.DBRetainChatRecords)*24*60*60*1000) && msg.SendTime != 0 { + if utils.GetCurrentTimestampByMill() > msg.SendTime+(int64(config.Config.Mongo.DBRetainChatRecords)*24*60*60*1000) { msgPb.Status = constant.MsgDeleted bytes, _ := proto.Marshal(msgPb) msgs.Msg[index].Msg = bytes diff --git a/internal/cron_task/clear_msg_test.go b/internal/cron_task/clear_msg_test.go index 87d735438..627ba1431 100644 --- a/internal/cron_task/clear_msg_test.go +++ b/internal/cron_task/clear_msg_test.go @@ -84,13 +84,12 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(mongoUri)) mongoClient = client.Database("openIM").Collection("msg") testUID1 := "test_del_id1" - //testUID2 := "test_del_id2" - //testUID3 := "test_del_id3" + //testUID4 := "test_del_id4" //testUID5 := "test_del_id5" //testUID6 := "test_del_id6" err = SetUserMaxSeq(testUID1, 600) - userChat := GenUserChat(1, 500, 200, 0, testUID1) + userChat := GenUserChat(1, 600, 200, 0, testUID1) err = CreateChat(userChat) if err := DeleteMongoMsgAndResetRedisSeq(operationID, testUID1); err != nil { t.Error("checkMaxSeqWithMongo failed", testUID1) @@ -103,6 +102,66 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { t.Error("err is not nil", testUID1, err.Error()) } if minSeq != 201 { - t.Error("is not the same", "minSeq:", minSeq, "targetSeq", 201) + t.Error("test1 is not the same", "minSeq:", minSeq, "targetSeq", 201) + } + + testUID2 := "test_del_id2" + err = SetUserMaxSeq(testUID2, 7000) + userChat = GenUserChat(1, 4999, 5000, 0, testUID2) + userChat2 := GenUserChat(5000, 7000, 6000, 1, testUID2) + err = CreateChat(userChat) + err = CreateChat(userChat2) + if err := DeleteMongoMsgAndResetRedisSeq(operationID, testUID2); err != nil { + t.Error("checkMaxSeqWithMongo failed", testUID2) + } + if err := checkMaxSeqWithMongo(operationID, testUID2, constant.WriteDiffusion); err != nil { + t.Error("checkMaxSeqWithMongo failed", testUID2) + } + minSeq, err = GetUserMinSeq(testUID2) + if err != nil { + t.Error("err is not nil", testUID2, err.Error()) + } + if minSeq != 6001 { + t.Error("test2 is not the same", "minSeq:", minSeq, "targetSeq", 6001) + } + + testUID3 := "test_del_id3" + err = SetUserMaxSeq(testUID3, 4999) + userChat = GenUserChat(1, 4999, 5000, 0, testUID3) + err = CreateChat(userChat) + if err := DeleteMongoMsgAndResetRedisSeq(operationID, testUID3); err != nil { + t.Error("checkMaxSeqWithMongo failed", testUID3) + } + if err := checkMaxSeqWithMongo(operationID, testUID3, constant.WriteDiffusion); err != nil { + t.Error("checkMaxSeqWithMongo failed", testUID3) + } + minSeq, err = GetUserMinSeq(testUID3) + if err != nil { + t.Error("err is not nil", testUID3, err.Error()) + } + if minSeq != 5000 { + t.Error("test3 is not the same", "minSeq:", minSeq, "targetSeq", 5000) + } + + testUID4 := "test_del_id4" + err = SetUserMaxSeq(testUID4, 12000) + userChat = GenUserChat(1, 4999, 5000, 0, testUID4) + userChat2 = GenUserChat(5000, 9999, 10000, 1, testUID4) + userChat3 := GenUserChat(10000, 12000, 11000, 2, testUID4) + err = CreateChat(userChat) + err = CreateChat(userChat2) + err = CreateChat(userChat3) + if err := DeleteMongoMsgAndResetRedisSeq(operationID, testUID4); err != nil { + t.Error("checkMaxSeqWithMongo failed", testUID4) + } + if err := checkMaxSeqWithMongo(operationID, testUID4, constant.WriteDiffusion); err != nil { + t.Error("checkMaxSeqWithMongo failed", testUID4) + } + minSeq, err = GetUserMinSeq(testUID4) + if err != nil { + t.Error("err is not nil", testUID4, err.Error()) + } + if minSeq != 11001 { + t.Error("test3 is not the same", "minSeq:", minSeq, "targetSeq", 11001) } }