Skip to content

Commit

Permalink
Treat unitless values as ms, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić committed Sep 9, 2020
1 parent dbc8a9f commit d0582b8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 4 additions & 1 deletion cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ func optionFlagSet() *pflag.FlagSet {
flags.StringSlice("tag", nil, "add a `tag` to be applied to all samples, as `[name]=[value]`")
flags.String("console-output", "", "redirects the console logging to the provided output file")
flags.Bool("discard-response-bodies", false, "Read but don't process or save HTTP response bodies")
flags.String("dns", "ttl=inf,strategy=first", "DNS configuration")
flags.String("dns", "ttl=inf,strategy=first", "DNS configuration. Possible ttl values are: 'inf' "+
"for a persistent cache, '0' to disable the cache,\nor a positive duration, e.g. '1s', '1m', etc. "+
"Milliseconds are assumed if no unit is provided.\n"+
"Possible values for the strategy to use to select a single IP are: 'first', 'random' or 'round-robin'.\n")
return flags
}

Expand Down
9 changes: 9 additions & 0 deletions core/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,12 @@ func TestDNSResolver(t *testing.T) {
Strategy: lib.DefaultDNSConfig().Strategy,
}},
},
"1000ms": { // cache IPs for 1s, test that unitless values are interpreted as ms
lib.Options{DNS: &lib.DNSConfig{
TTL: null.StringFrom("1000"),
Strategy: lib.DefaultDNSConfig().Strategy,
}},
},
"2s": {
lib.Options{DNS: &lib.DNSConfig{
TTL: null.StringFrom("2s"),
Expand Down Expand Up @@ -1064,6 +1070,9 @@ func TestDNSResolver(t *testing.T) {
case "0":
require.Len(t, entries, 5)
checkLog(t, entries)
case "1000ms":
require.Len(t, entries, 3)
checkLog(t, entries)
case "2s":
require.Len(t, entries, 2)
checkLog(t, entries)
Expand Down
4 changes: 2 additions & 2 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,9 @@ func parseTTL(ttlS string) (types.NullDuration, error) {
// disable cache
default:
origTTLs := ttlS
// Treat unitless values as seconds
// Treat unitless values as milliseconds
if t, err := strconv.ParseFloat(ttlS, 32); err == nil {
ttlS = fmt.Sprintf("%.2fs", t)
ttlS = fmt.Sprintf("%.2fms", t)
}
ttlD, err := types.ParseExtendedDuration(ttlS)
if ttlD < 0 || err != nil {
Expand Down

0 comments on commit d0582b8

Please sign in to comment.