github.com/htcondor/osdf-client/v6@v6.13.0-rc1.0.20231009141709-766e7b4d1dc8/errorAccum_test.go (about)

     1  package stashcp
     2  
     3  import (
     4  	"errors"
     5  	"net/url"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  //  TestErrorAccum tests simple adding and removing from the accumulator
    12  func TestErrorAccum(t *testing.T) {
    13  	bunchOfErrors = make([]TimestampedError, 0)
    14  	defer func() {
    15  		bunchOfErrors = make([]TimestampedError, 0)
    16  	}()
    17  	// Case 1: cache with http
    18  	err := errors.New("error1")
    19  	err2 := errors.New("error2")
    20  	AddError(err)
    21  	AddError(err2)
    22  
    23  	errStr := GetErrors()
    24  	assert.Equal(t, "Attempt #2: error2 (0s elapsed, 0s since start); Attempt #1: error1 (0s since start)", errStr)
    25  
    26  }
    27  
    28  // TestErrorsRetryableFalse tests that errors are not retryable
    29  func TestErrorsRetryableFalse(t *testing.T) {
    30  	bunchOfErrors = make([]TimestampedError, 0)
    31  	defer func() {
    32  		bunchOfErrors = make([]TimestampedError, 0)
    33  	}()
    34  	// Case 2: cache with http
    35  	AddError(&SlowTransferError{})
    36  	AddError(&SlowTransferError{})
    37  	assert.True(t, ErrorsRetryable(), "ErrorsRetryable should be true")
    38  
    39  	AddError(&ConnectionSetupError{})
    40  	assert.True(t, ErrorsRetryable(), "ErrorsRetryable should be true")
    41  
    42  	// Now add a non-retryable error
    43  	AddError(errors.New("Non retryable error"))
    44  	assert.False(t, ErrorsRetryable(), "ErrorsRetryable should be false")
    45  
    46  }
    47  
    48  // TestErrorsRetryableTrue tests that errors are retryable
    49  func TestErrorsRetryableTrue(t *testing.T) {
    50  	bunchOfErrors = make([]TimestampedError, 0)
    51  	defer func() {
    52  		bunchOfErrors = make([]TimestampedError, 0)
    53  	}()
    54  	// Try with a retryable error nested error
    55  	AddError(&url.Error{Err: &SlowTransferError{}})
    56  	assert.True(t, ErrorsRetryable(), "ErrorsRetryable should be true")
    57  
    58  	AddError(&ConnectionSetupError{})
    59  	assert.True(t, ErrorsRetryable(), "ErrorsRetryable should be true")
    60  
    61  }