2016-01-29 19:42:34 +00:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-04-23 16:03:47 +00:00
|
|
|
func TestDurationMinusBuffer(t *testing.T) {
|
|
|
|
const (
|
|
|
|
buffer = 10 * time.Second
|
|
|
|
jitter = 16
|
|
|
|
)
|
|
|
|
intv := 1 * time.Minute
|
|
|
|
minValue := (intv - buffer) - ((intv - buffer) / jitter)
|
|
|
|
maxValue := intv - buffer
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
d := DurationMinusBuffer(intv, buffer, jitter)
|
|
|
|
if d < minValue || d > maxValue {
|
|
|
|
t.Fatalf("Bad: %v", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 19:42:34 +00:00
|
|
|
func TestRandomStagger(t *testing.T) {
|
|
|
|
intv := time.Minute
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
stagger := RandomStagger(intv)
|
|
|
|
if stagger < 0 || stagger >= intv {
|
|
|
|
t.Fatalf("Bad: %v", stagger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRateScaledInterval(t *testing.T) {
|
2016-03-25 20:26:29 +00:00
|
|
|
const min = 1 * time.Second
|
2016-01-29 19:42:34 +00:00
|
|
|
rate := 200.0
|
|
|
|
if v := RateScaledInterval(rate, min, 0); v != min {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := RateScaledInterval(rate, min, 100); v != min {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := RateScaledInterval(rate, min, 200); v != 1*time.Second {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := RateScaledInterval(rate, min, 1000); v != 5*time.Second {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := RateScaledInterval(rate, min, 5000); v != 25*time.Second {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := RateScaledInterval(rate, min, 10000); v != 50*time.Second {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
2016-03-25 20:26:29 +00:00
|
|
|
if v := RateScaledInterval(0, min, 10000); v != min {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := RateScaledInterval(0.0, min, 10000); v != min {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := RateScaledInterval(-1, min, 10000); v != min {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
2016-01-29 19:42:34 +00:00
|
|
|
}
|