aboutsummaryrefslogtreecommitdiffstats
path: root/cache/repo_cache_test.go
blob: e3a9cc15920f81176edcd742ef546f0a37b0d3bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package cache

import (
	"strings"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/MichaelMure/git-bug/entities/bug"
	"github.com/MichaelMure/git-bug/entities/identity"
	"github.com/MichaelMure/git-bug/entity"
	"github.com/MichaelMure/git-bug/query"
	"github.com/MichaelMure/git-bug/repository"
)

func TestCache(t *testing.T) {
	repo := repository.CreateGoGitTestRepo(t, false)

	indexCount := func(t *testing.T, name string) uint64 {
		t.Helper()

		idx, err := repo.GetIndex(name)
		require.NoError(t, err)
		count, err := idx.DocCount()
		require.NoError(t, err)

		return count
	}

	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)

	// 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.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))

	// Create a bug
	bug1, _, err := cache.Bugs().New("title", "message")
	require.NoError(t, err)

	// It's possible to create two identical bugs
	bug2, _, err := cache.Bugs().New("title", "marker")
	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.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)

	// 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)

	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(t, identity.Namespace))
	require.Equal(t, uint64(2), indexCount(t, bug.Namespace))

	// Resolving load from the disk
	_, 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)

	require.Len(t, cache.bugs.cached, 1)
	require.Len(t, cache.bugs.excerpts, 2)
	require.Len(t, cache.identities.cached, 1)
	require.Len(t, cache.identities.excerpts, 2)
	require.Equal(t, uint64(2), indexCount(t, identity.Namespace))
	require.Equal(t, uint64(2), indexCount(t, bug.Namespace))

	// Remove + RemoveAll
	err = cache.Identities().Remove(iden1.Id().String()[:10])
	require.NoError(t, err)
	err = cache.Bugs().Remove(bug1.Id().String()[:10])
	require.NoError(t, err)
	require.Len(t, cache.bugs.cached, 0)
	require.Len(t, cache.bugs.excerpts, 1)
	require.Len(t, cache.identities.cached, 0)
	require.Len(t, cache.identities.excerpts, 1)
	require.Equal(t, uint64(1), indexCount(t, identity.Namespace))
	require.Equal(t, uint64(1), indexCount(t, bug.Namespace))

	_, err = cache.Identities().New("René Descartes", "rene@descartes.fr")
	require.NoError(t, err)
	_, _, err = cache.Bugs().NewRaw(iden2, time.Now().Unix(), "title", "message", nil, nil)
	require.NoError(t, err)

	err = cache.RemoveAll()
	require.NoError(t, err)
	require.Len(t, cache.bugs.cached, 0)
	require.Len(t, cache.bugs.excerpts, 0)
	require.Len(t, cache.identities.cached, 0)
	require.Len(t, cache.identities.excerpts, 0)
	require.Equal(t, uint64(0), indexCount(t, identity.Namespace))
	require.Equal(t, uint64(0), indexCount(t, bug.Namespace))

	// 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)
}

func TestCachePushPull(t *testing.T) {
	repoA, repoB, _ := repository.SetupGoGitReposAndRemote(t)

	cacheA := createTestRepoCacheNoEvents(t, repoA)
	cacheB := createTestRepoCacheNoEvents(t, repoB)

	// Create, set and get user identity
	reneA, err := cacheA.Identities().New("René Descartes", "rene@descartes.fr")
	require.NoError(t, err)
	err = cacheA.SetUserIdentity(reneA)
	require.NoError(t, err)
	isaacB, err := cacheB.Identities().New("Isaac Newton", "isaac@newton.uk")
	require.NoError(t, err)
	err = cacheB.SetUserIdentity(isaacB)
	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
	_, _, err = cacheA.Bugs().New("bug1", "message")
	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.Bugs().AllIds(), 1)

	// retrieve and set identity
	reneB, err := cacheB.Identities().Resolve(reneA.Id())
	require.NoError(t, err)

	err = cacheB.SetUserIdentity(reneB)
	require.NoError(t, err)

	// B --> remote --> A
	_, _, err = cacheB.Bugs().New("bug2", "message")
	require.NoError(t, err)

	_, err = cacheB.Push("origin")
	require.NoError(t, err)

	err = cacheA.Pull("origin")
	require.NoError(t, err)

	require.Len(t, cacheA.Bugs().AllIds(), 2)
}

