improve coverage #118

Merged
vtolstov merged 7 commits from cover into v3 2022-04-17 16:28:59 +03:00
2 changed files with 24 additions and 2 deletions
Showing only changes of commit aa2b5ddaad - Show all commits

View File

@ -19,7 +19,7 @@ func BackoffExp(_ context.Context, _ Request, attempts int) (time.Duration, erro
// BackoffInterval specifies randomization interval for backoff func // BackoffInterval specifies randomization interval for backoff func
func BackoffInterval(min time.Duration, max time.Duration) BackoffFunc { func BackoffInterval(min time.Duration, max time.Duration) BackoffFunc {
return func(_ context.Context, _ Request, attempts int) (time.Duration, error) { return func(_ context.Context, _ Request, attempts int) (time.Duration, error) {
td := time.Duration(time.Duration(math.Pow(float64(attempts), math.E)) * time.Millisecond * 100) td := time.Duration(math.Pow(float64(attempts), math.E)) * time.Millisecond * 100
if td < min { if td < min {
return min, nil return min, nil
} else if td > max { } else if td > max {

View File

@ -6,7 +6,7 @@ import (
"time" "time"
) )
func TestBackoff(t *testing.T) { func TestBackoffExp(t *testing.T) {
results := []time.Duration{ results := []time.Duration{
0 * time.Second, 0 * time.Second,
100 * time.Millisecond, 100 * time.Millisecond,
@ -32,3 +32,25 @@ func TestBackoff(t *testing.T) {
} }
} }
} }
func TestBackoffInterval(t *testing.T) {
min := 100 * time.Millisecond
max := 300 * time.Millisecond
r := &testRequest{
service: "test",
method: "test",
}
fn := BackoffInterval(min, max)
for i := 0; i < 5; i++ {
d, err := fn(context.TODO(), r, i)
if err != nil {
t.Fatal(err)
}
if d < min || d > max {
t.Fatalf("Expected %v < %v < %v", min, d, max)
}
}
}