diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-11-12 18:08:05 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-11-12 18:08:05 +0300 |
commit | 6ac8fafb19404c7c0c7bfb981ef02b91ee42cf42 (patch) | |
tree | b9f149f40aa208ef7665bc43cc72b591ff0db6e6 /internal/source/gitlab/cache/cache_test.go | |
parent | b60ee3425b4b6d54154de1629075b52b8599d869 (diff) |
Simplify GitLab source domain response type
Diffstat (limited to 'internal/source/gitlab/cache/cache_test.go')
-rw-r--r-- | internal/source/gitlab/cache/cache_test.go | 56 |
1 files changed, 31 insertions, 25 deletions
diff --git a/internal/source/gitlab/cache/cache_test.go b/internal/source/gitlab/cache/cache_test.go index f5dd0dfa..01291e22 100644 --- a/internal/source/gitlab/cache/cache_test.go +++ b/internal/source/gitlab/cache/cache_test.go @@ -2,6 +2,7 @@ package cache import ( "context" + "errors" "sync" "sync/atomic" "testing" @@ -17,23 +18,27 @@ type client struct { status int } -func (s *client) Resolve(ctx context.Context, domain string) (*Lookup, int, error) { - atomic.AddUint64(&s.resolutions, 1) +func (c *client) Resolve(ctx context.Context, _ string) Lookup { + atomic.AddUint64(&c.resolutions, 1) - if s.failure != nil { - return &Lookup{}, s.status, s.failure + if c.status == 0 { + c.status = 200 } - return &Lookup{Domain: <-s.domain}, 200, nil + if c.failure != nil { + return Lookup{Domain: Domain{}, Status: c.status, Err: c.failure} + } + + return Lookup{Domain: Domain{Name: <-c.domain}, Status: c.status, Err: nil} } func withTestCache(config resolverConfig, block func(*Cache, *client)) { var resolver *client if config.buffered { - resolver = &client{domain: make(chan string, 1)} + resolver = &client{domain: make(chan string, 1), failure: config.failure} } else { - resolver = &client{domain: make(chan string)} + resolver = &client{domain: make(chan string), failure: config.failure} } cache := NewCache(resolver) @@ -51,8 +56,8 @@ func (cache *Cache) withTestEntry(config entryConfig, block func()) { entry := cache.store.ReplaceOrCreate(context.Background(), domain) if config.retrieved { - newResponse := make(chan Response, 1) - newResponse <- Response{lookup: &Lookup{Domain: domain}, status: 200} + newResponse := make(chan Lookup, 1) + newResponse <- Lookup{Domain: Domain{Name: domain}, Status: 200} entry.setResponse(newResponse) } @@ -65,6 +70,7 @@ func (cache *Cache) withTestEntry(config entryConfig, block func()) { type resolverConfig struct { buffered bool + failure error } type entryConfig struct { @@ -78,11 +84,11 @@ func TestGetLookup(t *testing.T) { withTestCache(resolverConfig{buffered: true}, func(cache *Cache, resolver *client) { resolver.domain <- "my.gitlab.com" - lookup, status, err := cache.Resolve(context.Background(), "my.gitlab.com") + lookup := cache.Resolve(context.Background(), "my.gitlab.com") - assert.NoError(t, err) - assert.Equal(t, 200, status) - assert.Equal(t, "my.gitlab.com", lookup.Domain) + assert.NoError(t, lookup.Err) + assert.Equal(t, 200, lookup.Status) + assert.Equal(t, "my.gitlab.com", lookup.Domain.Name) assert.Equal(t, uint64(1), resolver.resolutions) }) }) @@ -114,9 +120,9 @@ func TestGetLookup(t *testing.T) { t.Run("when item is in short cache", func(t *testing.T) { withTestCache(resolverConfig{}, func(cache *Cache, resolver *client) { cache.withTestEntry(entryConfig{expired: false, retrieved: true}, func() { - lookup, _, _ := cache.Resolve(context.Background(), "my.gitlab.com") + lookup := cache.Resolve(context.Background(), "my.gitlab.com") - assert.Equal(t, "my.gitlab.com", lookup.Domain) + assert.Equal(t, "my.gitlab.com", lookup.Domain.Name) assert.Equal(t, uint64(0), resolver.resolutions) }) }) @@ -125,9 +131,9 @@ func TestGetLookup(t *testing.T) { t.Run("when item is in long cache only", func(t *testing.T) { withTestCache(resolverConfig{}, func(cache *Cache, resolver *client) { cache.withTestEntry(entryConfig{expired: true, retrieved: true}, func() { - lookup, _, _ := cache.Resolve(context.Background(), "my.gitlab.com") + lookup := cache.Resolve(context.Background(), "my.gitlab.com") - assert.Equal(t, "my.gitlab.com", lookup.Domain) + assert.Equal(t, "my.gitlab.com", lookup.Domain.Name) assert.Equal(t, uint64(0), resolver.resolutions) resolver.domain <- "my.gitlab.com" @@ -152,13 +158,13 @@ func TestGetLookup(t *testing.T) { }) t.Run("when retrieval failed with an error", func(t *testing.T) { - // cache := NewCache() - // resolver := &stubbedClient{ - // failure: errors.New("could not retrieve lookup"), - // } - // - // lookup := cache.GetLookup("my.gitlab.com", resolver.Resolve) - // - // assert.Equal(t, &Lookup{}, lookup) + withTestCache(resolverConfig{failure: errors.New("500 err")}, func(cache *Cache, resolver *client) { + maxRetrievalInterval = 0 + + lookup := cache.Resolve(context.Background(), "my.gitlab.com") + + assert.Equal(t, uint64(3), resolver.resolutions) + assert.EqualError(t, lookup.Err, "500 err") + }) }) } |