Skip to content

Commit

Permalink
test: Add tests for retry func
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Aug 28, 2023
1 parent dd9f9e4 commit 7067afc
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions internal/util/retry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package util

import (
"context"
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestRetry(t *testing.T) {
t.Run("halt", func(t *testing.T) {
targetErr := errors.New("test")
var runs int
if err := Retry(context.Background(), 10, 0, func(try uint) error {
runs += 1
return HaltRetries(targetErr)
}); !assert.ErrorIs(t, targetErr, err) {
return
}
assert.Equal(t, 1, runs)
})

t.Run("max", func(t *testing.T) {
var runs int
if err := Retry(context.Background(), 10, 0, func(try uint) error {
runs += 1
return errors.New("test")
}); !assert.Error(t, err) {
return
}
assert.Equal(t, 10, runs)
})

t.Run("pass on first run", func(t *testing.T) {
var runs int
if err := Retry(context.Background(), 10, 0, func(try uint) error {
runs += 1
return nil
}); !assert.NoError(t, err) {
return
}
assert.Equal(t, 1, runs)
})

t.Run("pass on fifth run", func(t *testing.T) {
var runs int
if err := Retry(context.Background(), 10, 0, func(try uint) error {
runs += 1
if runs < 5 {
return errors.New("test")
}
return nil
}); !assert.NoError(t, err) {
return
}
assert.Equal(t, 5, runs)
})

t.Run("sleep backoff", func(t *testing.T) {
var runs int
start := time.Now()
if err := Retry(context.Background(), 10, time.Millisecond, func(try uint) error {
runs += 1
if runs < 5 {
return errors.New("test")
}
return nil
}); !assert.NoError(t, err) {
return
}
assert.Equal(t, 5, runs)
assert.Greater(t, time.Since(start), 15*time.Millisecond)
})

t.Run("cancel context", func(t *testing.T) {
var runs int
ctx, cancel := context.WithCancel(context.Background())
cancel()
if err := Retry(ctx, 10, 0, func(try uint) error {
runs += 1
return errors.New("test")
}); !assert.Error(t, err) {
return
}
assert.Equal(t, 1, runs)
})
}

0 comments on commit 7067afc

Please sign in to comment.