gitee.com/KyleChenSource/lib-robot@v1.0.2/robottest/testcaseimp/net_close.go (about) 1 package testcaseimp 2 3 import ( 4 "math/rand" 5 6 "gitee.com/KyleChenSource/lib-robot/robottest/common" 7 "gitee.com/KyleChenSource/lib-robot/robottest/robot/testcase" 8 9 "gitee.com/KyleChenSource/lib-robot/robottest/protos" 10 11 "gitee.com/KyleChenSource/lib-robot/robottest/log" 12 ) 13 14 type NetCloseFactory struct { 15 } 16 17 type NetCloseCnf struct { 18 testcase.TestcaseActionConfig_ 19 } 20 21 func (this *NetCloseFactory) Cnf() testcase.TestcaseActionCnf { 22 return &NetCloseCnf{} 23 } 24 25 func (this *NetCloseFactory) New(id testcase.TestActionID, tf testcase.ITestFlow, cnf testcase.TestcaseActionCnf) (testcase.ITestcaseAction, error) { 26 return &NetClose{ 27 _id: id, 28 _tf: tf, 29 _cnf: cnf.(*NetCloseCnf), 30 }, nil 31 } 32 33 func init() { 34 testcase.TESTCASEACTION_MGR.Register("NetClose", &NetCloseFactory{}) 35 } 36 37 type NetClose struct { 38 _id testcase.TestActionID 39 _tf testcase.ITestFlow 40 _cnf *NetCloseCnf 41 _timer *common.TimeInterface 42 } 43 44 // 启动 45 // 目前仅仅支持定时器和消息两种驱动模式 46 func (this *NetClose) Start() error { 47 delay := int64(this._cnf.Delay) 48 if this._cnf.RandMin > 0 { 49 delay += (*this._cnf).RandMin 50 } 51 if this._cnf.RandMax > this._cnf.RandMin { 52 delay += rand.Int63n((*this._cnf).RandMax - (*this._cnf).RandMin) 53 } 54 if delay > 0 { 55 t, err := this._tf.TestCtx().TimerAdd(delay, this.OnTimer, nil) 56 if err != nil { 57 return err 58 } 59 this._timer = t 60 log.LogInfo("%s action:%d timer:%d", this._tf.LogPre(), this._id, delay) 61 } else { 62 this.exec() 63 log.LogInfo("%s action:%d OK", this._tf.LogPre(), this._id) 64 } 65 66 return nil 67 } 68 69 // 停止,需要删除目前所有的驱动注册 70 func (this *NetClose) Stop() { 71 if this._timer != nil { 72 this._tf.TestCtx().TimerDel(this._timer) 73 } 74 } 75 76 func (this *NetClose) ID() testcase.TestActionID { 77 return this._id 78 } 79 80 func (this *NetClose) Name() string { 81 return this._cnf.ActionName() 82 } 83 84 func (this *NetClose) Testflow() testcase.ITestFlow { 85 return this._tf 86 } 87 88 func (this *NetClose) OnMsg(protoId protos.ProtoID, header protos.JsonString, data protos.JsonString) { 89 } 90 91 func (this *NetClose) OnTimer(t *common.TimeInterface, data any) int64 { 92 log.LogInfo("%s action:%d timer", this._tf.LogPre(), this._id) 93 this._timer = nil 94 95 this.exec() 96 return 0 97 } 98 99 func (this *NetClose) exec() { 100 this._tf.TestCtx().NetClose("Close by NetClose Action") 101 this._tf.OnActionEnd(this, nil, this._cnf.FailContinue) 102 }