Skip to content

Commit

Permalink
add tests to retry mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
najeal committed Dec 12, 2024
1 parent eb7887f commit 430fcaf
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions utils/backoff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package utils

import (
"errors"
"testing"

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

func TestWithMaxRetries(t *testing.T) {
t.Run("NotEnoughRetry", func(t *testing.T) {
retryable := newMockRetryableFn(3)
err := WithMaxRetries(
func() (err error) {
_, err = retryable.Run()
return err
},
2,
)
require.Error(t, err)
})
t.Run("EnoughRetry", func(t *testing.T) {
retryable := newMockRetryableFn(2)
var res bool
err := WithMaxRetries(
func() (err error) {
res, err = retryable.Run()
return err
},
2,
)
require.NoError(t, err)
require.True(t, res)
})
}

type mockRetryableFn struct {
counter uint64
trigger uint64
}

func newMockRetryableFn(trigger uint64) mockRetryableFn {
return mockRetryableFn{
counter: 0,
trigger: trigger,
}
}

func (m *mockRetryableFn) Run() (bool, error) {
if m.counter == m.trigger {
return true, nil
}
m.counter++
return false, errors.New("error")
}

0 comments on commit 430fcaf

Please sign in to comment.