package controller import ( "context" "fmt" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation" "go.mongodb.org/mongo-driver/bson" "math/rand" "strconv" "sync" "testing" "time" ) func Test_BatchInsertChat2DB(t *testing.T) { config.Config.Mongo.DBAddress = []string{"192.168.44.128:37017"} config.Config.Mongo.DBTimeout = 60 config.Config.Mongo.DBDatabase = "openIM" config.Config.Mongo.DBSource = "admin" config.Config.Mongo.DBUserName = "root" config.Config.Mongo.DBPassword = "openIM123" config.Config.Mongo.DBMaxPoolSize = 100 config.Config.Mongo.DBRetainChatRecords = 3650 config.Config.Mongo.ChatRecordsClearTime = "0 2 * * 3" mongo, err := unrelation.NewMongo() if err != nil { t.Fatal(err) } err = mongo.GetDatabase().Client().Ping(context.Background(), nil) if err != nil { panic(err) } db := &commonMsgDatabase{ msgDocDatabase: unrelation.NewMsgMongoDriver(mongo.GetDatabase()), } //ctx := context.Background() //msgs := make([]*sdkws.MsgData, 0, 1) //for i := 0; i < cap(msgs); i++ { // msgs = append(msgs, &sdkws.MsgData{ // Content: []byte(fmt.Sprintf("test-%d", i)), // SendTime: time.Now().UnixMilli(), // }) //} //err = db.BatchInsertChat2DB(ctx, "test", msgs, 0) //if err != nil { // panic(err) //} _ = db.BatchInsertChat2DB c := mongo.GetDatabase().Collection("msg") ch := make(chan int) rand.Seed(time.Now().UnixNano()) index := 10 var wg sync.WaitGroup for i := 0; i < 1000; i++ { wg.Add(1) go func(channelID int) { defer wg.Done() <-ch var arr []string for i := 0; i < 500; i++ { arr = append(arr, strconv.Itoa(i+1)) } rand.Shuffle(len(arr), func(i, j int) { arr[i], arr[j] = arr[j], arr[i] }) for j, s := range arr { if j == 0 { fmt.Printf("channnelID: %d, arr[0]: %s\n", channelID, arr[j]) } filter := bson.M{"doc_id": "test:0"} update := bson.M{ "$addToSet": bson.M{ fmt.Sprintf("msgs.%d.del_list", index): bson.M{"$each": []string{s}}, }, } _, err := c.UpdateOne(context.Background(), filter, update) if err != nil { t.Fatal(err) } } }(i) } for i := 0; i < 1000; i++ { wg.Add(1) go func() { defer wg.Done() <-ch var arr []string for i := 0; i < 500; i++ { arr = append(arr, strconv.Itoa(1001+i)) } rand.Shuffle(len(arr), func(i, j int) { arr[i], arr[j] = arr[j], arr[i] }) for _, s := range arr { filter := bson.M{"doc_id": "test:0"} update := bson.M{ "$addToSet": bson.M{ fmt.Sprintf("msgs.%d.read_list", index): bson.M{"$each": []string{s}}, }, } _, err := c.UpdateOne(context.Background(), filter, update) if err != nil { t.Fatal(err) } } }() } time.Sleep(time.Second * 2) close(ch) wg.Wait() } func GetDB() *commonMsgDatabase { config.Config.Mongo.DBAddress = []string{"192.168.44.128:37017"} config.Config.Mongo.DBTimeout = 60 config.Config.Mongo.DBDatabase = "openIM" config.Config.Mongo.DBSource = "admin" config.Config.Mongo.DBUserName = "root" config.Config.Mongo.DBPassword = "openIM123" config.Config.Mongo.DBMaxPoolSize = 100 config.Config.Mongo.DBRetainChatRecords = 3650 config.Config.Mongo.ChatRecordsClearTime = "0 2 * * 3" mongo, err := unrelation.NewMongo() if err != nil { panic(err) } err = mongo.GetDatabase().Client().Ping(context.Background(), nil) if err != nil { panic(err) } return &commonMsgDatabase{ msgDocDatabase: unrelation.NewMsgMongoDriver(mongo.GetDatabase()), } } func Test_Insert(t *testing.T) { db := GetDB() ctx := context.Background() var arr []*unRelationTb.MsgInfoModel for i := 0; i < 345; i++ { arr = append(arr, &unRelationTb.MsgInfoModel{ Msg: &unRelationTb.MsgDataModel{ Seq: int64(i), Content: fmt.Sprintf("test-%d", i), }, }) } if err := db.BatchInsertBlock(ctx, "test", arr, 0); err != nil { t.Fatal(err) } } func Test_Revoke(t *testing.T) { db := GetDB() ctx := context.Background() var arr []*unRelationTb.MsgInfoModel for i := 0; i < 456; i++ { arr = append(arr, &unRelationTb.MsgInfoModel{ Revoke: &unRelationTb.RevokeModel{ UserID: "uid_" + strconv.Itoa(i), Nickname: "uname_" + strconv.Itoa(i), Time: time.Now().UnixMilli(), }, }) } if err := db.BatchInsertBlock(ctx, "test", arr, 123); err != nil { t.Fatal(err) } } func Test_Delete(t *testing.T) { db := GetDB() ctx := context.Background() var arr []*unRelationTb.MsgInfoModel for i := 0; i < 123; i++ { arr = append(arr, &unRelationTb.MsgInfoModel{ DelList: []string{"uid_1", "uid_2"}, }) } if err := db.BatchInsertBlock(ctx, "test", arr, 210); err != nil { t.Fatal(err) } } func Test_Delete1(t *testing.T) { config.Config.Mongo.DBAddress = []string{"192.168.44.128:37017"} config.Config.Mongo.DBTimeout = 60 config.Config.Mongo.DBDatabase = "openIM" config.Config.Mongo.DBSource = "admin" config.Config.Mongo.DBUserName = "root" config.Config.Mongo.DBPassword = "openIM123" config.Config.Mongo.DBMaxPoolSize = 100 config.Config.Mongo.DBRetainChatRecords = 3650 config.Config.Mongo.ChatRecordsClearTime = "0 2 * * 3" mongo, err := unrelation.NewMongo() if err != nil { panic(err) } err = mongo.GetDatabase().Client().Ping(context.Background(), nil) if err != nil { panic(err) } c := mongo.GetClient().Database("openIM").Collection("msg") var o unRelationTb.MsgDocModel err = c.FindOne(context.Background(), bson.M{"doc_id": "test:0"}).Decode(&o) if err != nil { panic(err) } for i, model := range o.Msg { fmt.Println(i, model == nil) } }