-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient_test.go
135 lines (118 loc) · 4.05 KB
/
client_test.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2022-2025 The sacloud/iaas-api-go Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package iaas
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
client "github.com/sacloud/api-client-go"
"github.com/stretchr/testify/require"
)
type dummyHandler struct {
called []time.Time
responseCode int
}
func (s *dummyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if s.responseCode == http.StatusMovedPermanently {
w.Header().Set("Location", "/index.html")
}
w.WriteHeader(s.responseCode)
switch s.responseCode {
case http.StatusMultipleChoices,
http.StatusMovedPermanently,
http.StatusFound,
http.StatusSeeOther,
http.StatusNotModified,
http.StatusUseProxy,
http.StatusTemporaryRedirect,
http.StatusPermanentRedirect:
s.responseCode = http.StatusOK
default:
s.called = append(s.called, time.Now())
}
}
func (s *dummyHandler) isRetried() bool {
return len(s.called) > 1
}
func TestClient_Do_CheckRetryWithContext(t *testing.T) {
caller := NewClientWithOptions(&client.Options{
RetryMax: 1,
RetryWaitMin: 1,
RetryWaitMax: 1,
})
t.Run("context.Canceled", func(t *testing.T) {
h := &dummyHandler{
responseCode: http.StatusServiceUnavailable,
}
dummyServer := httptest.NewServer(h)
defer dummyServer.Close()
ctx, cancel := context.WithCancel(context.Background())
// make ctx to Canceled
cancel()
caller.Do(ctx, http.MethodGet, dummyServer.URL, nil) //nolint
require.False(t, h.isRetried(), "don't retry when context was canceled")
})
t.Run("context.DeadlineExceeded", func(t *testing.T) {
h := &dummyHandler{
responseCode: http.StatusServiceUnavailable,
}
dummyServer := httptest.NewServer(h)
defer dummyServer.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
// make ctx to DeadlineExceeded
time.Sleep(time.Millisecond)
caller.Do(ctx, http.MethodGet, dummyServer.URL, nil) //nolint
require.False(t, h.isRetried(), "don't retry when context exceeded deadline")
})
}
func TestClient_RetryByStatusCode(t *testing.T) {
cases := []struct {
responseCode int
shouldRetry bool
}{
{responseCode: http.StatusOK, shouldRetry: false},
{responseCode: http.StatusCreated, shouldRetry: false},
{responseCode: http.StatusAccepted, shouldRetry: false},
{responseCode: http.StatusNoContent, shouldRetry: false},
{responseCode: http.StatusMovedPermanently, shouldRetry: false},
{responseCode: http.StatusFound, shouldRetry: false},
{responseCode: http.StatusBadRequest, shouldRetry: false},
{responseCode: http.StatusUnauthorized, shouldRetry: false},
{responseCode: http.StatusForbidden, shouldRetry: false},
{responseCode: http.StatusNotFound, shouldRetry: false},
{responseCode: http.StatusLocked, shouldRetry: true}, // Locked: 423
{responseCode: http.StatusInternalServerError, shouldRetry: false},
{responseCode: http.StatusBadGateway, shouldRetry: false},
{responseCode: http.StatusServiceUnavailable, shouldRetry: true},
{responseCode: http.StatusGatewayTimeout, shouldRetry: false},
}
caller := NewClientWithOptions(&client.Options{
RetryMax: 1,
RetryWaitMin: 1,
RetryWaitMax: 1,
})
for _, tt := range cases {
h := &dummyHandler{
responseCode: tt.responseCode,
}
dummyServer := httptest.NewServer(h)
caller.Do(context.Background(), http.MethodGet, dummyServer.URL, nil) //nolint
dummyServer.Close()
require.Equal(t, tt.shouldRetry, h.isRetried(),
"got unexpected retry status with status[%d]: expected:%t got:%t", tt.responseCode, tt.shouldRetry, h.isRetried())
}
}