diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2021-12-07 08:19:30 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2021-12-08 04:21:29 +0300 |
commit | d7c9431546fc48a02f1e176b2ac21a6c8927542c (patch) | |
tree | 3d595a00a9fb4a257d21ee5425354995fc9a6434 | |
parent | c57f28159a32b325ec54f190b0f13c4676600d42 (diff) |
test: add slow req acceptance test
-rw-r--r-- | internal/source/gitlab/cache/cache.go | 4 | ||||
-rw-r--r-- | internal/source/gitlab/cache/cache_test.go | 4 | ||||
-rw-r--r-- | internal/source/gitlab/cache/retriever.go | 3 | ||||
-rw-r--r-- | test/acceptance/helpers_test.go | 5 | ||||
-rw-r--r-- | test/acceptance/serving_test.go | 30 |
5 files changed, 41 insertions, 5 deletions
diff --git a/internal/source/gitlab/cache/cache.go b/internal/source/gitlab/cache/cache.go index 7a81b3af..473c464c 100644 --- a/internal/source/gitlab/cache/cache.go +++ b/internal/source/gitlab/cache/cache.go @@ -2,7 +2,7 @@ package cache import ( "context" - "errors" + "fmt" "gitlab.com/gitlab-org/gitlab-pages/internal/config" "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api" @@ -100,7 +100,7 @@ func (c *Cache) retrieve(ctx context.Context, entry *Entry) *api.Lookup { var lookup *api.Lookup select { case <-ctx.Done(): - lookup = &api.Lookup{Name: entry.domain, Error: errors.New("context done")} + lookup = &api.Lookup{Name: entry.domain, Error: fmt.Errorf("context done: %w", ctx.Err())} case <-entry.retrieved: lookup = entry.Lookup() } diff --git a/internal/source/gitlab/cache/cache_test.go b/internal/source/gitlab/cache/cache_test.go index 2d3ada76..6eb48911 100644 --- a/internal/source/gitlab/cache/cache_test.go +++ b/internal/source/gitlab/cache/cache_test.go @@ -221,7 +221,7 @@ func TestResolve(t *testing.T) { lookup := cache.Resolve(context.Background(), "my.gitlab.com") require.Equal(t, 0, len(resolver.lookups)) - require.EqualError(t, lookup.Error, "retrieval context done") + require.EqualError(t, lookup.Error, "retrieval context done: "+context.DeadlineExceeded.Error()) }) }) @@ -235,7 +235,7 @@ func TestResolve(t *testing.T) { resolver.domain <- "err.gitlab.com" require.Equal(t, "my.gitlab.com", lookup.Name) - require.EqualError(t, lookup.Error, "context done") + require.EqualError(t, lookup.Error, "context done: "+context.Canceled.Error()) }) }) }) diff --git a/internal/source/gitlab/cache/retriever.go b/internal/source/gitlab/cache/retriever.go index 2f6c1f07..65c7f1a2 100644 --- a/internal/source/gitlab/cache/retriever.go +++ b/internal/source/gitlab/cache/retriever.go @@ -3,6 +3,7 @@ package cache import ( "context" "errors" + "fmt" "time" "gitlab.com/gitlab-org/labkit/correlation" @@ -47,7 +48,7 @@ func (r *Retriever) Retrieve(originalCtx context.Context, domain string) (lookup select { case <-ctx.Done(): logMsg = "retrieval context done" - lookup = api.Lookup{Error: errors.New(logMsg)} + lookup = api.Lookup{Error: fmt.Errorf(logMsg+": %w", ctx.Err())} case lookup = <-r.resolveWithBackoff(ctx, domain): logMsg = "retrieval response sent" } diff --git a/test/acceptance/helpers_test.go b/test/acceptance/helpers_test.go index ce2d47d3..769b0467 100644 --- a/test/acceptance/helpers_test.go +++ b/test/acceptance/helpers_test.go @@ -449,6 +449,7 @@ type stubOpts struct { pagesHandler http.HandlerFunc pagesStatusResponse int pagesRoot string + delay time.Duration } func NewGitlabDomainsSourceStub(t *testing.T, opts *stubOpts) *httptest.Server { @@ -529,6 +530,10 @@ func defaultAPIHandler(t *testing.T, opts *stubOpts) http.HandlerFunc { w.WriteHeader(http.StatusNoContent) return } + // to test slow responses from the API + if opts.delay > 0 { + time.Sleep(opts.delay) + } opts.setAPICalled(true) diff --git a/test/acceptance/serving_test.go b/test/acceptance/serving_test.go index c79894f4..8b01f5b2 100644 --- a/test/acceptance/serving_test.go +++ b/test/acceptance/serving_test.go @@ -1,6 +1,7 @@ package acceptance_test import ( + "context" "fmt" "io" "net/http" @@ -551,3 +552,32 @@ func TestDiskDisabledFailsToServeFileAndLocalContent(t *testing.T) { }, time.Second, 10*time.Millisecond) } } + +func TestSlowRequests(t *testing.T) { + opts := &stubOpts{ + delay: 250 * time.Millisecond, + } + logBuf := RunPagesProcess(t, + withStubOptions(opts), + withExtraArgument("gitlab-retrieval-timeout", "1s"), + withListeners([]ListenSpec{httpListener}), + ) + + ctx, cancel := context.WithTimeout(context.Background(), opts.delay/2) + defer cancel() + + url := httpListener.URL("/index.html") + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) + require.NoError(t, err) + + req.Host = "group.gitlab-example.com" + + _, err = DoPagesRequest(t, httpListener, req) + require.Error(t, err, "cancelling the context should trigger this error") + + require.Eventually(t, func() bool { + require.Contains(t, logBuf.String(), "context done: context canceled", "error mismatch") + require.Contains(t, logBuf.String(), "\"status\":404", "status mismatch") + return true + }, time.Second, time.Millisecond) +} |