git-bug/cache/repo_cache_test.go

277 lines
7.7 KiB
Go
Raw Normal View History

2019-03-25 23:58:27 +03:00
package cache
import (
"testing"
2020-07-26 11:14:11 +03:00
"github.com/stretchr/testify/assert"
2019-03-25 23:58:27 +03:00
"github.com/stretchr/testify/require"
2020-07-28 15:24:04 +03:00
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/query"
"github.com/MichaelMure/git-bug/repository"
2019-03-25 23:58:27 +03:00
)
func TestCache(t *testing.T) {
repo := repository.CreateGoGitTestRepo(false)
defer repository.CleanupTestRepos(repo)
2019-03-25 23:58:27 +03:00
cache, err := NewRepoCache(repo)
require.NoError(t, err)
// Create, set and get user identity
iden1, err := cache.NewIdentity("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.NewIdentity("René Descartes", "rene@descartes.fr")
require.NoError(t, err)
// Two identical identities yield a different id
require.NotEqual(t, iden1.Id(), iden2.Id())
// There is now two identities in the cache
require.Len(t, cache.AllIdentityIds(), 2)
require.Len(t, cache.identitiesExcerpts, 2)
require.Len(t, cache.identities, 2)
// Create a bug
2019-06-16 22:40:45 +03:00
bug1, _, err := cache.NewBug("title", "message")
2019-03-25 23:58:27 +03:00
require.NoError(t, err)
// It's possible to create two identical bugs
2019-06-16 22:40:45 +03:00
bug2, _, err := cache.NewBug("title", "message")
2019-03-25 23:58:27 +03:00
require.NoError(t, err)
// two identical bugs yield a different id
require.NotEqual(t, bug1.Id(), bug2.Id())
// There is now two bugs in the cache
require.Len(t, cache.AllBugsIds(), 2)
require.Len(t, cache.bugExcerpts, 2)
require.Len(t, cache.bugs, 2)
// Resolving
_, err = cache.ResolveIdentity(iden1.Id())
require.NoError(t, err)
_, err = cache.ResolveIdentityExcerpt(iden1.Id())
require.NoError(t, err)
_, err = cache.ResolveIdentityPrefix(iden1.Id().String()[:10])
2019-03-25 23:58:27 +03:00
require.NoError(t, err)
_, err = cache.ResolveBug(bug1.Id())
require.NoError(t, err)
_, err = cache.ResolveBugExcerpt(bug1.Id())
require.NoError(t, err)
_, err = cache.ResolveBugPrefix(bug1.Id().String()[:10])
2019-03-25 23:58:27 +03:00
require.NoError(t, err)
// Querying
q, err := query.Parse("status:open author:descartes sort:edit-asc")
2019-03-25 23:58:27 +03:00
require.NoError(t, err)
2020-12-08 16:42:13 +03:00
res, err := cache.QueryBugs(q)
require.NoError(t, err)
require.Len(t, res, 2)
2019-03-25 23:58:27 +03:00
// Close
require.NoError(t, cache.Close())
require.Empty(t, cache.bugs)
require.Empty(t, cache.bugExcerpts)
require.Empty(t, cache.identities)
require.Empty(t, cache.identitiesExcerpts)
// Reload, only excerpt are loaded
2020-08-25 05:43:42 +03:00
cache, err = NewRepoCache(repo)
require.NoError(t, err)
2019-03-25 23:58:27 +03:00
require.Empty(t, cache.bugs)
require.Empty(t, cache.identities)
require.Len(t, cache.bugExcerpts, 2)
require.Len(t, cache.identitiesExcerpts, 2)
// Resolving load from the disk
_, err = cache.ResolveIdentity(iden1.Id())
require.NoError(t, err)
_, err = cache.ResolveIdentityExcerpt(iden1.Id())
require.NoError(t, err)
_, err = cache.ResolveIdentityPrefix(iden1.Id().String()[:10])
2019-03-25 23:58:27 +03:00
require.NoError(t, err)
_, err = cache.ResolveBug(bug1.Id())
require.NoError(t, err)
_, err = cache.ResolveBugExcerpt(bug1.Id())
require.NoError(t, err)
_, err = cache.ResolveBugPrefix(bug1.Id().String()[:10])
2019-03-25 23:58:27 +03:00
require.NoError(t, err)
}
2019-04-02 23:01:42 +03:00
func TestPushPull(t *testing.T) {
2021-01-25 14:39:34 +03:00
repoA, repoB, remote := repository.SetupGoGitReposAndRemote()
defer repository.CleanupTestRepos(repoA, repoB, remote)
2019-04-02 23:01:42 +03:00
cacheA, err := NewRepoCache(repoA)
require.NoError(t, err)
cacheB, err := NewRepoCache(repoB)
require.NoError(t, err)
// Create, set and get user identity
reneA, err := cacheA.NewIdentity("René Descartes", "rene@descartes.fr")
require.NoError(t, err)
err = cacheA.SetUserIdentity(reneA)
require.NoError(t, err)
// distribute the identity
_, err = cacheA.Push("origin")
require.NoError(t, err)
err = cacheB.Pull("origin")
require.NoError(t, err)
// Create a bug in A
2019-06-16 22:40:45 +03:00
_, _, err = cacheA.NewBug("bug1", "message")
2019-04-02 23:01:42 +03:00
require.NoError(t, err)
// A --> remote --> B
_, err = cacheA.Push("origin")
require.NoError(t, err)
err = cacheB.Pull("origin")
require.NoError(t, err)
require.Len(t, cacheB.AllBugsIds(), 1)
// retrieve and set identity
reneB, err := cacheB.ResolveIdentity(reneA.Id())
require.NoError(t, err)
err = cacheB.SetUserIdentity(reneB)
require.NoError(t, err)
// B --> remote --> A
2019-06-16 22:40:45 +03:00
_, _, err = cacheB.NewBug("bug2", "message")
2019-04-02 23:01:42 +03:00
require.NoError(t, err)
_, err = cacheB.Push("origin")
require.NoError(t, err)
err = cacheA.Pull("origin")
require.NoError(t, err)
require.Len(t, cacheA.AllBugsIds(), 2)
}
2020-07-26 11:14:11 +03:00
func TestRemove(t *testing.T) {
repo := repository.CreateGoGitTestRepo(false)
remoteA := repository.CreateGoGitTestRepo(true)
remoteB := repository.CreateGoGitTestRepo(true)
2020-07-26 11:14:11 +03:00
defer repository.CleanupTestRepos(repo, remoteA, remoteB)
2020-12-05 05:08:54 +03:00
err := repo.AddRemote("remoteA", remoteA.GetLocalRemote())
2020-07-26 11:14:11 +03:00
require.NoError(t, err)
2020-12-05 05:08:54 +03:00
err = repo.AddRemote("remoteB", remoteB.GetLocalRemote())
2020-07-26 11:14:11 +03:00
require.NoError(t, err)
repoCache, err := NewRepoCache(repo)
require.NoError(t, err)
rene, err := repoCache.NewIdentity("René Descartes", "rene@descartes.fr")
require.NoError(t, err)
2020-07-26 10:52:29 +03:00
err = repoCache.SetUserIdentity(rene)
require.NoError(t, err)
2020-08-25 05:43:42 +03:00
_, _, err = repoCache.NewBug("title", "message")
require.NoError(t, err)
2020-07-26 11:14:11 +03:00
// and one more for testing
2020-07-26 10:52:29 +03:00
b1, _, err := repoCache.NewBug("title", "message")
2020-07-26 11:14:11 +03:00
require.NoError(t, err)
_, err = repoCache.Push("remoteA")
require.NoError(t, err)
_, err = repoCache.Push("remoteB")
require.NoError(t, err)
_, err = repoCache.Fetch("remoteA")
require.NoError(t, err)
_, err = repoCache.Fetch("remoteB")
require.NoError(t, err)
err = repoCache.RemoveBug(b1.Id().String())
require.NoError(t, err)
2020-08-25 05:43:42 +03:00
assert.Equal(t, 1, len(repoCache.bugs))
assert.Equal(t, 1, len(repoCache.bugExcerpts))
2020-07-28 15:24:04 +03:00
_, err = repoCache.ResolveBug(b1.Id())
assert.Error(t, bug.ErrBugNotExist, err)
2020-07-26 11:14:11 +03:00
}
2020-07-26 10:52:29 +03:00
func TestCacheEviction(t *testing.T) {
repo := repository.CreateGoGitTestRepo(false)
2020-07-26 10:52:29 +03:00
repoCache, err := NewRepoCache(repo)
require.NoError(t, err)
2020-08-25 16:26:23 +03:00
repoCache.setCacheSize(2)
2020-07-26 10:52:29 +03:00
2020-08-25 16:26:23 +03:00
require.Equal(t, 2, repoCache.maxLoadedBugs)
require.Equal(t, 0, repoCache.loadedBugs.Len())
2020-07-26 10:52:29 +03:00
require.Equal(t, 0, len(repoCache.bugs))
// Generating some bugs
rene, err := repoCache.NewIdentity("René Descartes", "rene@descartes.fr")
require.NoError(t, err)
err = repoCache.SetUserIdentity(rene)
require.NoError(t, err)
bug1, _, err := repoCache.NewBug("title", "message")
require.NoError(t, err)
checkBugPresence(t, repoCache, bug1, true)
2020-08-25 16:26:23 +03:00
require.Equal(t, 1, repoCache.loadedBugs.Len())
2020-07-26 10:52:29 +03:00
require.Equal(t, 1, len(repoCache.bugs))
bug2, _, err := repoCache.NewBug("title", "message")
require.NoError(t, err)
checkBugPresence(t, repoCache, bug1, true)
checkBugPresence(t, repoCache, bug2, true)
2020-08-25 16:26:23 +03:00
require.Equal(t, 2, repoCache.loadedBugs.Len())
2020-07-26 10:52:29 +03:00
require.Equal(t, 2, len(repoCache.bugs))
// Number of bugs should not exceed max size of lruCache, oldest one should be evicted
bug3, _, err := repoCache.NewBug("title", "message")
require.NoError(t, err)
2020-08-25 16:26:23 +03:00
require.Equal(t, 2, repoCache.loadedBugs.Len())
2020-07-26 10:52:29 +03:00
require.Equal(t, 2, len(repoCache.bugs))
2020-08-25 05:43:42 +03:00
checkBugPresence(t, repoCache, bug1, false)
checkBugPresence(t, repoCache, bug2, true)
checkBugPresence(t, repoCache, bug3, true)
2020-07-26 10:52:29 +03:00
// Accessing bug should update position in lruCache and therefore it should not be evicted
2020-08-25 16:26:23 +03:00
repoCache.loadedBugs.Get(bug2.Id())
oldestId, _ := repoCache.loadedBugs.GetOldest()
2020-07-26 10:52:29 +03:00
require.Equal(t, bug3.Id(), oldestId)
checkBugPresence(t, repoCache, bug1, false)
checkBugPresence(t, repoCache, bug2, true)
checkBugPresence(t, repoCache, bug3, true)
2020-08-25 16:26:23 +03:00
require.Equal(t, 2, repoCache.loadedBugs.Len())
2020-07-26 10:52:29 +03:00
require.Equal(t, 2, len(repoCache.bugs))
}
func checkBugPresence(t *testing.T, cache *RepoCache, bug *BugCache, presence bool) {
id := bug.Id()
2020-08-25 16:26:23 +03:00
require.Equal(t, presence, cache.loadedBugs.Contains(id))
2020-07-26 10:52:29 +03:00
b, ok := cache.bugs[id]
require.Equal(t, presence, ok)
2020-08-25 05:43:42 +03:00
if ok {
require.Equal(t, bug, b)
}
2020-07-26 10:52:29 +03:00
}