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