Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events parsing v2 milestone 2 #338

Merged
merged 18 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Dockerfile_milestone1

This file was deleted.

135 changes: 135 additions & 0 deletions registry/exec/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package exec

import (
"errors"
"fmt"
"strings"
"time"
)

//go:generate mockery --name RetryableExecutor --structname RetryableExecutorMock --filename exec_mock.go --inpackage

type RetryableExecutor[T any] interface {
ExecWithFallback(execFn func() (T, error), fallbackFn func() error) (T, error)
}

type retryableExecutor[T any] struct {
opts *Opts
}

func NewRetryableExecutor[T any](opts ...OptsFn) RetryableExecutor[T] {
execOpts := NewDefaultExecOpts()

for _, opt := range opts {
opt(execOpts)
}

return &retryableExecutor[T]{
execOpts,
}
}

func (r *retryableExecutor[T]) ExecWithFallback(execFn func() (T, error), fallbackFn func() error) (res T, err error) {
if execFn == nil {
return res, ErrMissingExecFn
}

if fallbackFn == nil {
return res, ErrMissingFallbackFn
}

execErr := &Error{}

retryCount := uint(0)

for {
res, err = execFn()

if err == nil {
return res, nil
}

execErr.AddErr(fmt.Errorf("exec function error: %w", err))

if retryCount == r.opts.maxRetryCount {
return res, execErr
}

if err = fallbackFn(); err != nil && !r.opts.retryOnFallbackError {
execErr.AddErr(fmt.Errorf("fallback function error: %w", err))

return res, execErr
}

retryCount++

time.Sleep(r.opts.retryTimeout)
}
}

var (
ErrMissingExecFn = errors.New("no exec function provided")
ErrMissingFallbackFn = errors.New("no fallback function provided")
)

const (
defaultMaxRetryCount = 3
defaultErrTimeout = 0 * time.Second
defaultRetryOnFallbackError = true
)

type Opts struct {
maxRetryCount uint
retryTimeout time.Duration
retryOnFallbackError bool
}

func NewDefaultExecOpts() *Opts {
return &Opts{
maxRetryCount: defaultMaxRetryCount,
retryTimeout: defaultErrTimeout,
retryOnFallbackError: defaultRetryOnFallbackError,
}
}

type OptsFn func(opts *Opts)

func WithMaxRetryCount(maxCount uint) OptsFn {
return func(opts *Opts) {
if maxCount == 0 {
maxCount = defaultMaxRetryCount
}

opts.maxRetryCount = maxCount
}
}

func WithRetryTimeout(retryTimeout time.Duration) OptsFn {
return func(opts *Opts) {
opts.retryTimeout = retryTimeout
}
}

func WithRetryOnFallBackError(retryOnFallbackError bool) OptsFn {
return func(opts *Opts) {
opts.retryOnFallbackError = retryOnFallbackError
}
}

type Error struct {
errs []error
}

func (e *Error) AddErr(err error) {
e.errs = append(e.errs, err)
}

func (e *Error) Error() string {
sb := strings.Builder{}

for i, err := range e.errs {
sb.WriteString(fmt.Sprintf("error %d: %s\n", i, err))
}

return sb.String()
}
46 changes: 46 additions & 0 deletions registry/exec/exec_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

153 changes: 153 additions & 0 deletions registry/exec/exec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package exec

import (
"errors"
"testing"
"time"

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

func TestRetryableExecutor_ExecWithFallback(t *testing.T) {
exec := NewRetryableExecutor[int]()

execFnCallCount := 0
fallbackFnCallCount := 0

execFnRes := 11

res, err := exec.ExecWithFallback(func() (int, error) {
execFnCallCount++

return execFnRes, nil
}, func() error {
fallbackFnCallCount++

return nil
})

assert.Nil(t, err)
assert.Equal(t, execFnRes, res)
assert.Equal(t, 1, execFnCallCount)
assert.Equal(t, 0, fallbackFnCallCount)
}

func TestRetryableExecutor_ExecWithFallback_RetrySuccess(t *testing.T) {
exec := NewRetryableExecutor[int]()

execFnCallCount := 0
fallbackFnCallCount := 0

execFnRes := 11

res, err := exec.ExecWithFallback(func() (int, error) {
execFnCallCount++

if execFnCallCount < 2 {
return 0, errors.New("boom")
}

return execFnRes, nil
}, func() error {
fallbackFnCallCount++

return nil
})

assert.Nil(t, err)
assert.Equal(t, execFnRes, res)
assert.Equal(t, 2, execFnCallCount)
assert.Equal(t, 1, fallbackFnCallCount)
}

func TestRetryableExecutor_ExecWithFallback_NilFns(t *testing.T) {
exec := NewRetryableExecutor[int]()

res, err := exec.ExecWithFallback(nil, nil)
assert.ErrorIs(t, err, ErrMissingExecFn)
assert.Equal(t, 0, res)

res, err = exec.ExecWithFallback(func() (int, error) {
return 1, nil
}, nil)
assert.ErrorIs(t, err, ErrMissingFallbackFn)
assert.Equal(t, 0, res)

}

func TestRetryableExecutor_ExecWithFallback_ExecFnError(t *testing.T) {
retryCount := uint(5)

exec := NewRetryableExecutor[int](
WithMaxRetryCount(retryCount),
WithRetryTimeout(100*time.Millisecond),
)

execFnCallCount := uint(0)
fallbackFnCallCount := uint(0)

res, err := exec.ExecWithFallback(func() (int, error) {
execFnCallCount++

return 0, errors.New("boom")
}, func() error {
fallbackFnCallCount++

return nil
})
assert.NotNil(t, err)
assert.Equal(t, 0, res)
assert.Equal(t, retryCount+1, execFnCallCount)
assert.Equal(t, retryCount, fallbackFnCallCount)

execErr := err.(*Error)
assert.Len(t, execErr.errs, int(retryCount+1))
}

func TestRetryableExecutor_ExecWithFallback_FallBackFnError(t *testing.T) {
exec := NewRetryableExecutor[int]()

execFnCallCount := 0
fallbackFnCallCount := 0

res, err := exec.ExecWithFallback(func() (int, error) {
execFnCallCount++

return 0, errors.New("boom")
}, func() error {
fallbackFnCallCount++

return errors.New("boom")
})
assert.NotNil(t, err)
assert.Equal(t, 0, res)
assert.Equal(t, defaultMaxRetryCount+1, execFnCallCount)
assert.Equal(t, defaultMaxRetryCount, fallbackFnCallCount)

execErr := err.(*Error)
assert.Len(t, execErr.errs, defaultMaxRetryCount+1)
}

func TestRetryableExecutor_ExecWithFallback_FallBackFnError_NoRetry(t *testing.T) {
exec := NewRetryableExecutor[int](WithRetryOnFallBackError(false))

execFnCallCount := 0
fallbackFnCallCount := 0

res, err := exec.ExecWithFallback(func() (int, error) {
execFnCallCount++

return 0, errors.New("boom")
}, func() error {
fallbackFnCallCount++

return errors.New("boom")
})
assert.NotNil(t, err)
assert.Equal(t, 0, res)
assert.Equal(t, 1, execFnCallCount)
assert.Equal(t, 1, fallbackFnCallCount)

execErr := err.(*Error)
assert.Len(t, execErr.errs, 2)
}
Loading