fix: deep copy api step to avoid data racing

This commit is contained in:
xucong053
2022-07-05 20:28:58 +08:00
parent d9829277de
commit ea95651eb3

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