persist_test.go (3657B)
1 package persist 2 3 import ( 4 "context" 5 "testing" 6 7 "git.defalsify.org/vise.git/db/mem" 8 "git.defalsify.org/vise.git/state" 9 "git.defalsify.org/vise.git/cache" 10 ) 11 12 func TestCreateCache(t *testing.T) { 13 ca := cache.NewCache() 14 if ca.Levels() != 1 { 15 t.Fatalf("expected level 1, got: %d", ca.Levels()) 16 } 17 ca.Push() 18 ca.Push() 19 if ca.Levels() != 3 { 20 t.Fatalf("expected level 3, got: %d", ca.Levels()) 21 } 22 ca.Reset() 23 if ca.Levels() != 1 { 24 t.Fatalf("expected level 1, got: %d", ca.Levels()) 25 } 26 } 27 28 func TestCacheUseSize(t *testing.T) { 29 ca := cache.NewCache() 30 v := ca.CacheUseSize 31 if v != 0 { 32 t.Fatalf("expected cache use size 0, got: %v", v) 33 } 34 ca.Add("foo", "barbarbar", 12) 35 v = ca.CacheUseSize 36 if v != 9 { 37 t.Fatalf("expected cache use size 9, got: %v", v) 38 } 39 ca.Reset() 40 v = ca.CacheUseSize 41 if v != 9 { 42 t.Fatalf("expected cache use size 9, got: %v", v) 43 } 44 ca.Pop() 45 v = ca.CacheUseSize 46 if v != 0 { 47 t.Fatalf("expected cache use size 0, got: %v", v) 48 } 49 } 50 51 func TestInvalidateState(t *testing.T) { 52 st := state.NewState(0) 53 ca := cache.NewCache() 54 55 ctx := context.Background() 56 store := mem.NewMemDb() 57 store.Connect(ctx, "") 58 pr := NewPersister(store).WithSession("xyzzy").WithContent(st, ca) 59 err := pr.Save("foo") 60 if err != nil { 61 t.Fatal(err) 62 } 63 64 st.Invalidate() 65 defer func() { 66 if r := recover(); r == nil { 67 t.Fatal("expected panic") 68 } 69 }() 70 _ = pr.Save("foo") 71 } 72 73 func TestInvalidateCache(t *testing.T) { 74 st := state.NewState(0) 75 ca := cache.NewCache() 76 77 ctx := context.Background() 78 store := mem.NewMemDb() 79 store.Connect(ctx, "") 80 pr := NewPersister(store).WithSession("xyzzy").WithContent(st, ca) 81 err := pr.Save("foo") 82 if err != nil { 83 t.Fatal(err) 84 } 85 86 ca.Invalidate() 87 defer func() { 88 if r := recover(); r == nil { 89 t.Fatal("expected panic") 90 } 91 }() 92 _ = pr.Save("foo") 93 } 94 95 func TestInvalidateAll(t *testing.T) { 96 st := state.NewState(0) 97 ca := cache.NewCache() 98 99 ctx := context.Background() 100 store := mem.NewMemDb() 101 store.Connect(ctx, "") 102 pr := NewPersister(store).WithSession("xyzzy").WithContent(st, ca) 103 err := pr.Save("foo") 104 if err != nil { 105 t.Fatal(err) 106 } 107 108 ca.Invalidate() 109 st.Invalidate() 110 defer func() { 111 if r := recover(); r == nil { 112 t.Fatal("expected panic") 113 } 114 }() 115 _ = pr.Save("foo") 116 } 117 118 func TestFlush(t *testing.T) { 119 ctx := context.Background() 120 st := state.NewState(15) 121 ca := cache.NewCache() 122 store := mem.NewMemDb() 123 store.Connect(ctx, "") 124 125 ca.Add("foo", "bar", 0) 126 ca.Push() 127 ca.Add("inky", "pinky", 42) 128 ca.Push() 129 ca.Add("blinky", "clyde", 13) 130 ca.WithCacheSize(666) 131 132 st.Down("xyzzy") 133 st.Down("plugh") 134 st.SetFlag(3) 135 st.SetFlag(10) 136 st.SetFlag(19) 137 138 pe := NewPersister(store).WithContent(st, ca).WithFlush() 139 err := pe.Save("baz") 140 if err != nil { 141 t.Fatal(err) 142 } 143 expectBitSize := uint32(15 + 8) 144 if st.FlagBitSize() != expectBitSize { 145 t.Fatalf("expected bitsize %d, got %d", expectBitSize, st.FlagBitSize()) 146 } 147 st = pe.GetState() 148 node, lvl := st.Where() 149 if lvl != 0 { 150 t.Fatalf("expected level 0, got: %d", lvl) 151 } 152 if node != "" { 153 t.Fatalf("expected node '', got '%s'", node) 154 } 155 cm := pe.GetMemory() 156 if cm.Levels() != 1 { 157 t.Fatalf("expected level 1, got: %d", cm.Levels()) 158 } 159 _, err = cm.Get("foo") 160 if err == nil { 161 t.Fatal("expected error") 162 } 163 _, err = cm.Get("blinky") 164 if err == nil { 165 t.Fatal("expected error") 166 } 167 _, err = cm.ReservedSize("foo") 168 if err == nil { 169 t.Fatal("expected error") 170 } 171 ks := cm.Keys(0) 172 if len(ks) > 0 { 173 t.Fatalf("expected keys list length 0, got: %v", ks) 174 } 175 o, ok := cm.(*cache.Cache) 176 if !ok { 177 panic("not cache") 178 } 179 if o.CacheUseSize != 0 { 180 t.Fatalf("expected cache use size 0, got: %v", o.CacheUseSize) 181 } 182 }