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

View File

@@ -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)

View File

@@ -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() {

View File

@@ -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