-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Templating for generating random Ports #441
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"path/filepath" | ||
"regexp" | ||
"strings" | ||
"strconv" | ||
"testing" | ||
"text/template" | ||
"time" | ||
|
@@ -119,13 +120,15 @@ func runTestSpec(t *testing.T, test *TestSpec) (err error) { | |
startTime := time.Now().UnixNano() / 1000000 | ||
|
||
for i = -1; i < cmdSpec.Retry; i++ { | ||
err = nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be a good spot for a comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neha is putting up comments in her next PR. |
||
|
||
cmdString := generateCmdString(&cmdSpec) | ||
tmplOutput, tmplErr := performTemplating(strings.Join(cmdString, " "), cache) | ||
if tmplErr != nil { | ||
err = fmt.Errorf("Executing templating failed: %s", tmplErr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is an error with templating, we don't want to keep retrying (which makes setting err to nil unnecessary). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can make the templating error return the same as the timeout err statement, however, the reason the |
||
t.Log(err) | ||
} | ||
tmplString := strings.Fields(tmplOutput) | ||
tmplString = strings.Fields(tmplOutput) | ||
|
||
t.Logf("Running: %s", strings.Join(tmplString, " ")) | ||
cmdOutput, cmdErr := exec.Command(tmplString[0], tmplString[1:]...).CombinedOutput() | ||
|
@@ -148,7 +151,7 @@ func runTestSpec(t *testing.T, test *TestSpec) (err error) { | |
t.Log("This command :", tmplString, "has re-run", i, "times.") | ||
} | ||
} | ||
return | ||
return err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding |
||
} | ||
|
||
func generateCmdString(cmdSpec *CommandSpec) (cmdString []string) { | ||
|
@@ -208,9 +211,18 @@ func performTemplating(s string, cache map[string]string) (output string, err er | |
cache[in] = out | ||
return out | ||
} | ||
p := func(in string, min, max int) string { | ||
if val, ok := cache[in]; ok { | ||
return val | ||
} | ||
out := strconv.Itoa(rand.Intn(max - min) + min) | ||
cache[in] = out | ||
return out | ||
} | ||
var doc bytes.Buffer | ||
var fm = template.FuncMap{ | ||
"uniq": func(in string) string { return f(in) }, | ||
"port": func(in string, min, max int) string { return p(in, min, max) }, | ||
} | ||
err = t.Execute(&doc, fm) | ||
if err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can simplify your time code. Just use
time.now()
and later check elapsed (eg,elapsed := time.Since(startTime)
). I believe the units you're using for the timeout are seconds (needs a comment!), so multiply the timeout by time.Second so you can compare against elapsed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See Go Playground for a simple example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
timeout and delay are defined in milliseconds.