Skip to content

Commit 41c5fe2

Browse files
KarolNowakkKarol Nowak
andauthored
fix(cache): pass deadline to context in load function (#440)
* feat(cache): pass deadline to context in load function * chore: fix tests * chore: fix lints * chore: fix lints * chore: fix lints * chore: remove context from test --------- Co-authored-by: Karol Nowak <[email protected]>
1 parent 9d26eaf commit 41c5fe2

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

core/cache/httpFrontend.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ func (hf *HTTPFrontend) load(ctx context.Context, key string, loader HTTPLoader,
114114
oldSpan := trace.FromContext(ctx)
115115
newContext := trace.NewContext(context.Background(), oldSpan)
116116

117+
deadline, hasDeadline := ctx.Deadline()
118+
if hasDeadline {
119+
var cancel context.CancelFunc
120+
121+
newContext, cancel = context.WithDeadline(newContext, deadline)
122+
123+
defer cancel()
124+
}
125+
117126
newContextWithSpan, span := trace.StartSpan(newContext, "flamingo/cache/httpFrontend/load")
118127
span.Annotate(nil, key)
119128
defer span.End()

core/cache/httpFrontend_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10+
"net/http/httptest"
1011
"strings"
1112
"testing"
1213
"time"
@@ -120,6 +121,27 @@ func createResponse(statusCode int, body string) *http.Response {
120121
return response
121122
}
122123

124+
func loaderWithWatingTime(ctx context.Context) (*http.Response, *Meta, error) {
125+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
126+
time.Sleep(5 * time.Second)
127+
w.WriteHeader(http.StatusOK)
128+
129+
_, _ = w.Write([]byte("Test 123"))
130+
}))
131+
132+
defer server.Close()
133+
134+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL, nil)
135+
if err != nil {
136+
return nil, nil, err
137+
}
138+
139+
client := &http.Client{}
140+
resp, err := client.Do(req)
141+
142+
return resp, nil, err
143+
}
144+
123145
func TestHTTPFrontend_Get(t *testing.T) {
124146
// wait channel top check async cache setting
125147
cacheSetComplete := make(chan struct{}, 1)
@@ -279,3 +301,70 @@ func TestHTTPFrontend_Get(t *testing.T) {
279301
})
280302
}
281303
}
304+
305+
//nolint:bodyclose // response might be nil so we cannot close the body
306+
func TestContextDeadlineExceeded(t *testing.T) {
307+
t.Parallel()
308+
309+
t.Run("exceeded, throw error", func(t *testing.T) {
310+
t.Parallel()
311+
312+
entry := &Entry{
313+
Meta: Meta{
314+
lifetime: time.Now().Add(-24 * time.Hour),
315+
gracetime: time.Now().Add(-24 * time.Hour),
316+
},
317+
Data: nil,
318+
}
319+
320+
backendMock := &MockBackend{}
321+
backendMock.On("Get", "test").Return(entry, true)
322+
backendMock.On("Set", "test", mock.Anything).Return(func(string, *Entry) error {
323+
return nil
324+
})
325+
326+
contextWithDeadline, cancel := context.WithDeadline(context.Background(), time.Now().Add(4*time.Second))
327+
t.Cleanup(cancel)
328+
329+
hf := new(HTTPFrontend).Inject(
330+
backendMock,
331+
&flamingo.NullLogger{},
332+
)
333+
334+
got, err := hf.Get(contextWithDeadline, "test", loaderWithWatingTime)
335+
336+
assert.ErrorIs(t, err, context.DeadlineExceeded)
337+
assert.Nil(t, got)
338+
})
339+
340+
t.Run("did not exceed, no error", func(t *testing.T) {
341+
t.Parallel()
342+
343+
entry := &Entry{
344+
Meta: Meta{
345+
lifetime: time.Now().Add(-24 * time.Hour),
346+
gracetime: time.Now().Add(-24 * time.Hour),
347+
},
348+
Data: nil,
349+
}
350+
351+
backendMock := &MockBackend{}
352+
backendMock.On("Get", "test").Return(entry, true)
353+
backendMock.On("Set", "test", mock.Anything).Return(func(string, *Entry) error {
354+
return nil
355+
})
356+
357+
contextWithDeadline, cancel := context.WithDeadline(context.Background(), time.Now().Add(7*time.Second))
358+
t.Cleanup(cancel)
359+
360+
hf := new(HTTPFrontend).Inject(
361+
backendMock,
362+
&flamingo.NullLogger{},
363+
)
364+
365+
got, err := hf.Get(contextWithDeadline, "test", loaderWithWatingTime)
366+
367+
assert.NoError(t, err)
368+
assert.Equal(t, got.StatusCode, http.StatusOK)
369+
})
370+
}

0 commit comments

Comments
 (0)