From d9829277de3e7dea8b19e1e94af11aa2ade45dfb Mon Sep 17 00:00:00 2001 From: xucong053 Date: Tue, 5 Jul 2022 20:15:42 +0800 Subject: [PATCH 1/3] fix: reuse plugin instance if it already initialized --- hrp/plugin.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/hrp/plugin.go b/hrp/plugin.go index a2343358..9ab2017e 100644 --- a/hrp/plugin.go +++ b/hrp/plugin.go @@ -27,6 +27,8 @@ const ( const projectInfoFile = "proj.json" // used for ensuring root project +var pluginMap = map[string]funplugin.IPlugin{} // used for reusing plugin instance + func initPlugin(path, venv string, logOn bool) (plugin funplugin.IPlugin, err error) { // plugin file not found if path == "" { @@ -37,6 +39,11 @@ func initPlugin(path, venv string, logOn bool) (plugin funplugin.IPlugin, err er return nil, nil } + // reuse plugin instance if it already initialized + if p, ok := pluginMap[pluginPath]; ok { + return p, nil + } + pluginOptions := []funplugin.Option{funplugin.WithLogOn(logOn)} if strings.HasSuffix(pluginPath, ".py") { @@ -69,6 +76,9 @@ func initPlugin(path, venv string, logOn bool) (plugin funplugin.IPlugin, err er return } + // add plugin instance to plugin map + pluginMap[pluginPath] = plugin + // catch Interrupt and SIGTERM signals to ensure plugin quitted c := make(chan os.Signal) signal.Notify(c, os.Interrupt, syscall.SIGTERM) From ea95651eb3a55022b15bc84c5ca2f5fba920ddf7 Mon Sep 17 00:00:00 2001 From: xucong053 Date: Tue, 5 Jul 2022 20:28:58 +0800 Subject: [PATCH 2/3] fix: deep copy api step to avoid data racing --- hrp/step_api.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/hrp/step_api.go b/hrp/step_api.go index a2c0bad0..d8483997 100644 --- a/hrp/step_api.go +++ b/hrp/step_api.go @@ -2,6 +2,8 @@ package hrp import ( "fmt" + "github.com/jinzhu/copier" + "github.com/rs/zerolog/log" "github.com/httprunner/httprunner/v4/hrp/internal/builtin" ) @@ -95,18 +97,24 @@ func (s *StepAPIWithOptionalArgs) Struct() *TStep { return s.step } -func (s *StepAPIWithOptionalArgs) Run(r *SessionRunner) (*StepResult, error) { +func (s *StepAPIWithOptionalArgs) Run(r *SessionRunner) (stepResult *StepResult, err error) { + defer func() { + if err != nil { + r.summary.Success = false + } + stepResult.StepType = stepTypeAPI + }() // extend request with referenced API api, _ := s.step.API.(*API) - extendWithAPI(s.step, api) - - stepResult, err := runStepRequest(r, s.step) - stepResult.StepType = stepTypeAPI - if err != nil { - r.summary.Success = false - return stepResult, err + step := &TStep{} + // deep copy step to avoid data racing + if err = copier.Copy(step, s.step); err != nil { + log.Error().Err(err).Msg("copy step failed") + return } - return stepResult, nil + extendWithAPI(step, api) + stepResult, err = runStepRequest(r, step) + return } // extend teststep with api, teststep will merge and override referenced api From 85e9a091d0d019e5d057956e30df9b018c70288c Mon Sep 17 00:00:00 2001 From: xucong053 Date: Tue, 5 Jul 2022 22:36:34 +0800 Subject: [PATCH 3/3] fix: unittest --- hrp/runner_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hrp/runner_test.go b/hrp/runner_test.go index d5d7c30b..6b92fdf5 100644 --- a/hrp/runner_test.go +++ b/hrp/runner_test.go @@ -3,6 +3,7 @@ package hrp import ( "io/ioutil" "os" + "path/filepath" "testing" "time" @@ -22,6 +23,8 @@ func buildHashicorpGoPlugin() { func removeHashicorpGoPlugin() { log.Info().Msg("[teardown] remove hashicorp go plugin") os.Remove(tmpl("debugtalk.bin")) + pluginPath, _ := filepath.Abs(tmpl("debugtalk.bin")) + delete(pluginMap, pluginPath) } func buildHashicorpPyPlugin() {