-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helper: guard against negative inputs into random stagger
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.
- Loading branch information
Showing
2 changed files
with
32 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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)) | ||
} | ||
} |