Merge pull request #1406 from xucong053/bugfix

fix: reuse plugin instance if it already initialized
fix: deep copy api step to avoid data racing
This commit is contained in:
debugtalk
2022-07-09 15:55:55 +08:00
committed by GitHub
3 changed files with 30 additions and 9 deletions
+10
View File
@@ -27,6 +27,8 @@ const (
const projectInfoFile = "proj.json" // used for ensuring root project 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) { func initPlugin(path, venv string, logOn bool) (plugin funplugin.IPlugin, err error) {
// plugin file not found // plugin file not found
if path == "" { if path == "" {
@@ -37,6 +39,11 @@ func initPlugin(path, venv string, logOn bool) (plugin funplugin.IPlugin, err er
return nil, nil 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)} pluginOptions := []funplugin.Option{funplugin.WithLogOn(logOn)}
if strings.HasSuffix(pluginPath, ".py") { if strings.HasSuffix(pluginPath, ".py") {
@@ -69,6 +76,9 @@ func initPlugin(path, venv string, logOn bool) (plugin funplugin.IPlugin, err er
return return
} }
// add plugin instance to plugin map
pluginMap[pluginPath] = plugin
// catch Interrupt and SIGTERM signals to ensure plugin quitted // catch Interrupt and SIGTERM signals to ensure plugin quitted
c := make(chan os.Signal) c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM) signal.Notify(c, os.Interrupt, syscall.SIGTERM)
+3
View File
@@ -3,6 +3,7 @@ package hrp
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"testing" "testing"
"time" "time"
@@ -22,6 +23,8 @@ func buildHashicorpGoPlugin() {
func removeHashicorpGoPlugin() { func removeHashicorpGoPlugin() {
log.Info().Msg("[teardown] remove hashicorp go plugin") log.Info().Msg("[teardown] remove hashicorp go plugin")
os.Remove(tmpl("debugtalk.bin")) os.Remove(tmpl("debugtalk.bin"))
pluginPath, _ := filepath.Abs(tmpl("debugtalk.bin"))
delete(pluginMap, pluginPath)
} }
func buildHashicorpPyPlugin() { func buildHashicorpPyPlugin() {
+17 -9
View File
@@ -2,6 +2,8 @@ package hrp
import ( import (
"fmt" "fmt"
"github.com/jinzhu/copier"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/builtin"
) )
@@ -95,18 +97,24 @@ func (s *StepAPIWithOptionalArgs) Struct() *TStep {
return s.step 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 // extend request with referenced API
api, _ := s.step.API.(*API) api, _ := s.step.API.(*API)
extendWithAPI(s.step, api) step := &TStep{}
// deep copy step to avoid data racing
stepResult, err := runStepRequest(r, s.step) if err = copier.Copy(step, s.step); err != nil {
stepResult.StepType = stepTypeAPI log.Error().Err(err).Msg("copy step failed")
if err != nil { return
r.summary.Success = false
return stepResult, err
} }
return stepResult, nil extendWithAPI(step, api)
stepResult, err = runStepRequest(r, step)
return
} }
// extend teststep with api, teststep will merge and override referenced api // extend teststep with api, teststep will merge and override referenced api