Skip to content

Commit

Permalink
Merge pull request #124 from ddosify/fix/prevent-race-on-dynamic-env
Browse files Browse the repository at this point in the history
prevent race on calls to faker func
  • Loading branch information
fatihbaltaci authored Jan 31, 2023
2 parents 20c04bc + f805aaa commit e95ee31
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fi'''
steps {
lock('multi_branch_server') {
sh 'go run --race main.go -t https://servdown.com/ -d 1 -n 1500'
sh 'go test -race -run ^TestDynamicVariableRace$ go.ddosify.com/ddosify/core/scenario/scripting/injection'
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions core/scenario/scripting/injection/dynamic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package injection

import (
"testing"
)

func TestDynamicVariableRace(t *testing.T) {
num := 10
ei := EnvironmentInjector{}
for key := range dynamicFakeDataMap {
for i := 0; i < num; i++ {
go ei.getFakeData(key)
}
}
}
11 changes: 9 additions & 2 deletions core/scenario/scripting/injection/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"regexp"
"strings"
"sync"
"time"

"go.ddosify.com/ddosify/core/types/regex"
Expand All @@ -17,6 +18,7 @@ type EnvironmentInjector struct {
jr *regexp.Regexp
dr *regexp.Regexp
jdr *regexp.Regexp
mu sync.Mutex
}

func (ei *EnvironmentInjector) Init() {
Expand All @@ -34,8 +36,13 @@ func (ei *EnvironmentInjector) getFakeData(key string) (interface{}, error) {
return nil, fmt.Errorf("%s is not a valid dynamic variable", key)
}

res := reflect.ValueOf(fakeFunc).Call(nil)[0].Interface()
return res, nil
preventRaceOnRandomFunc := func(fakeFunc interface{}) interface{} {
ei.mu.Lock()
defer ei.mu.Unlock()
return reflect.ValueOf(fakeFunc).Call(nil)[0].Interface()
}

return preventRaceOnRandomFunc(fakeFunc), nil
}

func truncateTag(tag string, rx string) string {
Expand Down

0 comments on commit e95ee31

Please sign in to comment.