BlueGit

timfa bluegit

Files

bluegit / internal / gitgraph / cache_test.go

cache_test.go

cache_test.go 2.6 KiB Download Raw
1package gitgraph
2
3import (
4 "context"
5 "errors"
6 "testing"
7)
8
9func TestCacheHitSkipsLoader(t *testing.T) {
10 c := NewCache(0)
11 commits := commits("c:b", "b:a", "a:")
12 refs := []Ref{{Name: "master", SHA: "c", Kind: "branch"}}
13 key := Key(7, map[string]string{"refs/heads/master": "c"}, "master", 40)
14 var loads int
15 load := func(ctx context.Context) ([]Commit, []Ref, error) {
16 loads++
17 return commits, refs, nil
18 }
19 g1, err := c.Get(context.Background(), key, "master", load)
20 if err != nil || g1 == nil || len(g1.Rows) != 3 {
21 t.Fatalf("koud: %v %+v", err, g1)
22 }
23 if loads != 1 {
24 t.Fatalf("eerste load=%d", loads)
25 }
26 fail := func(ctx context.Context) ([]Commit, []Ref, error) {
27 t.Fatal("loader mag bij een hit niet worden aangeroepen")
28 return nil, nil, errors.New("geen git")
29 }
30 g2, err := c.Get(context.Background(), key, "master", fail)
31 if err != nil || g2 == nil || len(g2.Rows) != 3 {
32 t.Fatalf("warm: %v %+v", err, g2)
33 }
34 hits, misses, entries, _ := c.Stats()
35 if hits != 1 || misses != 1 || entries != 1 {
36 t.Fatalf("stats hits=%d misses=%d entries=%d", hits, misses, entries)
37 }
38}
39
40func TestCacheDropOnPush(t *testing.T) {
41 c := NewCache(0)
42 key := Key(3, map[string]string{"refs/heads/master": "c"}, "master", 40)
43 _, err := c.Get(context.Background(), key, "master", func(ctx context.Context) ([]Commit, []Ref, error) {
44 return commits("c:"), []Ref{{Name: "master", SHA: "c", Kind: "branch"}}, nil
45 })
46 if err != nil {
47 t.Fatal(err)
48 }
49 c.Drop(3)
50 var loads int
51 _, err = c.Get(context.Background(), key, "master", func(ctx context.Context) ([]Commit, []Ref, error) {
52 loads++
53 return commits("c:"), []Ref{{Name: "master", SHA: "c", Kind: "branch"}}, nil
54 })
55 if err != nil || loads != 1 {
56 t.Fatalf("na drop opnieuw laden, loads=%d err=%v", loads, err)
57 }
58 c.Drop(99)
59 _, _, entries, _ := c.Stats()
60 if entries != 1 {
61 t.Fatalf("andere repo mag deze entry niet wissen, entries=%d", entries)
62 }
63}
64
65func TestCacheLRUEvicts(t *testing.T) {
66 c := NewCache(400) // klein: een paar grafieken
67 load := func(sha string) Loader {
68 return func(ctx context.Context) ([]Commit, []Ref, error) {
69 return []Commit{{SHA: sha, Subject: "lang onderwerp zodat de schatting meetelt " + sha, Parents: nil}}, nil, nil
70 }
71 }
72 for i := 0; i < 40; i++ {
73 key := Key(int64(i), map[string]string{"h": string(rune('a' + i%26))}, "master", 40)
74 if _, err := c.Get(context.Background(), key, "master", load("s")); err != nil {
75 t.Fatal(err)
76 }
77 }
78 _, _, entries, bytes := c.Stats()
79 if entries == 0 || entries == 40 {
80 t.Fatalf("verwacht eviction, entries=%d bytes=%d", entries, bytes)
81 }
82 if entries > 1 && bytes > 400 {
83 t.Fatalf("cache groter dan max: bytes=%d entries=%d", bytes, entries)
84 }
85}