File: connection_reset_error_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.36.33-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, bullseye-backports
  • size: 163,900 kB
  • sloc: makefile: 182
file content (132 lines) | stat: -rw-r--r-- 3,445 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// +build go1.7

package request_test

import (
	"net/http"
	"reflect"
	"testing"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/client"
	"github.com/aws/aws-sdk-go/aws/client/metadata"
	"github.com/aws/aws-sdk-go/aws/corehandlers"
	"github.com/aws/aws-sdk-go/aws/request"
	v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
	"github.com/aws/aws-sdk-go/awstesting/unit"
	"github.com/aws/aws-sdk-go/private/protocol/jsonrpc"
)

type connResetCloser struct {
	Err error
}

func (rc *connResetCloser) Read(b []byte) (int, error) {
	return 0, rc.Err
}

func (rc *connResetCloser) Close() error {
	return nil
}

func TestSerializationErrConnectionReset_accept(t *testing.T) {
	cases := map[string]struct {
		Err            error
		ExpectAttempts int
	}{
		"accept with temporary": {
			Err:            errAcceptConnectionResetStub,
			ExpectAttempts: 6,
		},
		"read not temporary": {
			Err:            errReadConnectionResetStub,
			ExpectAttempts: 1,
		},
		"write with temporary": {
			Err:            errWriteConnectionResetStub,
			ExpectAttempts: 6,
		},
		"write broken pipe with temporary": {
			Err:            errWriteBrokenPipeStub,
			ExpectAttempts: 6,
		},
		"generic connection reset": {
			Err:            errConnectionResetStub,
			ExpectAttempts: 6,
		},
		"use of closed network connection": {
			Err:            errUseOfClosedConnectionStub,
			ExpectAttempts: 6,
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			count := 0
			handlers := request.Handlers{}
			handlers.Send.PushBack(func(r *request.Request) {
				count++
				r.HTTPResponse = &http.Response{}
				r.HTTPResponse.Body = &connResetCloser{
					Err: c.Err,
				}
			})

			handlers.Sign.PushBackNamed(v4.SignRequestHandler)
			handlers.Build.PushBackNamed(jsonrpc.BuildHandler)
			handlers.Unmarshal.PushBackNamed(jsonrpc.UnmarshalHandler)
			handlers.UnmarshalMeta.PushBackNamed(jsonrpc.UnmarshalMetaHandler)
			handlers.UnmarshalError.PushBackNamed(jsonrpc.UnmarshalErrorHandler)
			handlers.AfterRetry.PushBackNamed(corehandlers.AfterRetryHandler)

			op := &request.Operation{
				Name:       "op",
				HTTPMethod: "POST",
				HTTPPath:   "/",
			}

			meta := metadata.ClientInfo{
				ServiceName:   "fooService",
				SigningName:   "foo",
				SigningRegion: "foo",
				Endpoint:      "localhost",
				APIVersion:    "2001-01-01",
				JSONVersion:   "1.1",
				TargetPrefix:  "Foo",
			}
			cfg := unit.Session.Config.Copy()
			cfg.MaxRetries = aws.Int(5)
			cfg.SleepDelay = func(time.Duration) {}

			req := request.New(
				*cfg,
				meta,
				handlers,
				client.DefaultRetryer{NumMaxRetries: 5},
				op,
				&struct{}{},
				&struct{}{},
			)

			osErr := c.Err
			req.ApplyOptions(request.WithResponseReadTimeout(time.Second))
			err := req.Send()
			if err == nil {
				t.Error("Expected error 'SerializationError', but received nil")
			}
			if aerr, ok := err.(awserr.Error); ok && aerr.Code() != request.ErrCodeSerialization {
				t.Errorf("Expected 'SerializationError', but received %q", aerr.Code())
			} else if !ok {
				t.Errorf("Expected 'awserr.Error', but received %v", reflect.TypeOf(err))
			} else if aerr.OrigErr().Error() != osErr.Error() {
				t.Errorf("Expected %q, but received %q", osErr.Error(), aerr.OrigErr().Error())
			}

			if e, a := c.ExpectAttempts, count; e != a {
				t.Errorf("Expected %v, but received %v", e, a)
			}
		})
	}
}