-
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.
Merge pull request #14497 from hashicorp/b-guard-random-stagger
helper: guard against negative inputs into random stagger
- Loading branch information
Showing
3 changed files
with
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:bug | ||
helpers: Fixed a bug where random stagger func did not protect against negative inputs | ||
``` |
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)) | ||
} | ||
} |