a608e7950e
This PR modifies RandomStagger to protect against negative input values. If the given interval is negative, the value returned will be somewhere in the stratosphere. Instead, treat negative inputs like zero, returning zero.
30 lines
524 B
Go
30 lines
524 B
Go
package helper
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/shoenig/test/must"
|
|
)
|
|
|
|
func TestCluster_RandomStagger(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
input time.Duration
|
|
}{
|
|
{name: "positive", input: 1 * time.Second},
|
|
{name: "negative", input: -1 * time.Second},
|
|
{name: "zero", input: 0},
|
|
}
|
|
|
|
abs := func(d time.Duration) time.Duration {
|
|
return Max(d, -d)
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
result := RandomStagger(tc.input)
|
|
must.GreaterEq(t, result, 0)
|
|
must.LessEq(t, result, abs(tc.input))
|
|
}
|
|
}
|