File: timeout_read_closer_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 (122 lines) | stat: -rw-r--r-- 2,571 bytes parent folder | download | duplicates (3)
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
package request

import (
	"bytes"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"testing"
	"time"

	"github.com/aws/aws-sdk-go/aws/awserr"
)

type testReader struct {
	duration time.Duration
	count    int
}

func (r *testReader) Read(b []byte) (int, error) {
	if r.count > 0 {
		r.count--
		return len(b), nil
	}
	time.Sleep(r.duration)
	return 0, io.EOF
}

func (r *testReader) Close() error {
	return nil
}

func TestTimeoutReadCloser(t *testing.T) {
	reader := timeoutReadCloser{
		reader: &testReader{
			duration: time.Second,
			count:    5,
		},
		duration: time.Millisecond,
	}
	b := make([]byte, 100)
	_, err := reader.Read(b)
	if err != nil {
		t.Log(err)
	}
}

func TestTimeoutReadCloserSameDuration(t *testing.T) {
	reader := timeoutReadCloser{
		reader: &testReader{
			duration: time.Millisecond,
			count:    5,
		},
		duration: time.Millisecond,
	}
	b := make([]byte, 100)
	_, err := reader.Read(b)
	if err != nil {
		t.Log(err)
	}
}

func TestWithResponseReadTimeout(t *testing.T) {
	r := Request{
		HTTPRequest: &http.Request{
			URL: &url.URL{},
		},
		HTTPResponse: &http.Response{
			Body: ioutil.NopCloser(bytes.NewReader(nil)),
		},
	}
	r.ApplyOptions(WithResponseReadTimeout(time.Second))
	err := r.Send()
	if err != nil {
		t.Error(err)
	}
	v, ok := r.HTTPResponse.Body.(*timeoutReadCloser)
	if !ok {
		t.Error("Expected the body to be a timeoutReadCloser")
	}
	if v.duration != time.Second {
		t.Errorf("Expected %v, but receive %v\n", time.Second, v.duration)
	}
}

func TestAdaptToResponseTimeout(t *testing.T) {
	testCases := []struct {
		childErr         error
		r                Request
		expectedRootCode string
	}{
		{
			childErr: awserr.New(ErrCodeResponseTimeout, "timeout!", nil),
			r: Request{
				Error: awserr.New("ErrTest", "FooBar", awserr.New(ErrCodeResponseTimeout, "timeout!", nil)),
			},
			expectedRootCode: ErrCodeResponseTimeout,
		},
		{
			childErr: awserr.New(ErrCodeResponseTimeout+"1", "timeout!", nil),
			r: Request{
				Error: awserr.New("ErrTest", "FooBar", awserr.New(ErrCodeResponseTimeout+"1", "timeout!", nil)),
			},
			expectedRootCode: "ErrTest",
		},
		{
			r: Request{
				Error: awserr.New("ErrTest", "FooBar", nil),
			},
			expectedRootCode: "ErrTest",
		},
	}

	for i, c := range testCases {
		adaptToResponseTimeoutError(&c.r)
		if aerr, ok := c.r.Error.(awserr.Error); !ok {
			t.Errorf("Case %d: Expected 'awserr', but received %v", i+1, c.r.Error)
		} else if aerr.Code() != c.expectedRootCode {
			t.Errorf("Case %d: Expected %q, but received %s", i+1, c.expectedRootCode, aerr.Code())
		}
	}
}