func TestRemove(t *testing.T) {
	repo := repository.CreateGoGitTestRepo(t, false)
	remoteA := repository.CreateGoGitTestRepo(t, true)
	remoteB := repository.CreateGoGitTestRepo(t, true)

	err := repo.AddRemote("remoteA", remoteA.GetLocalRemote())
	require.NoError(t, err)

	err = repo.AddRemote("remoteB", remoteB.GetLocalRemote())
	require.NoError(t, err)

	repoCache := createTestRepoCacheNoEvents(t, repo)

	rene, err := repoCache.Identities().New("René Descartes", "rene@descartes.fr")
	require.NoError(t, err)

	err = repoCache.SetUserIdentity(rene)
	require.NoError(t, err)

	_, _, err = repoCache.Bugs().New("title", "message")
	require.NoError(t, err)

	// and one more for testing
	b1, _, err := repoCache.Bugs().New("title", "message")
	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.Bugs().Remove(b1.Id().String())
	require.NoError(t, err)
	assert.Len(t, repoCache.bugs.cached, 1)
	assert.Len(t, repoCache.bugs.excerpts, 1)

	_, err = repoCache.Bugs().Resolve(b1.Id())
	assert.ErrorAs(t, entity.ErrNotFound{}, err)
}

func TestCacheEviction(t *testing.T) {
	repo := repository.CreateGoGitTestRepo(t, false)
	repoCache := createTestRepoCacheNoEvents(t, repo)
	repoCache.setCacheSize(2)

	require.Equal(t, 2, repoCache.bugs.maxLoaded)
	require.Len(t, repoCache.bugs.cached, 0)
	require.Equal(t, repoCache.bugs.lru.Len(), 0)

	// Generating some bugs
	rene, err := repoCache.Identities().New("René Descartes", "rene@descartes.fr")
	require.NoError(t, err)
	err = repoCache.SetUserIdentity(rene)
	require.NoError(t, err)

	bug1, _, err := repoCache.Bugs().New("title", "message")
	require.NoError(t, err)

	checkBugPresence(t, repoCache, bug1, true)
	require.Len(t, repoCache.bugs.cached, 1)
	require.Equal(t, 1, repoCache.bugs.lru.Len())

	bug2, _, err := repoCache.Bugs().New("title", "message")
	require.NoError(t, err)

	checkBugPresence(t, repoCache, bug1, true)
	checkBugPresence(t, repoCache, bug2, true)
	require.Len(t, repoCache.bugs.cached, 2)
	require.Equal(t, 2, repoCache.bugs.lru.Len())

	// Number of bugs should not exceed max size of lruCache, oldest one should be evicted
	bug3, _, err := repoCache.Bugs().New("title", "message")
	require.NoError(t, err)

	require.Len(t, repoCache.bugs.cached, 2)
	require.Equal(t, 2, repoCache.bugs.lru.Len())
	checkBugPresence(t, repoCache, bug1, false)
	checkBugPresence(t, repoCache, bug2, true)
	checkBugPresence(t, repoCache, bug3, true)

	// Accessing bug should update position in lruCache, and therefore it should not be evicted
	repoCache.bugs.lru.Get(bug2.Id())
	oldestId, _ := repoCache.bugs.lru.GetOldest()
	require.Equal(t, bug3.Id(), oldestId)

	checkBugPresence(t, repoCache, bug1, false)
	checkBugPresence(t, repoCache, bug2, true)
	checkBugPresence(t, repoCache, bug3, true)
	require.Len(t, repoCache.bugs.cached, 2)
	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]
	require.Equal(t, presence, ok)
	if ok {
		require.Equal(t, bug, b)
	}
}

func createTestRepoCacheNoEvents(t *testing.T, repo repository.TestedRepo) *RepoCache {
	t.Helper()

	cache, err := NewRepoCacheNoEvents(repo)
	require.NoError(t, err)

	t.Cleanup(func() {
		require.NoError(t, cache.Close())
	})

	return cache
}