fix: avoid data racing

This commit is contained in:
debugtalk
2022-01-06 18:40:00 +08:00
parent c7ed6db537
commit 7148328208
3 changed files with 53 additions and 45 deletions

View File

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

View File

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

View File

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