Skip to content

Commit

Permalink
fix issue regen-network#34, add stepDefs mutex and lock in appropriat…
Browse files Browse the repository at this point in the history
…e places to prevent races
  • Loading branch information
pauleibye committed Aug 25, 2024
1 parent ae1ad2e commit f6a51e7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
23 changes: 18 additions & 5 deletions find_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
func (r *Runner) findStep(t *testing.T, step *messages.PickleStep) *stepDef {
t.Helper()

for _, def := range r.stepDefs {
matches := def.regex.FindSubmatch([]byte(step.Text))
if len(matches) != 0 {
return def
}
r.stepDefsMutex.RLock()
def := findSubmatch(t, step.Text, r.stepDefs)
r.stepDefsMutex.RUnlock()
if def != nil {
return def
}

sig := guessMethodSig(step)
Expand All @@ -30,3 +30,16 @@ func (r *Runner) findStep(t *testing.T, step *messages.PickleStep) *stepDef {

return nil
}

func findSubmatch(t *testing.T, stepText string, stepDefs []*stepDef) *stepDef {
t.Helper()

for _, def := range stepDefs {
matches := def.regex.FindSubmatch([]byte(stepText))
if len(matches) != 0 {
return def
}
}

return nil
}
3 changes: 3 additions & 0 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gocuke

import (
"reflect"
"sync"
"testing"

messages "github.com/cucumber/messages/go/v22"
Expand All @@ -17,6 +18,7 @@ type Runner struct {
paths []string
parallel bool
stepDefs []*stepDef
stepDefsMutex *sync.RWMutex
haveSuggestion map[string]bool
suggestions []methodSig
supportedSpecialArgs map[reflect.Type]specialArgGetter
Expand Down Expand Up @@ -84,6 +86,7 @@ func NewRunner(t *testing.T, suiteType interface{}) *Runner {
},
},
suiteUsesRapid: false,
stepDefsMutex: &sync.RWMutex{},
}

r.registerSuite(suiteType)
Expand Down
9 changes: 6 additions & 3 deletions step_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"regexp"
"runtime"
"sync"
"testing"

"gotest.tools/v3/assert"
Expand Down Expand Up @@ -54,7 +55,9 @@ func (r *Runner) Step(step interface{}, definition interface{}) *Runner {

func (r *Runner) addStepDef(t *testing.T, exp *regexp.Regexp, definition reflect.Value) *stepDef {
t.Helper()
defer r.stepDefsMutex.Unlock()

r.stepDefsMutex.Lock()
def := r.newStepDefOrHook(t, exp, definition)
r.stepDefs = append(r.stepDefs, def)
return def
Expand All @@ -73,9 +76,9 @@ func (r *Runner) newStepDefOrHook(t *testing.T, exp *regexp.Regexp, f reflect.Va
file, line := rfunc.FileLine(funcPtr)

def := &stepDef{
regex: exp,
theFunc: f,
funcLoc: fmt.Sprintf("%s (%s:%d)", rfunc.Name(), file, line),
regex: exp,
theFunc: f,
funcLoc: fmt.Sprintf("%s (%s:%d)", rfunc.Name(), file, line),
}

for i := 0; i < typ.NumIn(); i++ {
Expand Down

0 comments on commit f6a51e7

Please sign in to comment.