|
7 | 7 | "fmt"
|
8 | 8 | "io"
|
9 | 9 | "net/http"
|
| 10 | + "net/http/httptest" |
10 | 11 | "strings"
|
11 | 12 | "testing"
|
12 | 13 | "time"
|
@@ -120,6 +121,27 @@ func createResponse(statusCode int, body string) *http.Response {
|
120 | 121 | return response
|
121 | 122 | }
|
122 | 123 |
|
| 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 | + |
123 | 145 | func TestHTTPFrontend_Get(t *testing.T) {
|
124 | 146 | // wait channel top check async cache setting
|
125 | 147 | cacheSetComplete := make(chan struct{}, 1)
|
@@ -279,3 +301,70 @@ func TestHTTPFrontend_Get(t *testing.T) {
|
279 | 301 | })
|
280 | 302 | }
|
281 | 303 | }
|
| 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