mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-07 23:41:22 +08:00
feat: call function
This commit is contained in:
11
builtin/function.go
Normal file
11
builtin/function.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package builtin
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
var FunctionsMap = map[string]interface{}{
|
||||||
|
"sleep": Sleep,
|
||||||
|
}
|
||||||
|
|
||||||
|
func Sleep(nSecs int) {
|
||||||
|
time.Sleep(time.Duration(nSecs) * time.Second)
|
||||||
|
}
|
||||||
26
parser.go
26
parser.go
@@ -7,6 +7,8 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/httprunner/httpboomer/builtin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseStep(step IStep, config *TConfig) *TStep {
|
func parseStep(step IStep, config *TConfig) *TStep {
|
||||||
@@ -166,3 +168,27 @@ func mergeVariables(variables, overriddenVariables map[string]interface{}) map[s
|
|||||||
}
|
}
|
||||||
return mergedVariables
|
return mergedVariables
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func callFunction(funcName string, params []interface{}) (interface{}, error) {
|
||||||
|
function, ok := builtin.FunctionsMap[funcName]
|
||||||
|
if !ok {
|
||||||
|
// function not found
|
||||||
|
return nil, fmt.Errorf("function %s is not found", funcName)
|
||||||
|
}
|
||||||
|
|
||||||
|
funcValue := reflect.ValueOf(function)
|
||||||
|
if funcValue.Kind() != reflect.Func {
|
||||||
|
// function not valid
|
||||||
|
return nil, fmt.Errorf("function %s is invalid", funcName)
|
||||||
|
}
|
||||||
|
|
||||||
|
paramsValue := make([]reflect.Value, len(params))
|
||||||
|
for index, param := range params {
|
||||||
|
paramsValue[index] = reflect.ValueOf(param)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("[callFunction] function: %v, params: %v", funcName, params)
|
||||||
|
result := funcValue.Call(paramsValue)
|
||||||
|
log.Printf("[callFunction] result: %v", result)
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package httpboomer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@@ -278,3 +279,16 @@ func TestMergeVariables(t *testing.T) {
|
|||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCallFunction(t *testing.T) {
|
||||||
|
funcName := "sleep"
|
||||||
|
params := []interface{}{1}
|
||||||
|
timeStart := time.Now()
|
||||||
|
_, err := callFunction(funcName, params)
|
||||||
|
if !assert.Nil(t, err) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if !assert.Greater(t, time.Since(timeStart), time.Duration(1)*time.Second) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user