Makes the API behave better with small wait values.

This commit is contained in:
James Phillips 2016-01-06 11:05:11 -08:00
parent 49342dc973
commit c048c5aca6
2 changed files with 27 additions and 2 deletions

View File

@ -261,9 +261,16 @@ func (r *request) setQueryOptions(q *QueryOptions) {
}
}
// durToMsec converts a duration to a millisecond specified string
// durToMsec converts a duration to a millisecond specified string. If the
// user selected a positive value that rounds to 0 ms, then we will use 1 ms
// so they get a short delay, otherwise Consul will translate the 0 ms into
// a huge default delay.
func durToMsec(dur time.Duration) string {
return fmt.Sprintf("%dms", dur/time.Millisecond)
ms := dur / time.Millisecond
if dur > 0 && ms == 0 {
ms = 1
}
return fmt.Sprintf("%dms", ms)
}
// setWriteOptions is used to annotate the request with

View File

@ -254,3 +254,21 @@ func TestAPI_UnixSocket(t *testing.T) {
t.Fatalf("bad: %v", info)
}
}
func TestAPI_durToMsec(t *testing.T) {
if ms := durToMsec(0); ms != "0ms" {
t.Fatalf("bad: %s", ms)
}
if ms := durToMsec(time.Millisecond); ms != "1ms" {
t.Fatalf("bad: %s", ms)
}
if ms := durToMsec(time.Microsecond); ms != "1ms" {
t.Fatalf("bad: %s", ms)
}
if ms := durToMsec(5 * time.Millisecond); ms != "5ms" {
t.Fatalf("bad: %s", ms)
}
}