Skip to content

Commit

Permalink
Merge pull request #261 from bojand/calldata_func_random_int
Browse files Browse the repository at this point in the history
Add random int template function to call data
  • Loading branch information
bojand authored Feb 20, 2021
2 parents f3a8748 + d99d72f commit b9aa837
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
13 changes: 13 additions & 0 deletions runner/calldata.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type CallData struct {
var tmplFuncMap = template.FuncMap{
"newUUID": newUUID,
"randomString": randomString,
"randomInt": randomInt,
}

// newCallData returns new CallData
Expand Down Expand Up @@ -208,3 +209,15 @@ func randomString(length int) string {

return stringWithCharset(length, charset)
}

func randomInt(min, max int) int {
if min < 0 {
min = 0
}

if max <= 0 {
max = 1
}

return seededRand.Intn(max-min) + min
}
39 changes: 39 additions & 0 deletions runner/calldata_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runner

import (
"strconv"
"strings"
"testing"
"text/template"
Expand Down Expand Up @@ -249,6 +250,44 @@ func TestCallTemplateData_ExecuteFuncs(t *testing.T) {
assert.NotEqual(t, rm["trace_id"], rm["span_id"])
})

t.Run("randomInt", func(t *testing.T) {
ctd := newCallData(md, nil, "worker_id_123", 200)
assert.NotNil(t, ctd)

// no template
r, err := ctd.ExecuteData(`{"trace_id":"asdf"}`)
assert.NoError(t, err)
assert.Equal(t, `{"trace_id":"asdf"}`, string(r))

rm, err := ctd.executeMetadata(`{"trace_id":"asdf"}`)
assert.NoError(t, err)
assert.Equal(t, map[string]string{"trace_id": "asdf"}, rm)

// 0 when 0
r, err = ctd.ExecuteData(`{"trace_id":"{{randomInt 0 0}}"}`)
assert.NoError(t, err)
assert.Equal(t, `{"trace_id":"0"}`, string(r))

// 0 when min -1
r, err = ctd.ExecuteData(`{"trace_id":"{{randomInt -1 1}}"}`)
assert.NoError(t, err)
assert.Equal(t, `{"trace_id":"0"}`, string(r))

// 0 when max -1
r, err = ctd.ExecuteData(`{"trace_id":"{{randomInt 0 -1}}"}`)
assert.NoError(t, err)
assert.Equal(t, `{"trace_id":"0"}`, string(r))

// specific range
r, err = ctd.ExecuteData(`{"trace_id":"{{randomInt 4 10}}"}`)
assert.NoError(t, err)
rs := strings.Replace(string(r), `{"trace_id":"`, "", -1)
rs = strings.Replace(rs, `"}`, "", -1)
n, err := strconv.Atoi(rs)
assert.NoError(t, err)
assert.True(t, 4 <= n && n < 10)
})

t.Run("custom functions", func(t *testing.T) {

ctd := newCallData(md, nil, "worker_id_123", 200)
Expand Down
12 changes: 9 additions & 3 deletions www/docs/calldata.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,17 @@ type CallData struct {

**Template Functions**

There are also two template functions available:
There are also template functions available:

`newUUID` - Generates a new UUID for each invocation.
`func newUUID() string`
Generates a new UUID for each invocation.

`func randomString(length int) string`
Generates a new random string for each incovation. Accepts a length parameter. If the argument is `<= 0` then a ranom string is generated with a random length between length of `2` and `16`.

`func randomInt(min, max int) int`
Generates a new non-negative pseudo-random number in range `[min, max)`.

`randomString` - Generates a new random string for each incovation. Accepts a length parameter. If the argument is `<= 0` then a ranom string is generated with a random length between length of `2` and `16`.

**Examples**

Expand Down

0 comments on commit b9aa837

Please sign in to comment.