github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/fs/fserrors/error_syscall_test.go (about)

     1  //go:build !plan9
     2  // +build !plan9
     3  
     4  package fserrors
     5  
     6  import (
     7  	"fmt"
     8  	"net"
     9  	"net/url"
    10  	"os"
    11  	"syscall"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  // make a plausible network error with the underlying errno
    18  func makeNetErr(errno syscall.Errno) error {
    19  	return &net.OpError{
    20  		Op:     "write",
    21  		Net:    "tcp",
    22  		Source: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 123},
    23  		Addr:   &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8080},
    24  		Err: &os.SyscallError{
    25  			Syscall: "write",
    26  			Err:     errno,
    27  		},
    28  	}
    29  }
    30  
    31  func TestWithSyscallCause(t *testing.T) {
    32  	for i, test := range []struct {
    33  		err           error
    34  		wantRetriable bool
    35  		wantErr       error
    36  	}{
    37  		{makeNetErr(syscall.EAGAIN), true, syscall.EAGAIN},
    38  		{makeNetErr(syscall.Errno(123123123)), false, syscall.Errno(123123123)},
    39  	} {
    40  		gotRetriable, gotErr := Cause(test.err)
    41  		what := fmt.Sprintf("test #%d: %v", i, test.err)
    42  		assert.Equal(t, test.wantErr, gotErr, what)
    43  		assert.Equal(t, test.wantRetriable, gotRetriable, what)
    44  	}
    45  }
    46  
    47  func TestWithSyscallShouldRetry(t *testing.T) {
    48  	for i, test := range []struct {
    49  		err  error
    50  		want bool
    51  	}{
    52  		{makeNetErr(syscall.EAGAIN), true},
    53  		{makeNetErr(syscall.Errno(123123123)), false},
    54  		{
    55  			wrap(&url.Error{
    56  				Op:  "post",
    57  				URL: "http://localhost/",
    58  				Err: makeNetErr(syscall.EPIPE),
    59  			}, "potato error"),
    60  			true,
    61  		},
    62  		{
    63  			wrap(&url.Error{
    64  				Op:  "post",
    65  				URL: "http://localhost/",
    66  				Err: makeNetErr(syscall.Errno(123123123)),
    67  			}, "listing error"),
    68  			false,
    69  		},
    70  	} {
    71  		got := ShouldRetry(test.err)
    72  		assert.Equal(t, test.want, got, fmt.Sprintf("test #%d: %v", i, test.err))
    73  	}
    74  }