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 26, 2024
1 parent ae1ad2e commit 2adb522
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 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
2 changes: 2 additions & 0 deletions step_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,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 Down

0 comments on commit 2adb522

Please sign in to comment.