github.com/jfrog/jfrog-client-go@v1.40.2/utils/retryexecutor_test.go (about) 1 package utils 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "github.com/jfrog/jfrog-client-go/utils/log" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestRetryExecutorSuccess(t *testing.T) { 13 retriesToPerform := 10 14 breakRetriesAt := 4 15 runCount := 0 16 executor := RetryExecutor{ 17 MaxRetries: retriesToPerform, 18 RetriesIntervalMilliSecs: 0, 19 ErrorMessage: "Testing RetryExecutor", 20 ExecutionHandler: func() (bool, error) { 21 runCount++ 22 if runCount == breakRetriesAt { 23 log.Warn("Breaking after", runCount-1, "retries") 24 return false, nil 25 } 26 return true, nil 27 }, 28 } 29 30 assert.NoError(t, executor.Execute()) 31 assert.Equal(t, breakRetriesAt, runCount) 32 } 33 34 func TestRetryExecutorTimeoutWithDefaultError(t *testing.T) { 35 retriesToPerform := 5 36 runCount := 0 37 38 executor := RetryExecutor{ 39 MaxRetries: retriesToPerform, 40 RetriesIntervalMilliSecs: 0, 41 ErrorMessage: "Testing RetryExecutor", 42 ExecutionHandler: func() (bool, error) { 43 runCount++ 44 return true, nil 45 }, 46 } 47 48 assert.Equal(t, executor.Execute(), RetryExecutorTimeoutError{executor.getTimeoutErrorMsg()}) 49 assert.Equal(t, retriesToPerform+1, runCount) 50 } 51 52 func TestRetryExecutorTimeoutWithCustomError(t *testing.T) { 53 retriesToPerform := 5 54 runCount := 0 55 56 executionHandler := errors.New("retry failed due to reason") 57 58 executor := RetryExecutor{ 59 MaxRetries: retriesToPerform, 60 RetriesIntervalMilliSecs: 0, 61 ErrorMessage: "Testing RetryExecutor", 62 ExecutionHandler: func() (bool, error) { 63 runCount++ 64 return true, executionHandler 65 }, 66 } 67 68 assert.Equal(t, executor.Execute(), executionHandler) 69 assert.Equal(t, retriesToPerform+1, runCount) 70 } 71 72 func TestRetryExecutorCancel(t *testing.T) { 73 retriesToPerform := 5 74 runCount := 0 75 76 retryContext, cancelFunc := context.WithCancel(context.Background()) 77 executor := RetryExecutor{ 78 Context: retryContext, 79 MaxRetries: retriesToPerform, 80 RetriesIntervalMilliSecs: 0, 81 ErrorMessage: "Testing RetryExecutor", 82 ExecutionHandler: func() (bool, error) { 83 runCount++ 84 return true, nil 85 }, 86 } 87 88 cancelFunc() 89 assert.EqualError(t, executor.Execute(), context.Canceled.Error()) 90 assert.Equal(t, 1, runCount) 91 }