1
2
3 package ecrpublic
4
5 import (
6 "context"
7 "github.com/aws/aws-sdk-go-v2/aws"
8 "github.com/aws/smithy-go/middleware"
9 smithyhttp "github.com/aws/smithy-go/transport/http"
10 "io/ioutil"
11 "net/http"
12 "strings"
13 "testing"
14 )
15
16 func TestClient_resolveRetryOptions(t *testing.T) {
17 nopClient := smithyhttp.ClientDoFunc(func(_ *http.Request) (*http.Response, error) {
18 return &http.Response{
19 StatusCode: 200,
20 Header: http.Header{},
21 Body: ioutil.NopCloser(strings.NewReader("")),
22 }, nil
23 })
24
25 cases := map[string]struct {
26 defaultsMode aws.DefaultsMode
27 retryer aws.Retryer
28 retryMaxAttempts int
29 opRetryMaxAttempts *int
30 retryMode aws.RetryMode
31 expectClientRetryMode aws.RetryMode
32 expectClientMaxAttempts int
33 expectOpMaxAttempts int
34 }{
35 "defaults": {
36 defaultsMode: aws.DefaultsModeStandard,
37 expectClientRetryMode: aws.RetryModeStandard,
38 expectClientMaxAttempts: 3,
39 expectOpMaxAttempts: 3,
40 },
41 "custom default retry": {
42 retryMode: aws.RetryModeAdaptive,
43 retryMaxAttempts: 10,
44 expectClientRetryMode: aws.RetryModeAdaptive,
45 expectClientMaxAttempts: 10,
46 expectOpMaxAttempts: 10,
47 },
48 "custom op max attempts": {
49 retryMode: aws.RetryModeAdaptive,
50 retryMaxAttempts: 10,
51 opRetryMaxAttempts: aws.Int(2),
52 expectClientRetryMode: aws.RetryModeAdaptive,
53 expectClientMaxAttempts: 10,
54 expectOpMaxAttempts: 2,
55 },
56 "custom op no change max attempts": {
57 retryMode: aws.RetryModeAdaptive,
58 retryMaxAttempts: 10,
59 opRetryMaxAttempts: aws.Int(10),
60 expectClientRetryMode: aws.RetryModeAdaptive,
61 expectClientMaxAttempts: 10,
62 expectOpMaxAttempts: 10,
63 },
64 "custom op 0 max attempts": {
65 retryMode: aws.RetryModeAdaptive,
66 retryMaxAttempts: 10,
67 opRetryMaxAttempts: aws.Int(0),
68 expectClientRetryMode: aws.RetryModeAdaptive,
69 expectClientMaxAttempts: 10,
70 expectOpMaxAttempts: 10,
71 },
72 }
73
74 for name, c := range cases {
75 t.Run(name, func(t *testing.T) {
76 client := NewFromConfig(aws.Config{
77 DefaultsMode: c.defaultsMode,
78 Retryer: func() func() aws.Retryer {
79 if c.retryer == nil {
80 return nil
81 }
82
83 return func() aws.Retryer { return c.retryer }
84 }(),
85 HTTPClient: nopClient,
86 RetryMaxAttempts: c.retryMaxAttempts,
87 RetryMode: c.retryMode,
88 })
89
90 if e, a := c.expectClientRetryMode, client.options.RetryMode; e != a {
91 t.Errorf("expect %v retry mode, got %v", e, a)
92 }
93 if e, a := c.expectClientMaxAttempts, client.options.Retryer.MaxAttempts(); e != a {
94 t.Errorf("expect %v max attempts, got %v", e, a)
95 }
96
97 _, _, err := client.invokeOperation(context.Background(), "mockOperation", struct{}{},
98 []func(*Options){
99 func(o *Options) {
100 if c.opRetryMaxAttempts == nil {
101 return
102 }
103 o.RetryMaxAttempts = *c.opRetryMaxAttempts
104 },
105 },
106 func(s *middleware.Stack, o Options) error {
107 s.Initialize.Clear()
108 s.Serialize.Clear()
109 s.Build.Clear()
110 s.Finalize.Clear()
111 s.Deserialize.Clear()
112
113 if e, a := c.expectOpMaxAttempts, o.Retryer.MaxAttempts(); e != a {
114 t.Errorf("expect %v op max attempts, got %v", e, a)
115 }
116 return nil
117 })
118 if err != nil {
119 t.Fatalf("expect no operation error, got %v", err)
120 }
121 })
122 }
123 }
124
View as plain text