-
Notifications
You must be signed in to change notification settings - Fork 1
/
frontend_test.go
246 lines (199 loc) · 6.56 KB
/
frontend_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package httpcache_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"flamingo.me/flamingo/v3/framework/flamingo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"flamingo.me/httpcache/mocks"
"flamingo.me/httpcache"
)
var (
testKey = "testkey"
)
func createEntry(t *testing.T, lifeTime, graceTime string, tags []string, header map[string][]string, status string, statusCode int, body string) httpcache.Entry {
t.Helper()
lifeTimeDuration, err := time.ParseDuration(lifeTime)
require.NoError(t, err)
graceTimeDuration, err := time.ParseDuration(graceTime)
require.NoError(t, err)
return httpcache.Entry{
Meta: httpcache.Meta{
LifeTime: time.Now().Add(lifeTimeDuration),
GraceTime: time.Now().Add(graceTimeDuration),
Tags: tags,
},
Header: header,
Status: status,
StatusCode: statusCode,
Body: []byte(body),
}
}
func TestFrontend_Get(t *testing.T) {
t.Parallel()
defaultEntry := createEntry(t, "10s", "15s", nil, nil, "200 OK", http.StatusOK, "default")
defaultGraceEntry := createEntry(t, "-10s", "15s", nil, nil, "200 OK", http.StatusOK, "grace")
defaultOldEntry := createEntry(t, "-10s", "-15s", nil, nil, "200 OK", http.StatusOK, "old")
type args struct {
loader httpcache.HTTPLoader
}
tests := []struct {
name string
cacheGetter func() (httpcache.Entry, bool)
args args
want httpcache.Entry
wantSet *httpcache.Entry
wantErr bool
wantLoaderCalled bool
}{
{
name: "in cache and in lifetime",
cacheGetter: func() (httpcache.Entry, bool) { return defaultEntry, true },
want: defaultEntry,
wantErr: false,
wantLoaderCalled: false,
},
{
name: "not in cache",
cacheGetter: func() (httpcache.Entry, bool) { return httpcache.Entry{}, false },
args: args{
loader: func(_ context.Context) (httpcache.Entry, error) {
return defaultEntry, nil
}},
want: defaultEntry,
wantSet: &defaultEntry,
wantErr: false,
wantLoaderCalled: true,
},
{
name: "in cache but not in lifetime/gracetime",
cacheGetter: func() (httpcache.Entry, bool) { return defaultOldEntry, true },
args: args{
loader: func(_ context.Context) (httpcache.Entry, error) {
return defaultEntry, nil
}},
want: defaultEntry,
wantSet: &defaultEntry,
wantErr: false,
wantLoaderCalled: true,
},
{
name: "in cache, not in lifetime, in gracetime",
cacheGetter: func() (httpcache.Entry, bool) { return defaultGraceEntry, true },
args: args{
loader: func(_ context.Context) (httpcache.Entry, error) {
return defaultEntry, nil
}},
want: defaultGraceEntry,
wantSet: &defaultEntry,
wantErr: false,
wantLoaderCalled: true,
},
{
name: "not in cache, loader panics",
cacheGetter: func() (httpcache.Entry, bool) { return httpcache.Entry{}, false },
args: args{
loader: func(_ context.Context) (httpcache.Entry, error) {
panic("test panic")
}},
want: httpcache.Entry{},
wantErr: true,
wantLoaderCalled: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
wait := make(chan struct{}, 1)
loaderCalled := false
loader := func(ctx context.Context) (httpcache.Entry, error) {
loaderCalled = true
return test.args.loader(ctx)
}
backend := new(mocks.Backend)
if test.cacheGetter != nil {
backend.EXPECT().Get(testKey).Return(test.cacheGetter())
}
if test.wantSet != nil {
backend.EXPECT().Set(testKey, *test.wantSet).Run(func(key string, entry httpcache.Entry) {
wait <- struct{}{}
}).Return(nil).Once()
} else {
close(wait)
}
f := new(httpcache.Frontend).Inject(new(flamingo.NullLogger)).SetBackend(backend)
got, err := f.Get(context.Background(), testKey, loader)
// wait for eventually async cache set to be finished
<-wait
assert.Equal(t, test.wantLoaderCalled, loaderCalled, "Loader expected to be called: %v, but actually called: %v", test.wantLoaderCalled, loaderCalled)
backend.AssertExpectations(t)
if (err != nil) != test.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, test.wantErr)
return
}
assert.Equal(t, test.want, got)
})
}
}
func TestContextDeadlineExceeded(t *testing.T) {
t.Parallel()
t.Run("exceeded, throw error", func(t *testing.T) {
t.Parallel()
backend := new(mocks.Backend)
backend.EXPECT().Get(testKey).Return(func() (httpcache.Entry, bool) { return httpcache.Entry{}, false }())
backend.EXPECT().Set(mock.Anything, mock.Anything).Return(nil)
contextWithDeadline, cancel := context.WithDeadline(context.Background(), time.Now().Add(4*time.Second))
t.Cleanup(cancel)
f := new(httpcache.Frontend).Inject(new(flamingo.NullLogger)).SetBackend(backend)
got, err := f.Get(contextWithDeadline, testKey, loaderWithWaitingTime)
assert.ErrorIs(t, err, context.DeadlineExceeded)
assert.Equal(t, httpcache.Entry{}, got)
})
t.Run("did not exceed, no error", func(t *testing.T) {
t.Parallel()
backend := new(mocks.Backend)
backend.EXPECT().Get(testKey).Return(func() (httpcache.Entry, bool) { return httpcache.Entry{}, false }())
backend.EXPECT().Set(mock.Anything, mock.Anything).Return(nil)
contextWithDeadline, cancel := context.WithDeadline(context.Background(), time.Now().Add(6*time.Second))
t.Cleanup(cancel)
f := new(httpcache.Frontend).Inject(new(flamingo.NullLogger)).SetBackend(backend)
got, err := f.Get(contextWithDeadline, testKey, loaderWithWaitingTime)
assert.NoError(t, err)
assert.Equal(t, []byte("body"), got.Body)
})
}
func loaderWithWaitingTime(ctx context.Context) (httpcache.Entry, error) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(5 * time.Second)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Test 123"))
}))
defer server.Close()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL, nil)
if err != nil {
return httpcache.Entry{}, err
}
client := &http.Client{}
resp, err := client.Do(req)
if resp != nil {
_ = resp.Body.Close()
}
if err != nil {
return httpcache.Entry{}, err
}
return httpcache.Entry{
Meta: httpcache.Meta{
LifeTime: time.Now().Add(10),
GraceTime: time.Now().Add(15),
Tags: nil,
},
Header: nil,
Status: "200 OK",
StatusCode: http.StatusOK,
Body: []byte("body"),
}, nil
}