github.com/pelicanplatform/pelican@v1.0.5/client/errorAccum_test.go (about) 1 /*************************************************************** 2 * 3 * Copyright (C) 2023, University of Nebraska-Lincoln 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); you 6 * may not use this file except in compliance with the License. You may 7 * obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ***************************************************************/ 18 19 package client 20 21 import ( 22 "errors" 23 "net/url" 24 "testing" 25 26 "github.com/stretchr/testify/assert" 27 ) 28 29 // TestErrorAccum tests simple adding and removing from the accumulator 30 func TestErrorAccum(t *testing.T) { 31 bunchOfErrors = make([]TimestampedError, 0) 32 defer func() { 33 bunchOfErrors = make([]TimestampedError, 0) 34 }() 35 // Case 1: cache with http 36 err := errors.New("error1") 37 err2 := errors.New("error2") 38 AddError(err) 39 AddError(err2) 40 41 errStr := GetErrors() 42 assert.Regexp(t, `Attempt\ \#2:\ error2\ \(0s\ elapsed,\ [0-9]+m?s\ since\ start\);\ Attempt\ \#1:\ error1\ \([0-9]+m?s\ since\ start\)`, errStr) 43 44 } 45 46 // TestErrorsRetryableFalse tests that errors are not retryable 47 func TestErrorsRetryableFalse(t *testing.T) { 48 bunchOfErrors = make([]TimestampedError, 0) 49 defer func() { 50 bunchOfErrors = make([]TimestampedError, 0) 51 }() 52 // Case 2: cache with http 53 AddError(&SlowTransferError{}) 54 AddError(&SlowTransferError{}) 55 assert.True(t, ErrorsRetryable(), "ErrorsRetryable should be true") 56 57 AddError(&ConnectionSetupError{}) 58 assert.True(t, ErrorsRetryable(), "ErrorsRetryable should be true") 59 60 // Now add a non-retryable error 61 AddError(errors.New("Non retryable error")) 62 assert.False(t, ErrorsRetryable(), "ErrorsRetryable should be false") 63 64 } 65 66 // TestErrorsRetryableTrue tests that errors are retryable 67 func TestErrorsRetryableTrue(t *testing.T) { 68 bunchOfErrors = make([]TimestampedError, 0) 69 defer func() { 70 bunchOfErrors = make([]TimestampedError, 0) 71 }() 72 // Try with a retryable error nested error 73 AddError(&url.Error{Err: &SlowTransferError{}}) 74 assert.True(t, ErrorsRetryable(), "ErrorsRetryable should be true") 75 76 AddError(&ConnectionSetupError{}) 77 assert.True(t, ErrorsRetryable(), "ErrorsRetryable should be true") 78 79 }