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

     1  package request
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"net/url"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/aavshr/aws-sdk-go/aws/awserr"
    13  )
    14  
    15  type testReader struct {
    16  	duration time.Duration
    17  	count    int
    18  }
    19  
    20  func (r *testReader) Read(b []byte) (int, error) {
    21  	if r.count > 0 {
    22  		r.count--
    23  		return len(b), nil
    24  	}
    25  	time.Sleep(r.duration)
    26  	return 0, io.EOF
    27  }
    28  
    29  func (r *testReader) Close() error {
    30  	return nil
    31  }
    32  
    33  func TestTimeoutReadCloser(t *testing.T) {
    34  	reader := timeoutReadCloser{
    35  		reader: &testReader{
    36  			duration: time.Second,
    37  			count:    5,
    38  		},
    39  		duration: time.Millisecond,
    40  	}
    41  	b := make([]byte, 100)
    42  	_, err := reader.Read(b)
    43  	if err != nil {
    44  		t.Log(err)
    45  	}
    46  }
    47  
    48  func TestTimeoutReadCloserSameDuration(t *testing.T) {
    49  	reader := timeoutReadCloser{
    50  		reader: &testReader{
    51  			duration: time.Millisecond,
    52  			count:    5,
    53  		},
    54  		duration: time.Millisecond,
    55  	}
    56  	b := make([]byte, 100)
    57  	_, err := reader.Read(b)
    58  	if err != nil {
    59  		t.Log(err)
    60  	}
    61  }
    62  
    63  func TestWithResponseReadTimeout(t *testing.T) {
    64  	r := Request{
    65  		HTTPRequest: &http.Request{
    66  			URL: &url.URL{},
    67  		},
    68  		HTTPResponse: &http.Response{
    69  			Body: ioutil.NopCloser(bytes.NewReader(nil)),
    70  		},
    71  	}
    72  	r.ApplyOptions(WithResponseReadTimeout(time.Second))
    73  	err := r.Send()
    74  	if err != nil {
    75  		t.Error(err)
    76  	}
    77  	v, ok := r.HTTPResponse.Body.(*timeoutReadCloser)
    78  	if !ok {
    79  		t.Error("Expected the body to be a timeoutReadCloser")
    80  	}
    81  	if v.duration != time.Second {
    82  		t.Errorf("Expected %v, but receive %v\n", time.Second, v.duration)
    83  	}
    84  }
    85  
    86  func TestAdaptToResponseTimeout(t *testing.T) {
    87  	testCases := []struct {
    88  		childErr         error
    89  		r                Request
    90  		expectedRootCode string
    91  	}{
    92  		{
    93  			childErr: awserr.New(ErrCodeResponseTimeout, "timeout!", nil),
    94  			r: Request{
    95  				Error: awserr.New("ErrTest", "FooBar", awserr.New(ErrCodeResponseTimeout, "timeout!", nil)),
    96  			},
    97  			expectedRootCode: ErrCodeResponseTimeout,
    98  		},
    99  		{
   100  			childErr: awserr.New(ErrCodeResponseTimeout+"1", "timeout!", nil),
   101  			r: Request{
   102  				Error: awserr.New("ErrTest", "FooBar", awserr.New(ErrCodeResponseTimeout+"1", "timeout!", nil)),
   103  			},
   104  			expectedRootCode: "ErrTest",
   105  		},
   106  		{
   107  			r: Request{
   108  				Error: awserr.New("ErrTest", "FooBar", nil),
   109  			},
   110  			expectedRootCode: "ErrTest",
   111  		},
   112  	}
   113  
   114  	for i, c := range testCases {
   115  		adaptToResponseTimeoutError(&c.r)
   116  		if aerr, ok := c.r.Error.(awserr.Error); !ok {
   117  			t.Errorf("Case %d: Expected 'awserr', but received %v", i+1, c.r.Error)
   118  		} else if aerr.Code() != c.expectedRootCode {
   119  			t.Errorf("Case %d: Expected %q, but received %s", i+1, c.expectedRootCode, aerr.Code())
   120  		}
   121  	}
   122  }