From 78ee56dedab0cf931d9aacecb19695b54caad682 Mon Sep 17 00:00:00 2001 From: zhaocy Date: Fri, 1 Jul 2022 18:49:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=B1=9E=E6=80=A7key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/robot/friend.go | 10 ++-- cmd/robot/hero.go | 2 +- cmd/robot/job.go | 26 ---------- cmd/robot/linkcase.go | 95 ++++++++++++++++++++++++++++++++++++ cmd/robot/linkcase_test.go | 32 ++++++++++++ cmd/robot/robot.go | 29 +++++++---- cmd/robot/user.go | 8 +-- modules/friend/api_list.go | 2 +- modules/friend/api_search.go | 4 +- modules/hero/model_hero.go | 22 +++++++-- 10 files changed, 179 insertions(+), 51 deletions(-) delete mode 100644 cmd/robot/job.go create mode 100644 cmd/robot/linkcase.go create mode 100644 cmd/robot/linkcase_test.go diff --git a/cmd/robot/friend.go b/cmd/robot/friend.go index 47232d460..5c57ef68c 100644 --- a/cmd/robot/friend.go +++ b/cmd/robot/friend.go @@ -11,11 +11,12 @@ var ( friendBuilders = []*TestCase{ { //list + Desc: "好友列表", mainType: string(comm.ModuleFriend), subType: friend.FriendSubTypeList, req: &pb.FriendListReq{}, rsp: &pb.FriendListRsp{}, - // enabled: true, + enabled: true, }, { //blacklist mainType: string(comm.ModuleFriend), @@ -29,7 +30,8 @@ var ( req: &pb.FriendSearchReq{ NickName: "", //设置测试参数 }, - rsp: &pb.FriendSearchRsp{}, + rsp: &pb.FriendSearchRsp{}, + enabled: true, }, { //apply mainType: string(comm.ModuleFriend), @@ -62,8 +64,8 @@ var ( req: &pb.FriendBlackAddReq{ FriendId: "0_62be9f40f67327fb53039b70", }, - rsp: &pb.FriendBlackAddRsp{}, - enabled: true, + rsp: &pb.FriendBlackAddRsp{}, + // enabled: true, }, { //delblack mainType: string(comm.ModuleFriend), diff --git a/cmd/robot/hero.go b/cmd/robot/hero.go index 0d9a77c36..e131373af 100644 --- a/cmd/robot/hero.go +++ b/cmd/robot/hero.go @@ -13,7 +13,7 @@ var ( //hero heroBuilders = []*TestCase{ { - desc: "英雄列表", + Desc: "英雄列表", mainType: string(comm.ModuleHero), subType: hero.HeroSubTypeList, req: &pb.HeroListReq{}, diff --git a/cmd/robot/job.go b/cmd/robot/job.go deleted file mode 100644 index 52c857c25..000000000 --- a/cmd/robot/job.go +++ /dev/null @@ -1,26 +0,0 @@ -package robot - -type CaseNode struct { - testCase TestCase //用例名 - next *CaseNode //测试用例 -} - -type LinkCase struct { - head *CaseNode -} - -func NewLinkCase() *LinkCase { - return &LinkCase{} -} - -func (this *LinkCase) isEmpty() bool { - return this.head == nil -} - -func (this *LinkCase) append(testCase *TestCase) { - newNode := &CaseNode{testCase: *testCase} - if this.isEmpty() { - this.head = newNode - } - -} diff --git a/cmd/robot/linkcase.go b/cmd/robot/linkcase.go new file mode 100644 index 000000000..ef0a3936f --- /dev/null +++ b/cmd/robot/linkcase.go @@ -0,0 +1,95 @@ +package robot + +import ( + "fmt" + "log" +) + +type CaseNode struct { + testCase *TestCase //用例名 + Next *CaseNode //测试用例 +} + +type LinkCase struct { + Head *CaseNode + force bool +} + +func NewLinkCase() *LinkCase { + return &LinkCase{} +} + +func NewDefault(testCast *TestCase) *LinkCase { + return &LinkCase{ + force: true, + Head: &CaseNode{testCase: testCast}, + } +} + +func (this *LinkCase) isEmpty() bool { + return this.Head == nil +} + +func (this *LinkCase) lastNode() *CaseNode { + cur := this.Head + for cur.Next != nil { + cur = cur.Next + } + return cur +} + +//尾部追加 +func (this *LinkCase) Append(testCase *TestCase) { + newNode := &CaseNode{testCase: testCase} + if this.isEmpty() { + this.Head = newNode + } else { + this.lastNode().Next = newNode + } +} + +//头部添加 +func (this *LinkCase) Unshift(testCase *TestCase) { + if this.force { + log.Fatal("use `NewLinkCase` method create link if you want to use `Unshift` opt") + return + } + newNode := &CaseNode{testCase: testCase} + newNode.Next = this.Head + this.Head = newNode +} + +func (this *LinkCase) length() int { + cur := this.Head + count := 0 + for cur.Next != nil { + cur = cur.Next + count++ + } + return count +} + +func (this *LinkCase) Insert(index int, testCase *TestCase) { + if this.force { + log.Fatal("use `NewLinkCase` method create link if you want to use `Insert` opt") + return + } + if index < 0 { + this.Unshift(testCase) + } else if index > this.length() { + this.Append(testCase) + } else { + preNode := this.Head + count := 0 + for count < index-1 { + preNode = preNode.Next + count++ + } + newNode := &CaseNode{testCase: testCase, Next: preNode.Next} + preNode.Next = newNode + } +} + +func (this *CaseNode) String() string { + return fmt.Sprintf("value=%v", this.testCase.Desc) +} diff --git a/cmd/robot/linkcase_test.go b/cmd/robot/linkcase_test.go new file mode 100644 index 000000000..fc1a98950 --- /dev/null +++ b/cmd/robot/linkcase_test.go @@ -0,0 +1,32 @@ +package robot_test + +import ( + "fmt" + "go_dreamfactory/cmd/robot" + "sync" + "testing" +) + +func TestJob(t *testing.T) { + // link := robot.NewLinkCase() + link := robot.NewDefault(&robot.TestCase{Desc: "login"}) + link.Append(&robot.TestCase{Desc: "aa"}) + // link.Append(&robot.TestCase{Desc: "bb"}) + + // link.Unshift(&robot.TestCase{Desc: "cc"}) + // link.Unshift(&robot.TestCase{Desc: "dd"}) + + // link.Insert(1, &robot.TestCase{Desc: "ee"}) + cur := link.Head + for cur != nil { + fmt.Println(cur) + cur = cur.Next + } +} + +func TestGJo(t *testing.T) { + var wg sync.WaitGroup + + wg.Add(1) + +} diff --git a/cmd/robot/robot.go b/cmd/robot/robot.go index c1790faed..a3a54be59 100644 --- a/cmd/robot/robot.go +++ b/cmd/robot/robot.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "log" "net/http" + "sync" "time" "github.com/gorilla/websocket" @@ -20,7 +21,10 @@ type Robot struct { ws *websocket.Conn opts *Options user *pb.DBUser - builders []*TestCase + builders []*TestCase //测试用例 + + linkCase *LinkCase //测试用例链,适应于用例跑批 + wg sync.WaitGroup } func NewRobot(opts *Options) *Robot { @@ -29,8 +33,9 @@ func NewRobot(opts *Options) *Robot { log.Fatal(err) } r := &Robot{ - ws: ws, - opts: opts, + ws: ws, + opts: opts, + linkCase: NewLinkCase(), } return r @@ -50,7 +55,7 @@ func (r *Robot) Run() { //处理响应 go func() { - for { + for { var msg *pb.UserMessage = &pb.UserMessage{} _, data, err := r.ws.ReadMessage() if err != nil { @@ -64,11 +69,12 @@ func (r *Robot) Run() { } }() - select {} + // select {} + r.wg.Wait() } type TestCase struct { - desc string + Desc string mainType string subType string req proto.Message @@ -91,6 +97,7 @@ func (r *Robot) addBuilders(builders []*TestCase) { func (r *Robot) handleReq() { for _, b := range r.builders { if b.req != nil && !b.requested { + r.wg.Add(1) time.Sleep(time.Second * 1) b.requested = true b.start = time.Now() @@ -115,7 +122,7 @@ func (r *Robot) handleRsp(msg *pb.UserMessage) { if b.print == nil { printReply(msg, b) } else { - fmt.Printf("===== %s [%s.%s] =====\n", b.desc, msg.MainType, msg.SubType) + fmt.Printf("===== %s [%s.%s] =====\n", b.Desc, msg.MainType, msg.SubType) b.print(b.rsp) fmt.Println("==============================") } @@ -127,6 +134,8 @@ func (r *Robot) handleRsp(msg *pb.UserMessage) { r.builders = append(r.builders[:i], r.builders[i+1:]...) } } + + r.wg.Done() } } @@ -201,7 +210,7 @@ func (r *Robot) AccountRegister(account string, sid int32) { //登录 var user_builders = []*TestCase{ { - desc: "登录", + Desc: "登录", mainType: "user", subType: "login", req: &pb.UserLoginReq{ @@ -226,9 +235,9 @@ func printReply(msg *pb.UserMessage, builder *TestCase) { } else { tt = time.Since(builder.start) } - log.Printf("rsp %s [%v] [%s.%s] [%v:%v]", builder.desc, tt, m.ReqMainType, m.ReqSubType, int32(m.Code), m.Data) + log.Printf("rsp %s [%v] [%s.%s] [%v:%v]", builder.Desc, tt, m.ReqMainType, m.ReqSubType, int32(m.Code), m.Data) } else { - log.Printf("rsp %s [%v] [%s.%s] [%v]", builder.desc, time.Since(builder.start), msg.MainType, msg.SubType, builder.rsp) + log.Printf("rsp %s [%v] [%s.%s] [%v]", builder.Desc, time.Since(builder.start), msg.MainType, msg.SubType, builder.rsp) } } diff --git a/cmd/robot/user.go b/cmd/robot/user.go index c11d030f3..e69d0d740 100644 --- a/cmd/robot/user.go +++ b/cmd/robot/user.go @@ -10,16 +10,16 @@ import ( var user_builders = []*TestCase{ { //create - desc: "创角", + Desc: "创角", mainType: string(comm.ModuleUser), subType: user.UserSubTypeCreate, req: &pb.UserCreateReq{ //设置请求参数 NickName: "乐谷6301", }, - rsp: &pb.UserCreateRsp{}, + rsp: &pb.UserCreateRsp{}, // enabled: true, }, { - desc: "添加资源", + Desc: "添加资源", mainType: string(comm.ModuleUser), subType: user.UserSubTypeAddRes, req: &pb.UserAddResReq{ //设置请求参数 @@ -27,7 +27,7 @@ var user_builders = []*TestCase{ Count: 100, }, rsp: &pb.UserAddResResp{}, - // enabled: true, + enabled: true, }, } diff --git a/modules/friend/api_list.go b/modules/friend/api_list.go index e41bfdfe4..dec98cbd3 100644 --- a/modules/friend/api_list.go +++ b/modules/friend/api_list.go @@ -12,7 +12,7 @@ func (this *apiComp) ListCheck(session comm.IUserSession, req *pb.FriendListReq) } //好友列表 -func (this *apiComp) List(session comm.IUserSession, chk map[string]interface{}, req *pb.FriendListReq) (code pb.ErrorCode, data proto.Message) { +func (this *apiComp) List(session comm.IUserSession, req *pb.FriendListReq) (code pb.ErrorCode, data proto.Message) { var ( self *pb.DBFriend rsp *pb.FriendListRsp diff --git a/modules/friend/api_search.go b/modules/friend/api_search.go index b1de7dc9d..eada9698d 100644 --- a/modules/friend/api_search.go +++ b/modules/friend/api_search.go @@ -7,9 +7,9 @@ import ( "google.golang.org/protobuf/proto" ) -func (this *apiComp) SearchCheck(session comm.IUserSession, req *pb.FriendSearchReq) (code comm.ErrorCode) { +func (this *apiComp) SearchCheck(session comm.IUserSession, req *pb.FriendSearchReq) (code pb.ErrorCode) { if req.NickName == "" { - code = comm.ErrorCode{Code: pb.ErrorCode_FriendSearchNameEmpty} + code = pb.ErrorCode_FriendSearchNameEmpty return } return diff --git a/modules/hero/model_hero.go b/modules/hero/model_hero.go index 83c8837e2..9a80d1680 100644 --- a/modules/hero/model_hero.go +++ b/modules/hero/model_hero.go @@ -165,6 +165,14 @@ func (this *ModelHero) mergeMainProperty(uid, heroId string, data map[string]int hero.Property[comm.Hp] += data[comm.Hp] hero.Property[comm.Atk] += data[comm.Atk] hero.Property[comm.Def] += data[comm.Def] + update := map[string]interface{}{ + comm.Hp: hero.Property[comm.Hp], + comm.Atk: hero.Property[comm.Atk], + comm.Def: hero.Property[comm.Def], + } + if err := this.modifyHeroData(uid, heroId, update); err != nil { + log.Errorf("mergeMainProperty err %v", err) + } } //合并附加属性 @@ -173,9 +181,17 @@ func (this *ModelHero) mergeAddProperty(uid, heroId string, data map[string]int3 if hero == nil { return } - hero.AddProperty[comm.Hp] = data[comm.Hp] - hero.AddProperty[comm.Atk] = data[comm.Atk] - hero.AddProperty[comm.Def] = data[comm.Def] + hero.AddProperty[comm.HpPro] += data[comm.HpPro] + hero.AddProperty[comm.AtkPro] += data[comm.AtkPro] + hero.AddProperty[comm.DefPro] += data[comm.DefPro] + update := map[string]interface{}{ + comm.HpPro: hero.AddProperty[comm.HpPro], + comm.AtkPro: hero.AddProperty[comm.AtkPro], + comm.DefPro: hero.AddProperty[comm.DefPro], + } + if err := this.modifyHeroData(uid, heroId, update); err != nil { + log.Errorf("mergeAddProperty err %v", err) + } } //属性计算 - 暂时放在modelHero里实现