diff options
author | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-02-08 18:24:58 +0300 |
---|---|---|
committer | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-04-13 23:17:52 +0300 |
commit | 6766ebf152e01fac8318614a42728ab4832ff35d (patch) | |
tree | e86b5419653ec6e414d8c956884ef634758bbc16 | |
parent | ecfdfa7f25f82c11870a0c83e9fd7be0fc80ea00 (diff) |
test: add cache test case for ctx errors
-rw-r--r-- | internal/source/gitlab/cache/cache_test.go | 53 |
1 files changed, 40 insertions, 13 deletions
diff --git a/internal/source/gitlab/cache/cache_test.go b/internal/source/gitlab/cache/cache_test.go index 267c9b29..b92aa617 100644 --- a/internal/source/gitlab/cache/cache_test.go +++ b/internal/source/gitlab/cache/cache_test.go @@ -48,16 +48,8 @@ func (c *clientMock) Status() error { } func withTestCache(config resolverConfig, cacheConfig *config.Cache, block func(*Cache, *clientMock)) { - var chanSize int - - if config.buffered { - chanSize = 1 - } else { - chanSize = 0 - } - resolver := &clientMock{ - domain: make(chan string, chanSize), + domain: make(chan string, config.bufferSize), lookups: make(chan uint64, 100), failure: config.failure, } @@ -91,8 +83,8 @@ func (cache *Cache) withTestEntry(config entryConfig, block func(*Entry)) { } type resolverConfig struct { - buffered bool - failure error + bufferSize int + failure error } type entryConfig struct { @@ -102,8 +94,43 @@ type entryConfig struct { } func TestResolve(t *testing.T) { + t.Run("ctx errors should not be cached", func(t *testing.T) { + cc := &config.Cache{ + CacheExpiry: 10 * time.Minute, + CacheCleanupInterval: 10 * time.Minute, + EntryRefreshTimeout: 10 * time.Minute, + RetrievalTimeout: 1 * time.Second, + MaxRetrievalInterval: 50 * time.Millisecond, + MaxRetrievalRetries: 3, + } + + withTestCache(resolverConfig{bufferSize: 1}, cc, func(cache *Cache, resolver *clientMock) { + require.Equal(t, 0, len(resolver.lookups)) + + go func() { + lookup := cache.Resolve(context.Background(), "foo.gitlab.com") + require.ErrorIs(t, lookup.Error, context.DeadlineExceeded) + require.Equal(t, "foo.gitlab.com", lookup.Name) + }() + + // wait for retrieval timeout to expire, then send a response to the retriever + time.Sleep(2 * time.Second) + + // the ctx should be expired and the goroutine should receive an error + resolver.domain <- "foo.gitlab.com" + + // future lookups should succeed once the entry has been refreshed. + // refresh happens in a separate goroutine so the first few requests might still fail + require.Eventually(t, func() bool { + resolver.domain <- "foo.gitlab.com" + lookup := cache.Resolve(context.Background(), "foo.gitlab.com") + return lookup.Error == nil + }, 1*time.Second, 10*time.Millisecond) + }) + }) + t.Run("when item is not cached", func(t *testing.T) { - withTestCache(resolverConfig{buffered: true}, nil, func(cache *Cache, resolver *clientMock) { + withTestCache(resolverConfig{bufferSize: 1}, nil, func(cache *Cache, resolver *clientMock) { require.Empty(t, resolver.lookups) resolver.domain <- "my.gitlab.com" @@ -170,7 +197,7 @@ func TestResolve(t *testing.T) { }) t.Run("when item is in long cache only", func(t *testing.T) { - withTestCache(resolverConfig{buffered: false}, nil, func(cache *Cache, resolver *clientMock) { + withTestCache(resolverConfig{}, nil, func(cache *Cache, resolver *clientMock) { cache.withTestEntry(entryConfig{expired: true, retrieved: true}, func(*Entry) { lookup := cache.Resolve(context.Background(), "my.gitlab.com") |