diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-11-13 15:40:18 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-11-13 15:40:18 +0300 |
commit | 9eb34511cf84be3caf4972d40b115d3bd1f2e295 (patch) | |
tree | 24286f74d3d30cf7037319010873ecb7f9292078 /internal/source/gitlab/cache | |
parent | 66473aec0d138a013963936ae293f1484630c0de (diff) |
Add more tests and fix cache entry refresh
Diffstat (limited to 'internal/source/gitlab/cache')
-rw-r--r-- | internal/source/gitlab/cache/cache_test.go | 7 | ||||
-rw-r--r-- | internal/source/gitlab/cache/entry.go | 6 | ||||
-rw-r--r-- | internal/source/gitlab/cache/entry_test.go | 43 | ||||
-rw-r--r-- | internal/source/gitlab/cache/memstore.go | 4 | ||||
-rw-r--r-- | internal/source/gitlab/cache/store.go | 2 |
5 files changed, 52 insertions, 10 deletions
diff --git a/internal/source/gitlab/cache/cache_test.go b/internal/source/gitlab/cache/cache_test.go index 5a885279..71212ef2 100644 --- a/internal/source/gitlab/cache/cache_test.go +++ b/internal/source/gitlab/cache/cache_test.go @@ -64,7 +64,7 @@ func (cache *Cache) withTestEntry(config entryConfig, block func(*Entry)) { domain = config.domain } - entry := cache.store.ReplaceOrCreate(context.Background(), domain) + entry := cache.store.LoadOrCreate(context.Background(), domain) if config.retrieved { newResponse := make(chan Lookup, 1) @@ -229,10 +229,11 @@ func TestResolve(t *testing.T) { t.Run("when cache entry is evicted from cache", func(t *testing.T) { withTestCache(resolverConfig{}, func(cache *Cache, resolver *client) { cache.withTestEntry(entryConfig{expired: false, retrieved: false}, func(entry *Entry) { + ctx := context.Background() lookup := make(chan *Lookup, 1) - go func() { lookup <- cache.Resolve(context.Background(), "my.gitlab.com") }() + go func() { lookup <- cache.Resolve(ctx, "my.gitlab.com") }() - cache.store.ReplaceOrCreate(context.Background(), "my.gitlab.com") + cache.store.ReplaceOrCreate(ctx, "my.gitlab.com", newCacheEntry(ctx, "my.gitlab.com")) resolver.domain <- "my.gitlab.com" <-lookup diff --git a/internal/source/gitlab/cache/entry.go b/internal/source/gitlab/cache/entry.go index 55380a7b..ad621963 100644 --- a/internal/source/gitlab/cache/entry.go +++ b/internal/source/gitlab/cache/entry.go @@ -85,11 +85,11 @@ func (e *Entry) Retrieve(client Resolver) <-chan struct{} { func (e *Entry) Refresh(ctx context.Context, client Resolver, store Store) { e.refresh.Do(func() { go func() { - newEntry := newCacheEntry(ctx, e.domain) + entry := newCacheEntry(ctx, e.domain) - <-newEntry.Retrieve(client) + <-entry.Retrieve(client) - store.ReplaceOrCreate(ctx, e.domain) + store.ReplaceOrCreate(ctx, e.domain, entry) }() }) } diff --git a/internal/source/gitlab/cache/entry_test.go b/internal/source/gitlab/cache/entry_test.go new file mode 100644 index 00000000..98164cc3 --- /dev/null +++ b/internal/source/gitlab/cache/entry_test.go @@ -0,0 +1,43 @@ +package cache + +import ( + "context" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestIsUpToDateAndNeedsRefresh(t *testing.T) { + t.Run("when is resolved and not expired", func(t *testing.T) { + entry := newCacheEntry(context.Background(), "my.gitlab.com") + entry.response = &Lookup{} + + assert.True(t, entry.IsUpToDate()) + assert.False(t, entry.NeedsRefresh()) + }) + + t.Run("when is resolved and is expired", func(t *testing.T) { + entry := newCacheEntry(context.Background(), "my.gitlab.com") + entry.response = &Lookup{} + entry.created = time.Now().Add(-time.Hour) + + assert.False(t, entry.IsUpToDate()) + assert.True(t, entry.NeedsRefresh()) + }) + + t.Run("when is not resolved and not expired", func(t *testing.T) { + entry := newCacheEntry(context.Background(), "my.gitlab.com") + + assert.False(t, entry.IsUpToDate()) + assert.False(t, entry.NeedsRefresh()) + }) + + t.Run("when is not resolved and is expired", func(t *testing.T) { + entry := newCacheEntry(context.Background(), "my.gitlab.com") + entry.created = time.Now().Add(-time.Hour) + + assert.False(t, entry.IsUpToDate()) + assert.False(t, entry.NeedsRefresh()) + }) +} diff --git a/internal/source/gitlab/cache/memstore.go b/internal/source/gitlab/cache/memstore.go index b521b55c..aefa2183 100644 --- a/internal/source/gitlab/cache/memstore.go +++ b/internal/source/gitlab/cache/memstore.go @@ -39,12 +39,10 @@ func (m *memstore) LoadOrCreate(ctx context.Context, domain string) *Entry { return entry } -func (m *memstore) ReplaceOrCreate(ctx context.Context, domain string) *Entry { +func (m *memstore) ReplaceOrCreate(ctx context.Context, domain string, entry *Entry) *Entry { m.mux.Lock() defer m.mux.Unlock() - entry := newCacheEntry(ctx, domain) - if _, exists := m.store.Get(domain); exists { m.store.Delete(domain) // delete manually to trigger onEvicted } diff --git a/internal/source/gitlab/cache/store.go b/internal/source/gitlab/cache/store.go index 18ffb3ea..d2619c0e 100644 --- a/internal/source/gitlab/cache/store.go +++ b/internal/source/gitlab/cache/store.go @@ -5,5 +5,5 @@ import "context" // Store defines an interface describing an abstract cache store type Store interface { LoadOrCreate(ctx context.Context, domain string) *Entry - ReplaceOrCreate(ctx context.Context, domain string) *Entry + ReplaceOrCreate(ctx context.Context, domain string, entry *Entry) *Entry } |