1
2
3 package sso
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 }, func(o *Options) {
89 if o.Retryer == nil {
90 t.Errorf("retryer must not be nil in functional options")
91 }
92 })
93
94 if e, a := c.expectClientRetryMode, client.options.RetryMode; e != a {
95 t.Errorf("expect %v retry mode, got %v", e, a)
96 }
97 if e, a := c.expectClientMaxAttempts, client.options.Retryer.MaxAttempts(); e != a {
98 t.Errorf("expect %v max attempts, got %v", e, a)
99 }
100
101 _, _, err := client.invokeOperation(context.Background(), "mockOperation", struct{}{},
102 []func(*Options){
103 func(o *Options) {
104 if c.opRetryMaxAttempts == nil {
105 return
106 }
107 o.RetryMaxAttempts = *c.opRetryMaxAttempts
108 },
109 },
110 func(s *middleware.Stack, o Options) error {
111 s.Initialize.Clear()
112 s.Serialize.Clear()
113 s.Build.Clear()
114 s.Finalize.Clear()
115 s.Deserialize.Clear()
116
117 if e, a := c.expectOpMaxAttempts, o.Retryer.MaxAttempts(); e != a {
118 t.Errorf("expect %v op max attempts, got %v", e, a)
119 }
120 return nil
121 })
122 if err != nil {
123 t.Fatalf("expect no operation error, got %v", err)
124 }
125 })
126 }
127 }
128
View as plain text