File: timeout_read_closer_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.30.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 662,428 kB
  • sloc: java: 16,875; makefile: 432; sh: 175
file content (103 lines) | stat: -rw-r--r-- 2,085 bytes parent folder | download | duplicates (5)
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
package http

import (
	"context"
	"errors"
	"io"
	"net/http"
	"testing"
	"time"

	"github.com/aws/smithy-go/middleware"
	smithyhttp "github.com/aws/smithy-go/transport/http"
)

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 TestResponseReadTimeoutMiddleware(t *testing.T) {
	reader := testReader{
		duration: time.Second,
		count:    0,
	}

	m := &readTimeout{duration: time.Millisecond}

	out, _, err := m.HandleDeserialize(context.Background(),
		middleware.DeserializeInput{},
		middleware.DeserializeHandlerFunc(
			func(ctx context.Context, input middleware.DeserializeInput) (
				out middleware.DeserializeOutput, metadata middleware.Metadata, err error,
			) {
				response := &smithyhttp.Response{
					Response: &http.Response{Body: &reader},
				}
				return middleware.DeserializeOutput{RawResponse: response}, metadata, err
			}),
	)

	if err != nil {
		t.Fatalf("expect no error, got %v", err)
	}

	response, ok := out.RawResponse.(*smithyhttp.Response)
	if !ok {
		t.Errorf("expect smithy response, got %T", out.RawResponse)
	}

	b := make([]byte, 100)
	_, err = response.Body.Read(b)
	if err != nil {
		readTimeoutError := &ResponseTimeoutError{}
		if !errors.As(err, &readTimeoutError) {
			t.Errorf("expect timeout error, got %v", err)
		}
	} else {
		t.Error("expect timeout error, got none")
	}
}