Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software:2222/vole.git

A free and open-source forge that lives in your Git repo.

Blame · server/web/cache_test.go

Line attribution on main · 70 line(s)

73ec800b
Keenan Nemetz
10d ago
1 package web
2
3 import (
4 "fmt"
2a8f7cef
Keenan Nemetz
9d ago
5 "html/template"
6 "strings"
73ec800b
Keenan Nemetz
10d ago
7 "testing"
8 )
9
10 // TestCommitCacheRemembersAndEvicts covers the bound that keeps a repository
11 // page's history summaries from growing without limit as people browse older
12 // commits.
13 func TestCommitCacheRemembersAndEvicts(t *testing.T) {
14 var c commitCache[int]
15 if _, ok := c.get("missing"); ok {
16 t.Fatal("empty cache returned a value")
17 }
18 c.put("a", 1)
19 if v, ok := c.get("a"); !ok || v != 1 {
20 t.Fatalf("get(a) = %d, %t; want 1, true", v, ok)
21 }
22 // A repeated put is not a new entry, so it cannot evict a real one.
23 c.put("a", 2)
24 if v, _ := c.get("a"); v != 1 {
25 t.Fatalf("re-put changed the value to %d", v)
26 }
27
28 for i := 0; i < commitCacheEntries; i++ {
29 c.put(fmt.Sprintf("k%d", i), i)
30 }
31 if _, ok := c.get("a"); ok {
32 t.Fatal("the oldest entry survived a full cache")
33 }
34 if got := len(c.order); got != commitCacheEntries {
35 t.Fatalf("order holds %d keys, want %d", got, commitCacheEntries)
36 }
37 if v, ok := c.get(fmt.Sprintf("k%d", commitCacheEntries-1)); !ok || v != commitCacheEntries-1 {
38 t.Fatalf("newest entry missing: %d, %t", v, ok)
39 }
40 }
41
42 // TestCommitKeySeparatesRepositories pins the key: two repositories can hold
43 // the same commit id, and one's summary must not answer for the other's.
44 func TestCommitKeySeparatesRepositories(t *testing.T) {
45 const sha = "0123456789abcdef0123456789abcdef01234567"
ed03b3d1
Keenan Nemetz
6h ago
46 if commitKey("/-/repos/a.git", sha) == commitKey("/-/repos/b.git", sha) {
73ec800b
Keenan Nemetz
10d ago
47 t.Fatal("the same commit id in two repositories shares a key")
48 }
49 }
2a8f7cef
Keenan Nemetz
9d ago
50
51 func TestReadmeCacheEvictsOldEntries(t *testing.T) {
52 t.Parallel()
53 var c readmeCache
54 // One large entry first, then enough small ones to exceed the byte budget.
55 big := readmeAnswer{html: template.HTML(strings.Repeat("x", readmeCacheBytes/2)), name: "README.md"}
56 c.put("big", big)
57 for i := 0; i < 8 && c.bytes <= readmeCacheBytes; i++ {
58 c.put(fmt.Sprintf("small-%d", i), readmeAnswer{html: template.HTML(strings.Repeat("y", 1<<20)), name: "README.md"})
59 }
60 if _, ok := c.get("big"); ok {
61 t.Fatal("the oldest entry survived a byte-budget eviction")
62 }
63 if c.bytes > readmeCacheBytes {
64 t.Fatalf("cache holds %d bytes, budget is %d", c.bytes, readmeCacheBytes)
65 }
66 if _, ok := c.get("small-7"); !ok && len(c.order) < readmeCacheEntries {
67 // The last inserted entry must survive unless the entry-count limit evicted it.
68 t.Fatal("the newest entry was evicted")
69 }
70 }