diff options
Diffstat (limited to 'cache')
-rw-r--r-- | cache/repo_cache_test.go | 223 |
1 files changed, 131 insertions, 92 deletions
diff --git a/cache/repo_cache_test.go b/cache/repo_cache_test.go index ac5a7c75..39814733 100644 --- a/cache/repo_cache_test.go +++ b/cache/repo_cache_test.go @@ -18,106 +18,125 @@ import ( func TestCache(t *testing.T) { repo := repository.CreateGoGitTestRepo(t, false) - cache, err := NewRepoCacheNoEvents(repo) - require.NoError(t, err) - - // Create, set and get user identity - iden1, err := cache.Identities().New("René Descartes", "rene@descartes.fr") - require.NoError(t, err) - err = cache.SetUserIdentity(iden1) - require.NoError(t, err) - userIden, err := cache.GetUserIdentity() - require.NoError(t, err) - require.Equal(t, iden1.Id(), userIden.Id()) - - // it's possible to create two identical identities - iden2, err := cache.Identities().New("René Descartes", "rene@descartes.fr") - require.NoError(t, err) + indexCount := func(t *testing.T, name string) uint64 { + t.Helper() - // Two identical identities yield a different id - require.NotEqual(t, iden1.Id(), iden2.Id()) - - indexCount := func(name string) uint64 { idx, err := repo.GetIndex(name) require.NoError(t, err) count, err := idx.DocCount() require.NoError(t, err) + return count } - // There is now two identities in the cache - require.Len(t, cache.Identities().AllIds(), 2) - require.Len(t, cache.identities.excerpts, 2) - require.Len(t, cache.identities.cached, 2) - require.Equal(t, uint64(2), indexCount(identity.Namespace)) - require.Equal(t, uint64(0), indexCount(bug.Namespace)) + var ( + cache *RepoCache + bug1 *BugCache + iden1 *IdentityCache + ) - // Create a bug - bug1, _, err := cache.Bugs().New("title", "message") - require.NoError(t, err) + t.Run("entity operations on cache", func(t *testing.T) { + cache, err := NewRepoCacheNoEvents(repo) + require.NoError(t, err) - // It's possible to create two identical bugs - bug2, _, err := cache.Bugs().New("title", "marker") - require.NoError(t, err) + // Create, set and get user identity + iden1, err = cache.Identities().New("René Descartes", "rene@descartes.fr") + require.NoError(t, err) + err = cache.SetUserIdentity(iden1) + require.NoError(t, err) + userIden, err := cache.GetUserIdentity() + require.NoError(t, err) + require.Equal(t, iden1.Id(), userIden.Id()) - // two identical bugs yield a different id - require.NotEqual(t, bug1.Id(), bug2.Id()) + // it's possible to create two identical identities + iden2, err := cache.Identities().New("René Descartes", "rene@descartes.fr") + require.NoError(t, err) - // There is now two bugs in the cache - require.Len(t, cache.Bugs().AllIds(), 2) - require.Len(t, cache.bugs.excerpts, 2) - require.Len(t, cache.bugs.cached, 2) - require.Equal(t, uint64(2), indexCount(identity.Namespace)) - require.Equal(t, uint64(2), indexCount(bug.Namespace)) + // Two identical identities yield a different id + require.NotEqual(t, iden1.Id(), iden2.Id()) - // Resolving - _, err = cache.Identities().Resolve(iden1.Id()) - require.NoError(t, err) - _, err = cache.Identities().ResolveExcerpt(iden1.Id()) - require.NoError(t, err) - _, err = cache.Identities().ResolvePrefix(iden1.Id().String()[:10]) - require.NoError(t, err) + // There is now two identities in the cache + require.Len(t, cache.Identities().AllIds(), 2) + require.Len(t, cache.identities.excerpts, 2) + require.Len(t, cache.identities.cached, 2) + require.Equal(t, uint64(2), indexCount(t, identity.Namespace)) + require.Equal(t, uint64(0), indexCount(t, bug.Namespace)) - _, err = cache.Bugs().Resolve(bug1.Id()) - require.NoError(t, err) - _, err = cache.Bugs().ResolveExcerpt(bug1.Id()) - require.NoError(t, err) - _, err = cache.Bugs().ResolvePrefix(bug1.Id().String()[:10]) - require.NoError(t, err) + // Create a bug + bug1, _, err = cache.Bugs().New("title", "message") + require.NoError(t, err) - // Querying - q, err := query.Parse("status:open author:descartes sort:edit-asc") - require.NoError(t, err) - res, err := cache.Bugs().Query(q) - require.NoError(t, err) - require.Len(t, res, 2) + // It's possible to create two identical bugs + bug2, _, err := cache.Bugs().New("title", "marker") + require.NoError(t, err) - q, err = query.Parse("status:open marker") // full-text search - require.NoError(t, err) - res, err = cache.Bugs().Query(q) - require.NoError(t, err) - require.Len(t, res, 1) + // two identical bugs yield a different id + require.NotEqual(t, bug1.Id(), bug2.Id()) - // Close - require.NoError(t, cache.Close()) - require.Empty(t, cache.bugs.cached) - require.Empty(t, cache.bugs.excerpts) - require.Empty(t, cache.identities.cached) - require.Empty(t, cache.identities.excerpts) + // There is now two bugs in the cache + require.Len(t, cache.Bugs().AllIds(), 2) + require.Len(t, cache.bugs.excerpts, 2) + require.Len(t, cache.bugs.cached, 2) + require.Equal(t, uint64(2), indexCount(t, identity.Namespace)) + require.Equal(t, uint64(2), indexCount(t, bug.Namespace)) + + // Resolving + _, err = cache.Identities().Resolve(iden1.Id()) + require.NoError(t, err) + _, err = cache.Identities().ResolveExcerpt(iden1.Id()) + require.NoError(t, err) + _, err = cache.Identities().ResolvePrefix(iden1.Id().String()[:10]) + require.NoError(t, err) + + _, err = cache.Bugs().Resolve(bug1.Id()) + require.NoError(t, err) + _, err = cache.Bugs().ResolveExcerpt(bug1.Id()) + require.NoError(t, err) + _, err = cache.Bugs().ResolvePrefix(bug1.Id().String()[:10]) + require.NoError(t, err) + + // Querying + q, err := query.Parse("status:open author:descartes sort:edit-asc") + require.NoError(t, err) + res, err := cache.Bugs().Query(q) + require.NoError(t, err) + require.Len(t, res, 2) + + q, err = query.Parse("status:open marker") // full-text search + require.NoError(t, err) + res, err = cache.Bugs().Query(q) + require.NoError(t, err) + require.Len(t, res, 1) + + // Close + require.NoError(t, cache.Close()) + require.Empty(t, cache.bugs.cached) + require.Empty(t, cache.bugs.excerpts) + require.Empty(t, cache.identities.cached) + require.Empty(t, cache.identities.excerpts) + }) + + t.Run("closed caches are empty", func(t *testing.T) { + t.Skip() + cache, err := NewRepoCacheNoEvents(repo) + require.NoError(t, err) + + require.Empty(t, cache.identities.excerpts) + }) // Reload, only excerpt are loaded, but as we need to load the identities used in the bugs // to check the signatures, we also load the identity used above - cache, err = NewRepoCacheNoEvents(repo) - require.NoError(t, err) + cache = createTestRepoCacheNoEvents(t, repo) + require.Len(t, cache.bugs.cached, 0) require.Len(t, cache.bugs.excerpts, 2) require.Len(t, cache.identities.cached, 0) require.Len(t, cache.identities.excerpts, 2) - require.Equal(t, uint64(2), indexCount(identity.Namespace)) - require.Equal(t, uint64(2), indexCount(bug.Namespace)) + require.Equal(t, uint64(2), indexCount(t, identity.Namespace)) + require.Equal(t, uint64(2), indexCount(t, bug.Namespace)) // Resolving load from the disk - _, err = cache.Identities().Resolve(iden1.Id()) + _, err := cache.Identities().Resolve(iden1.Id()) require.NoError(t, err) _, err = cache.Identities().ResolveExcerpt(iden1.Id()) require.NoError(t, err) @@ -142,11 +161,8 @@ func TestCache(t *testing.T) { func TestCachePushPull(t *testing.T) { repoA, repoB, _ := repository.SetupGoGitReposAndRemote(t) - cacheA, err := NewRepoCacheNoEvents(repoA) - require.NoError(t, err) - - cacheB, err := NewRepoCacheNoEvents(repoB) - require.NoError(t, err) + cacheA := createTestRepoCacheNoEvents(t, repoA) + cacheB := createTestRepoCacheNoEvents(t, repoB) // Create, set and get user identity reneA, err := cacheA.Identities().New("René Descartes", "rene@descartes.fr") @@ -208,8 +224,7 @@ func TestRemove(t *testing.T) { err = repo.AddRemote("remoteB", remoteB.GetLocalRemote()) require.NoError(t, err) - repoCache, err := NewRepoCacheNoEvents(repo) - require.NoError(t, err) + repoCache := createTestRepoCacheNoEvents(t, repo) rene, err := repoCache.Identities().New("René Descartes", "rene@descartes.fr") require.NoError(t, err) @@ -247,8 +262,7 @@ func TestRemove(t *testing.T) { func TestCacheEviction(t *testing.T) { repo := repository.CreateGoGitTestRepo(t, false) - repoCache, err := NewRepoCacheNoEvents(repo) - require.NoError(t, err) + repoCache := createTestRepoCacheNoEvents(t, repo) repoCache.setCacheSize(2) require.Equal(t, 2, repoCache.bugs.maxLoaded) @@ -298,7 +312,25 @@ func TestCacheEviction(t *testing.T) { require.Equal(t, 2, repoCache.bugs.lru.Len()) } +func TestLongDescription(t *testing.T) { + // See https://github.com/MichaelMure/git-bug/issues/606 + + text := strings.Repeat("x", 65536) + + repo := repository.CreateGoGitTestRepo(t, false) + + backend := createTestRepoCacheNoEvents(t, repo) + + i, err := backend.Identities().New("René Descartes", "rene@descartes.fr") + require.NoError(t, err) + + _, _, err = backend.Bugs().NewRaw(i, time.Now().Unix(), text, text, nil, nil) + require.NoError(t, err) +} + func checkBugPresence(t *testing.T, cache *RepoCache, bug *BugCache, presence bool) { + t.Helper() + id := bug.Id() require.Equal(t, presence, cache.bugs.lru.Contains(id)) b, ok := cache.bugs.cached[id] @@ -308,19 +340,26 @@ func checkBugPresence(t *testing.T, cache *RepoCache, bug *BugCache, presence bo } } -func TestLongDescription(t *testing.T) { - // See https://github.com/MichaelMure/git-bug/issues/606 +func createTestRepoCacheNoEvents(t *testing.T, repo repository.TestedRepo) *RepoCache { + t.Helper() - text := strings.Repeat("x", 65536) + cache, err := NewRepoCacheNoEvents(repo) + require.NoError(t, err) - repo := repository.CreateGoGitTestRepo(t, false) + t.Cleanup(func() { + require.NoError(t, cache.Close()) + }) - backend, err := NewRepoCacheNoEvents(repo) - require.NoError(t, err) + return cache +} - i, err := backend.Identities().New("René Descartes", "rene@descartes.fr") - require.NoError(t, err) +func indexCount(t *testing.T, repo repository.TestedRepo, name string) uint64 { + t.Helper() - _, _, err = backend.Bugs().NewRaw(i, time.Now().Unix(), text, text, nil, nil) + idx, err := repo.GetIndex(name) require.NoError(t, err) + count, err := idx.DocCount() + require.NoError(t, err) + + return count } |