2023-02-22 12:06:55 +00:00
|
|
|
package backoff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRetry_Do(t *testing.T) {
|
|
|
|
counter := 0
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
backoffRetries int
|
|
|
|
backoffFactor float64
|
|
|
|
backoffMinDuration time.Duration
|
|
|
|
retryableFunc retryableFunc
|
2023-06-13 11:54:24 +00:00
|
|
|
cancelTimeout time.Duration
|
2023-02-22 12:06:55 +00:00
|
|
|
want uint64
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "return bad request",
|
|
|
|
retryableFunc: func() error {
|
|
|
|
return ErrBadRequest
|
|
|
|
},
|
|
|
|
want: 0,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty retries values",
|
|
|
|
retryableFunc: func() error {
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
want: 0,
|
2023-06-14 07:47:44 +00:00
|
|
|
wantErr: true,
|
2023-02-22 12:06:55 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "only one retry test",
|
|
|
|
backoffRetries: 5,
|
|
|
|
backoffFactor: 1.7,
|
|
|
|
backoffMinDuration: time.Millisecond * 10,
|
|
|
|
retryableFunc: func() error {
|
|
|
|
t := time.NewTicker(time.Millisecond * 5)
|
|
|
|
defer t.Stop()
|
|
|
|
for range t.C {
|
|
|
|
counter++
|
|
|
|
if counter%2 == 0 {
|
|
|
|
return fmt.Errorf("got some error")
|
|
|
|
}
|
|
|
|
if counter%3 == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
want: 1,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all retries failed test",
|
|
|
|
backoffRetries: 5,
|
|
|
|
backoffFactor: 0.1,
|
|
|
|
backoffMinDuration: time.Millisecond * 10,
|
|
|
|
retryableFunc: func() error {
|
|
|
|
t := time.NewTicker(time.Millisecond * 5)
|
|
|
|
defer t.Stop()
|
|
|
|
for range t.C {
|
|
|
|
return fmt.Errorf("got some error")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
want: 5,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-06-13 11:54:24 +00:00
|
|
|
{
|
|
|
|
name: "cancel context",
|
|
|
|
backoffRetries: 5,
|
2023-06-14 07:47:44 +00:00
|
|
|
backoffFactor: 1.7,
|
2023-06-13 11:54:24 +00:00
|
|
|
backoffMinDuration: time.Millisecond * 10,
|
|
|
|
retryableFunc: func() error {
|
2024-01-22 11:21:14 +00:00
|
|
|
return fmt.Errorf("got some error")
|
2023-06-13 11:54:24 +00:00
|
|
|
},
|
2023-06-14 07:47:44 +00:00
|
|
|
cancelTimeout: time.Millisecond * 40,
|
2023-06-13 11:54:24 +00:00
|
|
|
want: 3,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-02-22 12:06:55 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-06-14 07:47:44 +00:00
|
|
|
r := &Backoff{retries: tt.backoffRetries, factor: tt.backoffFactor, minDuration: tt.backoffMinDuration}
|
2024-01-22 11:21:14 +00:00
|
|
|
ctx := context.Background()
|
2023-06-13 11:54:24 +00:00
|
|
|
if tt.cancelTimeout != 0 {
|
2024-01-22 11:21:14 +00:00
|
|
|
newCtx, cancelFn := context.WithTimeout(context.Background(), tt.cancelTimeout)
|
|
|
|
ctx = newCtx
|
2023-06-13 11:54:24 +00:00
|
|
|
defer cancelFn()
|
|
|
|
}
|
2024-01-22 11:21:14 +00:00
|
|
|
got, err := r.Retry(ctx, tt.retryableFunc)
|
2023-02-22 12:06:55 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("Retry() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("Retry() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|