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