diff --git a/parser_test.go b/parser_test.go index b79f1c2b..dbf75af9 100644 --- a/parser_test.go +++ b/parser_test.go @@ -1,7 +1,6 @@ package hrp import ( - "os" "sort" "testing" "time" @@ -9,49 +8,6 @@ import ( "github.com/stretchr/testify/assert" ) -func TestLocatePlugin(t *testing.T) { - cwd, _ := os.Getwd() - _, err := locatePlugin(cwd) - if !assert.Error(t, err) { - t.Fail() - } - - _, err = locatePlugin("") - if !assert.Error(t, err) { - t.Fail() - } - - startPath := "examples/debugtalk.so" - _, err = locatePlugin(startPath) - if !assert.Nil(t, err) { - t.Fail() - } - - startPath = "examples/demo.json" - _, err = locatePlugin(startPath) - if !assert.Nil(t, err) { - t.Fail() - } - - startPath = "examples/" - _, err = locatePlugin(startPath) - if !assert.Nil(t, err) { - t.Fail() - } - - startPath = "examples/plugin/debugtalk.go" - _, err = locatePlugin(startPath) - if !assert.Nil(t, err) { - t.Fail() - } - - startPath = "/abc" - _, err = locatePlugin(startPath) - if !assert.Error(t, err) { - t.Fail() - } -} - func TestBuildURL(t *testing.T) { var url string url = buildURL("https://postman-echo.com", "/get") diff --git a/plugin_test.go b/plugin_test.go index 41e37684..b811c6e3 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -14,6 +14,7 @@ import ( func TestMain(m *testing.M) { fmt.Println("[TestMain] build go plugin") + // flag -race is necessary in order to be consistent with go test cmd := exec.Command("go", "build", "-buildmode=plugin", `-race`, "-o=examples/debugtalk.so", "examples/plugin/debugtalk.go") if err := cmd.Run(); err != nil { panic(err) @@ -21,6 +22,49 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } +func TestLocatePlugin(t *testing.T) { + cwd, _ := os.Getwd() + _, err := locatePlugin(cwd) + if !assert.Error(t, err) { + t.Fail() + } + + _, err = locatePlugin("") + if !assert.Error(t, err) { + t.Fail() + } + + startPath := "examples/debugtalk.so" + _, err = locatePlugin(startPath) + if !assert.Nil(t, err) { + t.Fail() + } + + startPath = "examples/demo.json" + _, err = locatePlugin(startPath) + if !assert.Nil(t, err) { + t.Fail() + } + + startPath = "examples/" + _, err = locatePlugin(startPath) + if !assert.Nil(t, err) { + t.Fail() + } + + startPath = "examples/plugin/debugtalk.go" + _, err = locatePlugin(startPath) + if !assert.Nil(t, err) { + t.Fail() + } + + startPath = "/abc" + _, err = locatePlugin(startPath) + if !assert.Error(t, err) { + t.Fail() + } +} + func TestCallPluginFunction(t *testing.T) { parser := newParser() parser.loadPlugin("examples/debugtalk.so") diff --git a/runner.go b/runner.go index 644d2c5e..c59e1fec 100644 --- a/runner.go +++ b/runner.go @@ -477,8 +477,16 @@ func (r *caseRunner) runStepTestCase(step *TStep) (stepResult *stepData, err err success: false, } testcase := step.TestCase + + // copy testcase to avoid data racing + copiedTestCase := &TestCase{} + if err = copier.Copy(copiedTestCase, testcase); err != nil { + log.Error().Err(err).Msg("copy testcase failed") + return nil, err + } + start := time.Now() - err = r.hrpRunner.newCaseRunner(testcase).run() + err = r.hrpRunner.newCaseRunner(copiedTestCase).run() stepResult.elapsed = time.Since(start).Milliseconds() if err != nil { return stepResult, err