From 3e149fa69f4555ea305d36b3c188b58ad8b1e747 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 16 Nov 2021 18:20:25 +0800 Subject: [PATCH] fix: data racing when running boomer --- go.mod | 1 + go.sum | 2 ++ parser.go | 6 ------ runner.go | 23 ++++++++++++++++------- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 029efa05..1d671ab9 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.16 require ( github.com/go-ole/go-ole v1.2.6 // indirect + github.com/jinzhu/copier v0.3.2 github.com/jmespath/go-jmespath v0.4.0 github.com/maja42/goval v1.2.1 github.com/mattn/go-runewidth v0.0.13 // indirect diff --git a/go.sum b/go.sum index f5595ca6..852a9ca8 100644 --- a/go.sum +++ b/go.sum @@ -171,6 +171,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jinzhu/copier v0.3.2 h1:QdBOCbaouLDYaIPFfi1bKv5F5tPpeTwXe4sD0jqtz5w= +github.com/jinzhu/copier v0.3.2/go.mod h1:24xnZezI2Yqac9J61UC6/dG/k76ttpq0DdJI3QmUvro= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= diff --git a/parser.go b/parser.go index 4a16d993..21e98bd7 100644 --- a/parser.go +++ b/parser.go @@ -13,12 +13,6 @@ import ( "github.com/httprunner/hrp/builtin" ) -func parseStep(step IStep, config *TConfig) *TStep { - tStep := step.ToStruct() - tStep.Request.URL = buildURL(config.BaseURL, tStep.Request.URL) - return tStep -} - func buildURL(baseURL, stepURL string) string { uConfig, err := url.Parse(baseURL) if err != nil { diff --git a/runner.go b/runner.go index 30f7832d..a04eeda7 100644 --- a/runner.go +++ b/runner.go @@ -14,6 +14,7 @@ import ( "testing" "time" + "github.com/jinzhu/copier" "github.com/pkg/errors" ) @@ -104,9 +105,17 @@ func (r *Runner) runCase(testcase *TestCase) error { func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err error) { log.Info().Str("step", step.Name()).Msg("run step start") + // copy step to avoid data racing + copiedStep := &TStep{} + if err = copier.Copy(copiedStep, step.ToStruct()); err != nil { + log.Error().Err(err).Msg("copy step data failed") + return + } + + stepVariables := copiedStep.Variables // override variables // step variables > session variables (extracted variables from previous steps) - stepVariables := mergeVariables(step.ToStruct().Variables, r.sessionVariables) + stepVariables = mergeVariables(stepVariables, r.sessionVariables) // step variables > testcase config variables stepVariables = mergeVariables(stepVariables, config.Variables) @@ -116,21 +125,21 @@ func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err e log.Error().Interface("variables", config.Variables).Err(err).Msg("parse step variables failed") return } - step.ToStruct().Variables = parsedVariables + copiedStep.Variables = parsedVariables // avoid data racing - if tc, ok := step.(*testcaseWithOptionalArgs); ok { + if _, ok := step.(*testcaseWithOptionalArgs); ok { // run referenced testcase - log.Info().Str("testcase", tc.step.Name).Msg("run referenced testcase") + log.Info().Str("testcase", copiedStep.Name).Msg("run referenced testcase") // TODO: override testcase config - stepData, err = r.runStepTestCase(tc.step) + stepData, err = r.runStepTestCase(copiedStep) if err != nil { log.Error().Err(err).Msg("run referenced testcase step failed") return } } else { // run request - tStep := parseStep(step, config) - stepData, err = r.runStepRequest(tStep) + copiedStep.Request.URL = buildURL(config.BaseURL, copiedStep.Request.URL) // avoid data racing + stepData, err = r.runStepRequest(copiedStep) if err != nil { log.Error().Err(err).Msg("run request step failed") return