diff options
author | Stan Hu <stanhu@gmail.com> | 2022-07-26 02:04:05 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2022-07-26 07:35:27 +0300 |
commit | 6827d12ae6e613295ce788365c08d7cab44d1f80 (patch) | |
tree | a7897c9d6eabdbc8a3126c6795ef074f1d0d6984 /internal/source | |
parent | 4d2b5871703931acb597b414d26b2e7835bad5b5 (diff) |
Refactor: move test cache config into testhelpers
This reduces some code duplication.
Closes https://gitlab.com/gitlab-org/gitlab-pages/-/issues/799
Diffstat (limited to 'internal/source')
-rw-r--r-- | internal/source/gitlab/cache/cache_test.go | 16 | ||||
-rw-r--r-- | internal/source/gitlab/cache/entry_test.go | 3 | ||||
-rw-r--r-- | internal/source/gitlab/gitlab_test.go | 16 |
3 files changed, 9 insertions, 26 deletions
diff --git a/internal/source/gitlab/cache/cache_test.go b/internal/source/gitlab/cache/cache_test.go index aad357ae..6973fd31 100644 --- a/internal/source/gitlab/cache/cache_test.go +++ b/internal/source/gitlab/cache/cache_test.go @@ -12,17 +12,9 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/config" "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api" + "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" ) -var testCacheConfig = config.Cache{ - CacheExpiry: time.Second, - CacheCleanupInterval: time.Second / 2, - EntryRefreshTimeout: time.Second / 2, - RetrievalTimeout: time.Second, - MaxRetrievalInterval: time.Second / 3, - MaxRetrievalRetries: 3, -} - type clientMock struct { counter uint64 lookups chan uint64 @@ -54,7 +46,7 @@ func withTestCache(config resolverConfig, cacheConfig *config.Cache, block func( failure: config.failure, } if cacheConfig == nil { - cacheConfig = &testCacheConfig + cacheConfig = &testhelpers.CacheConfig } cache := NewCache(resolver, cacheConfig) @@ -221,7 +213,7 @@ func TestResolve(t *testing.T) { }) t.Run("when retrieval failed with an error", func(t *testing.T) { - cc := testCacheConfig + cc := testhelpers.CacheConfig cc.MaxRetrievalInterval = 0 err := errors.New("500 error") @@ -234,7 +226,7 @@ func TestResolve(t *testing.T) { }) t.Run("when retrieval failed because of an internal retriever context timeout", func(t *testing.T) { - cc := testCacheConfig + cc := testhelpers.CacheConfig cc.RetrievalTimeout = 0 withTestCache(resolverConfig{}, &cc, func(cache *Cache, resolver *clientMock) { diff --git a/internal/source/gitlab/cache/entry_test.go b/internal/source/gitlab/cache/entry_test.go index 2ba26fd2..3bf2d67b 100644 --- a/internal/source/gitlab/cache/entry_test.go +++ b/internal/source/gitlab/cache/entry_test.go @@ -12,6 +12,7 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/config" "gitlab.com/gitlab-org/gitlab-pages/internal/domain" "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api" + "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" ) func TestIsUpToDateAndNeedsRefresh(t *testing.T) { @@ -54,7 +55,7 @@ func TestIsUpToDateAndNeedsRefresh(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - entry := newCacheEntry("my.gitlab.com", testCacheConfig.EntryRefreshTimeout, testCacheConfig.CacheExpiry) + entry := newCacheEntry("my.gitlab.com", testhelpers.CacheConfig.EntryRefreshTimeout, testhelpers.CacheConfig.CacheExpiry) if tt.resolved { entry.response = &api.Lookup{} } diff --git a/internal/source/gitlab/gitlab_test.go b/internal/source/gitlab/gitlab_test.go index 5a63edf2..df8299de 100644 --- a/internal/source/gitlab/gitlab_test.go +++ b/internal/source/gitlab/gitlab_test.go @@ -8,27 +8,17 @@ import ( "os" "sync" "testing" - "time" "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitlab-pages/internal/config" "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api" "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/cache" "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/client" "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/mock" + "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" ) -var testCacheConfig = config.Cache{ - CacheExpiry: time.Second, - CacheCleanupInterval: time.Second / 2, - EntryRefreshTimeout: time.Second / 2, - RetrievalTimeout: time.Second, - MaxRetrievalInterval: time.Second / 3, - MaxRetrievalRetries: 3, -} - type lookupPathTest struct { file string target string @@ -171,7 +161,7 @@ func TestResolveLookupPathsConcurrentNetRequests(t *testing.T) { t.Run(name, func(t *testing.T) { wg := &sync.WaitGroup{} mockClient := NewMockClient(t, test.file, nil, true) - cache := cache.NewCache(mockClient, &testCacheConfig) + cache := cache.NewCache(mockClient, &testhelpers.CacheConfig) for i := 0; i < 3; i++ { go sendResolveRequest(t, wg, cache, test) @@ -207,7 +197,7 @@ func TestResolveLookupPathsOrderDoesNotMatter(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { mockClient := NewMockClient(t, test.file, nil, true) - cache := cache.NewCache(mockClient, &testCacheConfig) + cache := cache.NewCache(mockClient, &testhelpers.CacheConfig) source := Gitlab{client: cache, enableDisk: true} request := httptest.NewRequest(http.MethodGet, test.target, nil) |