Skip to content

Commit 2adb522

Browse files
committed
fix issue #34, add stepDefs mutex and lock in appropriate places to prevent races
1 parent ae1ad2e commit 2adb522

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

find_step.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
func (r *Runner) findStep(t *testing.T, step *messages.PickleStep) *stepDef {
1010
t.Helper()
1111

12-
for _, def := range r.stepDefs {
13-
matches := def.regex.FindSubmatch([]byte(step.Text))
14-
if len(matches) != 0 {
15-
return def
16-
}
12+
r.stepDefsMutex.RLock()
13+
def := findSubmatch(t, step.Text, r.stepDefs)
14+
r.stepDefsMutex.RUnlock()
15+
if def != nil {
16+
return def
1717
}
1818

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

3131
return nil
3232
}
33+
34+
func findSubmatch(t *testing.T, stepText string, stepDefs []*stepDef) *stepDef {
35+
t.Helper()
36+
37+
for _, def := range stepDefs {
38+
matches := def.regex.FindSubmatch([]byte(stepText))
39+
if len(matches) != 0 {
40+
return def
41+
}
42+
}
43+
44+
return nil
45+
}

runner.go

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gocuke
22

33
import (
44
"reflect"
5+
"sync"
56
"testing"
67

78
messages "github.com/cucumber/messages/go/v22"
@@ -17,6 +18,7 @@ type Runner struct {
1718
paths []string
1819
parallel bool
1920
stepDefs []*stepDef
21+
stepDefsMutex *sync.RWMutex
2022
haveSuggestion map[string]bool
2123
suggestions []methodSig
2224
supportedSpecialArgs map[reflect.Type]specialArgGetter
@@ -84,6 +86,7 @@ func NewRunner(t *testing.T, suiteType interface{}) *Runner {
8486
},
8587
},
8688
suiteUsesRapid: false,
89+
stepDefsMutex: &sync.RWMutex{},
8790
}
8891

8992
r.registerSuite(suiteType)

step_def.go

+2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ func (r *Runner) Step(step interface{}, definition interface{}) *Runner {
5454

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

59+
r.stepDefsMutex.Lock()
5860
def := r.newStepDefOrHook(t, exp, definition)
5961
r.stepDefs = append(r.stepDefs, def)
6062
return def

0 commit comments

Comments
 (0)