From 7033b603ca07e493cdf3bb28b2a905bd674703e7 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 28 Dec 2022 13:56:07 +0800 Subject: [PATCH 1/9] test cron --- internal/cron_task/clear_msg.go | 4 +-- internal/cron_task/clear_msg_test.go | 44 +++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/internal/cron_task/clear_msg.go b/internal/cron_task/clear_msg.go index f6ea7cd1f..36239c8fb 100644 --- a/internal/cron_task/clear_msg.go +++ b/internal/cron_task/clear_msg.go @@ -114,7 +114,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") } - delStruct.minSeq = lastMsgPb.Seq + delStruct.minSeq = lastMsgPb.Seq + 1 } else { var hasMarkDelFlag bool for index, msg := range msgs.Msg { @@ -124,7 +124,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..678bfe1f4 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,43 @@ 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", 201) + } + + testUID3 := "test_del_id3" + err = SetUserMaxSeq(testUID3, 4999) + userChat = GenUserChat(1, 4999, 5000, 0, testUID3) + 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("test2 is not the same", "minSeq:", minSeq, "targetSeq", 201) } } From 3d9355848b4f92430cbabb9dbaa3bcb570eb75e8 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 28 Dec 2022 14:13:16 +0800 Subject: [PATCH 2/9] test cron --- internal/cron_task/clear_msg_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/cron_task/clear_msg_test.go b/internal/cron_task/clear_msg_test.go index 678bfe1f4..1c730d322 100644 --- a/internal/cron_task/clear_msg_test.go +++ b/internal/cron_task/clear_msg_test.go @@ -122,7 +122,7 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { t.Error("err is not nil", testUID2, err.Error()) } if minSeq != 6001 { - t.Error("test2 is not the same", "minSeq:", minSeq, "targetSeq", 201) + t.Error("test2 is not the same", "minSeq:", minSeq, "targetSeq", 6001) } testUID3 := "test_del_id3" @@ -139,6 +139,6 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { t.Error("err is not nil", testUID3, err.Error()) } if minSeq != 5000 { - t.Error("test2 is not the same", "minSeq:", minSeq, "targetSeq", 201) + t.Error("test3 is not the same", "minSeq:", minSeq, "targetSeq", 201) } } From 5888dc3b5dd86d3ad112be4f910ac6f70ebf92a0 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 28 Dec 2022 14:20:17 +0800 Subject: [PATCH 3/9] test cron --- internal/cron_task/clear_msg_test.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/internal/cron_task/clear_msg_test.go b/internal/cron_task/clear_msg_test.go index 1c730d322..627ba1431 100644 --- a/internal/cron_task/clear_msg_test.go +++ b/internal/cron_task/clear_msg_test.go @@ -128,6 +128,7 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { 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) } @@ -139,6 +140,28 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { t.Error("err is not nil", testUID3, err.Error()) } if minSeq != 5000 { - t.Error("test3 is not the same", "minSeq:", minSeq, "targetSeq", 201) + 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) } } From 7bf7686314eacdcaab7ae3a95f039fec212554de Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 28 Dec 2022 14:54:22 +0800 Subject: [PATCH 4/9] test cron --- internal/cron_task/clear_msg.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/cron_task/clear_msg.go b/internal/cron_task/clear_msg.go index 36239c8fb..a2240396c 100644 --- a/internal/cron_task/clear_msg.go +++ b/internal/cron_task/clear_msg.go @@ -115,6 +115,7 @@ func deleteMongoMsg(operationID string, ID string, index int64, delStruct *delMs return 0, utils.Wrap(err, "proto.Unmarshal failed") } 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 { From 45293152bb8503cbece188c4374d9ab0b8ec6a24 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 28 Dec 2022 15:20:04 +0800 Subject: [PATCH 5/9] test cron --- internal/cron_task/clear_msg.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cron_task/clear_msg.go b/internal/cron_task/clear_msg.go index a2240396c..60812b074 100644 --- a/internal/cron_task/clear_msg.go +++ b/internal/cron_task/clear_msg.go @@ -106,7 +106,7 @@ func deleteMongoMsg(operationID string, ID string, index int64, delStruct *delMs 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) From cdd86349d073a54e9d15e69f1046496c7cb662aa Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 28 Dec 2022 15:27:04 +0800 Subject: [PATCH 6/9] test cron --- internal/cron_task/clear_msg.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cron_task/clear_msg.go b/internal/cron_task/clear_msg.go index 60812b074..5ae3b16c6 100644 --- a/internal/cron_task/clear_msg.go +++ b/internal/cron_task/clear_msg.go @@ -100,7 +100,7 @@ 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() { From 01b2de80ae732c898dbcf61227858526d283bd92 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 28 Dec 2022 15:33:53 +0800 Subject: [PATCH 7/9] test cron --- internal/cron_task/clear_msg_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cron_task/clear_msg_test.go b/internal/cron_task/clear_msg_test.go index 627ba1431..ec63e1abe 100644 --- a/internal/cron_task/clear_msg_test.go +++ b/internal/cron_task/clear_msg_test.go @@ -162,6 +162,6 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { t.Error("err is not nil", testUID4, err.Error()) } if minSeq != 11001 { - t.Error("test3 is not the same", "minSeq:", minSeq, "targetSeq", 11001) + t.Error("test3 is not the same", "minSeq:", minSeq, "targetSeq", 11002) } } From 71a2090363508b344a1daf4ec6223d40e82be4cb Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 28 Dec 2022 15:41:04 +0800 Subject: [PATCH 8/9] test cron --- internal/cron_task/clear_msg_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cron_task/clear_msg_test.go b/internal/cron_task/clear_msg_test.go index ec63e1abe..a68ac7125 100644 --- a/internal/cron_task/clear_msg_test.go +++ b/internal/cron_task/clear_msg_test.go @@ -161,7 +161,7 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { if err != nil { t.Error("err is not nil", testUID4, err.Error()) } - if minSeq != 11001 { + if minSeq != 11002 { t.Error("test3 is not the same", "minSeq:", minSeq, "targetSeq", 11002) } } From 0a5783092510cff952ce13babfc6a7a1e4924ef2 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 28 Dec 2022 15:42:52 +0800 Subject: [PATCH 9/9] test cron --- internal/cron_task/clear_msg_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/cron_task/clear_msg_test.go b/internal/cron_task/clear_msg_test.go index a68ac7125..627ba1431 100644 --- a/internal/cron_task/clear_msg_test.go +++ b/internal/cron_task/clear_msg_test.go @@ -161,7 +161,7 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { if err != nil { t.Error("err is not nil", testUID4, err.Error()) } - if minSeq != 11002 { - t.Error("test3 is not the same", "minSeq:", minSeq, "targetSeq", 11002) + if minSeq != 11001 { + t.Error("test3 is not the same", "minSeq:", minSeq, "targetSeq", 11001) } }