-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
GH-35 randomize validation time
- Loading branch information
Showing
3 changed files
with
71 additions
and
3 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,21 @@ | ||
package common | ||
|
||
import ( | ||
"time" | ||
|
||
"k8s.io/apimachinery/pkg/util/rand" | ||
) | ||
|
||
// RandomizeDuration randomizes duration for a given variance. | ||
// variance is expected to be of a format 0.1 for 10%, 0.5 for 50% and so on | ||
func RandomizeDuration(variance float64, duration time.Duration) time.Duration { | ||
// we won't go smaller than a second - using milliseconds to have a relatively big number to randomize | ||
millisecond := float64(duration.Milliseconds()) | ||
|
||
upperLimit := millisecond * (1.0 + variance) | ||
lowerLimit := millisecond * (1.0 - variance) | ||
|
||
return time.Millisecond * time.Duration(rand.Int63nRange( | ||
int64(lowerLimit), | ||
int64(upperLimit))) | ||
} |
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,41 @@ | ||
package common | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestRandomizeDuration(t *testing.T) { | ||
testIterations := 100 | ||
|
||
tests := []struct { | ||
name string | ||
variance float64 | ||
duration time.Duration | ||
}{ | ||
{ | ||
name: "returns valid duration in range", | ||
variance: 0.5, | ||
duration: time.Second * 5, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
i := 0 | ||
for i < testIterations { | ||
if got := RandomizeDuration(tt.variance, tt.duration); !isValidVariance(tt.duration, got, tt.variance) { | ||
t.Errorf("RandomizeDuration() invalid randomization; got = %v", got.String()) | ||
} | ||
i++ | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func isValidVariance(duration, randomizedDuration time.Duration, variance float64) bool { | ||
upperLimit := float64(duration.Milliseconds()) + float64(duration.Milliseconds())*variance | ||
lowerLimmit := float64(duration.Milliseconds()) - float64(duration.Milliseconds())*variance | ||
|
||
return float64(randomizedDuration.Milliseconds()) > lowerLimmit && | ||
float64(randomizedDuration.Milliseconds()) < upperLimit | ||
} |
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