github.com/argoproj/argo-events@v1.9.1/common/retry_test.go (about) 1 /* 2 Copyright 2018 BlackRock, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package common 17 18 import ( 19 "fmt" 20 "strings" 21 "testing" 22 "time" 23 24 "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" 25 "github.com/stretchr/testify/assert" 26 "k8s.io/apimachinery/pkg/api/errors" 27 "k8s.io/apimachinery/pkg/util/wait" 28 29 apicommon "github.com/argoproj/argo-events/pkg/apis/common" 30 ) 31 32 func TestRetryableKubeAPIError(t *testing.T) { 33 errUnAuth := errors.NewUnauthorized("reason") 34 errNotFound := errors.NewNotFound(v1alpha1.Resource("sensor"), "hello") 35 errForbidden := errors.NewForbidden(v1alpha1.Resource("sensor"), "hello", nil) 36 errInvalid := errors.NewInvalid(v1alpha1.Kind("core/data"), "hello", nil) 37 errMethodNotSupported := errors.NewMethodNotSupported(v1alpha1.Resource("sensor"), "action") 38 39 assert.True(t, IsRetryableKubeAPIError(errUnAuth)) 40 assert.False(t, IsRetryableKubeAPIError(errNotFound)) 41 assert.False(t, IsRetryableKubeAPIError(errForbidden)) 42 assert.False(t, IsRetryableKubeAPIError(errInvalid)) 43 assert.False(t, IsRetryableKubeAPIError(errMethodNotSupported)) 44 } 45 46 func TestConnect(t *testing.T) { 47 err := DoWithRetry(nil, func() error { 48 return fmt.Errorf("new error") 49 }) 50 assert.NotNil(t, err) 51 assert.True(t, strings.Contains(err.Error(), "new error")) 52 53 err = DoWithRetry(nil, func() error { 54 return nil 55 }) 56 assert.Nil(t, err) 57 } 58 59 func TestConnectDurationString(t *testing.T) { 60 start := time.Now() 61 count := 2 62 err := DoWithRetry(nil, func() error { 63 if count == 0 { 64 return nil 65 } else { 66 count-- 67 return fmt.Errorf("new error") 68 } 69 }) 70 end := time.Now() 71 elapsed := end.Sub(start) 72 assert.NoError(t, err) 73 assert.Equal(t, 0, count) 74 assert.True(t, elapsed >= 2*time.Second) 75 } 76 77 func TestConnectRetry(t *testing.T) { 78 factor := apicommon.NewAmount("1.0") 79 jitter := apicommon.NewAmount("1") 80 duration := apicommon.FromInt64(1000000000) 81 backoff := apicommon.Backoff{ 82 Duration: &duration, 83 Factor: &factor, 84 Jitter: &jitter, 85 Steps: 5, 86 } 87 count := 2 88 start := time.Now() 89 err := DoWithRetry(&backoff, func() error { 90 if count == 0 { 91 return nil 92 } else { 93 count-- 94 return fmt.Errorf("new error") 95 } 96 }) 97 end := time.Now() 98 elapsed := end.Sub(start) 99 assert.NoError(t, err) 100 assert.Equal(t, 0, count) 101 assert.True(t, elapsed >= 2*time.Second) 102 } 103 104 func TestRetryFailure(t *testing.T) { 105 factor := apicommon.NewAmount("1.0") 106 jitter := apicommon.NewAmount("1") 107 duration := apicommon.FromString("1s") 108 backoff := apicommon.Backoff{ 109 Duration: &duration, 110 Factor: &factor, 111 Jitter: &jitter, 112 Steps: 2, 113 } 114 err := DoWithRetry(&backoff, func() error { 115 return fmt.Errorf("this is an error") 116 }) 117 assert.NotNil(t, err) 118 assert.Contains(t, err.Error(), "after retries") 119 assert.Contains(t, err.Error(), "this is an error") 120 } 121 122 func TestConvert2WaitBackoff(t *testing.T) { 123 factor := apicommon.NewAmount("1.0") 124 jitter := apicommon.NewAmount("1") 125 duration := apicommon.FromString("1s") 126 backoff := apicommon.Backoff{ 127 Duration: &duration, 128 Factor: &factor, 129 Jitter: &jitter, 130 Steps: 2, 131 } 132 waitBackoff, err := Convert2WaitBackoff(&backoff) 133 assert.NoError(t, err) 134 assert.Equal(t, wait.Backoff{ 135 Duration: 1 * time.Second, 136 Factor: 1.0, 137 Jitter: 1.0, 138 Steps: 2, 139 }, *waitBackoff) 140 }