2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-09-08 14:13:35 +00:00
|
|
|
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 {
|
2023-08-14 13:43:27 +00:00
|
|
|
return max(d, -d)
|
2022-09-08 14:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
result := RandomStagger(tc.input)
|
2022-09-26 13:28:03 +00:00
|
|
|
must.GreaterEq(t, 0, result)
|
|
|
|
must.LessEq(t, abs(tc.input), result)
|
2022-09-08 14:13:35 +00:00
|
|
|
}
|
|
|
|
}
|