fix: unsafe to write concurrent map and failed to parse parameters(type:[][]interface{}) in testcase script while data driven for testing

This commit is contained in:
xucong053
2022-04-29 14:19:44 +08:00
parent faf3a2c00a
commit a3a2f2b531
4 changed files with 26 additions and 8 deletions

View File

@@ -108,7 +108,7 @@ func (b *HRPBoomer) convertBoomerTask(testcase *TestCase, rendezvousList []*Rend
sessionRunner := caseRunner.newSession()
if parametersIterator.HasNext() {
sessionRunner.updateConfigVariables(parametersIterator.Next())
sessionRunner.updateSessionVariables(parametersIterator.Next())
}
startTime := time.Now()

View File

@@ -311,6 +311,10 @@ func convertParameters(key string, parametersRawList interface{}) (parameterSlic
for i := 0; i < parametersRawSlice.Len(); i++ {
parametersLine := make(map[string]interface{})
elem := parametersRawSlice.Index(i)
// e.g. Type: interface{} | []interface{}, convert interface{} to []interface{}
if elem.Kind() == reflect.Interface {
elem = elem.Elem()
}
switch elem.Kind() {
case reflect.Slice:
// case 3

View File

@@ -47,6 +47,20 @@ func TestLoadParameters(t *testing.T) {
},
},
},
{
map[string]interface{}{
"username-password": []interface{}{
[]interface{}{"test1", "111111"},
[]interface{}{"test2", "222222"},
},
},
map[string]Parameters{
"username-password": {
{"username": "test1", "password": "111111"},
{"username": "test2", "password": "222222"},
},
},
},
{
map[string]interface{}{},
nil,

View File

@@ -52,12 +52,12 @@ func (r *SessionRunner) Start(givenVars map[string]interface{}) error {
config := r.testCase.Config
log.Info().Str("testcase", config.Name).Msg("run testcase start")
// update config variables with given variables
r.updateConfigVariables(givenVars)
// reset session runner
r.resetSession()
// update config variables with given variables
r.updateSessionVariables(givenVars)
// run step in sequential order
for _, step := range r.testCase.TestSteps {
log.Info().Str("step", step.Name()).
@@ -122,16 +122,16 @@ func (r *SessionRunner) MergeStepVariables(vars map[string]interface{}) (map[str
return parsedVariables, nil
}
// updateConfigVariables updates config variables with given variables.
// updateSessionVariables updates session variables with given variables.
// this is used for data driven
func (r *SessionRunner) updateConfigVariables(parameters map[string]interface{}) {
func (r *SessionRunner) updateSessionVariables(parameters map[string]interface{}) {
if len(parameters) == 0 {
return
}
log.Info().Interface("parameters", parameters).Msg("update config variables")
log.Info().Interface("parameters", parameters).Msg("update session variables")
for k, v := range parameters {
r.parsedConfig.Variables[k] = v
r.sessionVariables[k] = v
}
}