mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-10 23:12:41 +08:00
feat: set timeout for Call function
This commit is contained in:
@@ -336,7 +336,7 @@ func (dExt *XTDriver) DoAction(action MobileAction) (err error) {
|
||||
return dExt.ClosePopupsHandler()
|
||||
case ACTION_CallFunction:
|
||||
if funcDesc, ok := action.Params.(string); ok {
|
||||
return dExt.Call(funcDesc, action.Fn)
|
||||
return dExt.Call(funcDesc, action.Fn, action.GetOptions()...)
|
||||
}
|
||||
return fmt.Errorf("invalid function description: %v", action.Params)
|
||||
case ACTION_AIAction:
|
||||
|
||||
@@ -1,21 +1,44 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/uixt/option"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// Call custom function, used for pre/post hook for actions
|
||||
func (dExt *XTDriver) Call(desc string, fn func()) error {
|
||||
startTime := time.Now()
|
||||
fn()
|
||||
log.Info().Str("desc", desc).
|
||||
Int64("duration(ms)", time.Since(startTime).Milliseconds()).
|
||||
Msg("function called")
|
||||
// Call custom function, used for pre/post action hook
|
||||
func (dExt *XTDriver) Call(desc string, fn func(), opts ...option.ActionOption) error {
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
|
||||
return nil
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
log.Info().Str("desc", desc).
|
||||
Int64("duration(ms)", time.Since(startTime).Milliseconds()).
|
||||
Msg("function called")
|
||||
}()
|
||||
|
||||
if actionOptions.Timeout == 0 {
|
||||
// wait for function to finish
|
||||
fn()
|
||||
return nil
|
||||
}
|
||||
|
||||
// set timeout for function execution
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
fn()
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
// function completed within timeout
|
||||
return nil
|
||||
case <-time.After(time.Duration(actionOptions.Timeout) * time.Second):
|
||||
return fmt.Errorf("function execution exceeded timeout of %d seconds", actionOptions.Timeout)
|
||||
}
|
||||
}
|
||||
|
||||
func preHandler_TapAbsXY(driver IDriver, options *option.ActionOptions, rawX, rawY float64) (
|
||||
|
||||
Reference in New Issue
Block a user