From 7a6890a160583420d1e4fa1c5e2e3f10c648a703 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Mon, 12 May 2025 08:47:47 +0800 Subject: [PATCH] feat: set timeout for Call function --- internal/version/VERSION | 2 +- step_ui.go | 4 ++-- uixt/driver_action.go | 2 +- uixt/driver_handler.go | 39 +++++++++++++++++++++++++++++++-------- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/internal/version/VERSION b/internal/version/VERSION index aefbbf5f..71fc4cdc 100644 --- a/internal/version/VERSION +++ b/internal/version/VERSION @@ -1 +1 @@ -v5.0.0-beta-2505100950 +v5.0.0-beta-2505120848 diff --git a/step_ui.go b/step_ui.go index 3a723c87..1c3e5d38 100644 --- a/step_ui.go +++ b/step_ui.go @@ -449,12 +449,12 @@ func (s *StepMobile) ClosePopups(opts ...option.ActionOption) *StepMobile { return s } -func (s *StepMobile) Call(name string, fn func()) *StepMobile { +func (s *StepMobile) Call(name string, fn func(), opts ...option.ActionOption) *StepMobile { s.obj().Actions = append(s.obj().Actions, uixt.MobileAction{ Method: uixt.ACTION_CallFunction, Params: name, // function description Fn: fn, - Options: nil, + Options: option.NewActionOptions(opts...), }) return s } diff --git a/uixt/driver_action.go b/uixt/driver_action.go index 0f79c981..767e27e4 100644 --- a/uixt/driver_action.go +++ b/uixt/driver_action.go @@ -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: diff --git a/uixt/driver_handler.go b/uixt/driver_handler.go index 2fcc321d..372bfca6 100644 --- a/uixt/driver_handler.go +++ b/uixt/driver_handler.go @@ -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) (