package tools import ( "OpenIM/pkg/common/constant" "OpenIM/pkg/proto/sdkws" "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" "testing" "time" ) func GenUserChat(startSeq, stopSeq, delSeq, index int64, userID string) *mongo2.UserChat { chat := &mongo2.UserChat{UID: userID + strconv.Itoa(int(index))} for i := startSeq; i <= stopSeq; i++ { msg := sdkws.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.com"), Seq: i, SendTime: time.Now().Unix(), CreateTime: time.Now().Unix(), Status: 1, } bytes, _ := proto.Marshal(&msg) sendTime := 0 chat.Msg = append(chat.Msg, mongo2.MsgInfo{SendTime: int64(sendTime), Msg: bytes}) } return chat } func SetUserMaxSeq(userID string, seq int) error { return redisClient.Set(context.Background(), "REDIS_USER_INCR_SEQ"+userID, seq, 0).Err() } func CreateChat(userChat *mongo2.UserChat) error { _, err := mongoClient.InsertOne(context.Background(), userChat) return err } func TestDeleteUserMsgsAndSetMinSeq(t *testing.T) { operationID := getCronTaskOperationID() redisClient = redis.NewClient(&redis.Options{ Addr: "127.0.0.1:16379", Password: "openIM123", // no password set DB: 13, // use default DB }) 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") 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) err = CreateChat(userChat) if err := DeleteUserMsgsAndSetMinSeq(operationID, testUID1); err != nil { t.Error("checkMaxSeqWithMongo failed", testUID1) } if err := checkMaxSeqWithMongo(operationID, testUID1, constant.WriteDiffusion); err != nil { t.Error("checkMaxSeqWithMongo failed", testUID1) } if err != nil { t.Error("err is not nil", testUID1, err.Error()) } }