github.com/aavshr/aws-sdk-go@v1.41.3/aws/request/connection_reset_error_test.go (about)

     1  //go:build go1.7
     2  // +build go1.7
     3  
     4  package request_test
     5  
     6  import (
     7  	"net/http"
     8  	"reflect"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/aavshr/aws-sdk-go/aws"
    13  	"github.com/aavshr/aws-sdk-go/aws/awserr"
    14  	"github.com/aavshr/aws-sdk-go/aws/client"
    15  	"github.com/aavshr/aws-sdk-go/aws/client/metadata"
    16  	"github.com/aavshr/aws-sdk-go/aws/corehandlers"
    17  	"github.com/aavshr/aws-sdk-go/aws/request"
    18  	v4 "github.com/aavshr/aws-sdk-go/aws/signer/v4"
    19  	"github.com/aavshr/aws-sdk-go/awstesting/unit"
    20  	"github.com/aavshr/aws-sdk-go/private/protocol/jsonrpc"
    21  )
    22  
    23  type connResetCloser struct {
    24  	Err error
    25  }
    26  
    27  func (rc *connResetCloser) Read(b []byte) (int, error) {
    28  	return 0, rc.Err
    29  }
    30  
    31  func (rc *connResetCloser) Close() error {
    32  	return nil
    33  }
    34  
    35  func TestSerializationErrConnectionReset_accept(t *testing.T) {
    36  	cases := map[string]struct {
    37  		Err            error
    38  		ExpectAttempts int
    39  	}{
    40  		"accept with temporary": {
    41  			Err:            errAcceptConnectionResetStub,
    42  			ExpectAttempts: 6,
    43  		},
    44  		"read not temporary": {
    45  			Err:            errReadConnectionResetStub,
    46  			ExpectAttempts: 1,
    47  		},
    48  		"write with temporary": {
    49  			Err:            errWriteConnectionResetStub,
    50  			ExpectAttempts: 6,
    51  		},
    52  		"write broken pipe with temporary": {
    53  			Err:            errWriteBrokenPipeStub,
    54  			ExpectAttempts: 6,
    55  		},
    56  		"generic connection reset": {
    57  			Err:            errConnectionResetStub,
    58  			ExpectAttempts: 6,
    59  		},
    60  		"use of closed network connection": {
    61  			Err:            errUseOfClosedConnectionStub,
    62  			ExpectAttempts: 6,
    63  		},
    64  	}
    65  
    66  	for name, c := range cases {
    67  		t.Run(name, func(t *testing.T) {
    68  			count := 0
    69  			handlers := request.Handlers{}
    70  			handlers.Send.PushBack(func(r *request.Request) {
    71  				count++
    72  				r.HTTPResponse = &http.Response{}
    73  				r.HTTPResponse.Body = &connResetCloser{
    74  					Err: c.Err,
    75  				}
    76  			})
    77  
    78  			handlers.Sign.PushBackNamed(v4.SignRequestHandler)
    79  			handlers.Build.PushBackNamed(jsonrpc.BuildHandler)
    80  			handlers.Unmarshal.PushBackNamed(jsonrpc.UnmarshalHandler)
    81  			handlers.UnmarshalMeta.PushBackNamed(jsonrpc.UnmarshalMetaHandler)
    82  			handlers.UnmarshalError.PushBackNamed(jsonrpc.UnmarshalErrorHandler)
    83  			handlers.AfterRetry.PushBackNamed(corehandlers.AfterRetryHandler)
    84  
    85  			op := &request.Operation{
    86  				Name:       "op",
    87  				HTTPMethod: "POST",
    88  				HTTPPath:   "/",
    89  			}
    90  
    91  			meta := metadata.ClientInfo{
    92  				ServiceName:   "fooService",
    93  				SigningName:   "foo",
    94  				SigningRegion: "foo",
    95  				Endpoint:      "localhost",
    96  				APIVersion:    "2001-01-01",
    97  				JSONVersion:   "1.1",
    98  				TargetPrefix:  "Foo",
    99  			}
   100  			cfg := unit.Session.Config.Copy()
   101  			cfg.MaxRetries = aws.Int(5)
   102  			cfg.SleepDelay = func(time.Duration) {}
   103  
   104  			req := request.New(
   105  				*cfg,
   106  				meta,
   107  				handlers,
   108  				client.DefaultRetryer{NumMaxRetries: 5},
   109  				op,
   110  				&struct{}{},
   111  				&struct{}{},
   112  			)
   113  
   114  			osErr := c.Err
   115  			req.ApplyOptions(request.WithResponseReadTimeout(time.Second))
   116  			err := req.Send()
   117  			if err == nil {
   118  				t.Error("Expected error 'SerializationError', but received nil")
   119  			}
   120  			if aerr, ok := err.(awserr.Error); ok && aerr.Code() != request.ErrCodeSerialization {
   121  				t.Errorf("Expected 'SerializationError', but received %q", aerr.Code())
   122  			} else if !ok {
   123  				t.Errorf("Expected 'awserr.Error', but received %v", reflect.TypeOf(err))
   124  			} else if aerr.OrigErr().Error() != osErr.Error() {
   125  				t.Errorf("Expected %q, but received %q", osErr.Error(), aerr.OrigErr().Error())
   126  			}
   127  
   128  			if e, a := c.ExpectAttempts, count; e != a {
   129  				t.Errorf("Expected %v, but received %v", e, a)
   130  			}
   131  		})
   132  	}
   133  }