test writemeta

This commit is contained in:
sawka 2024-05-13 00:08:50 -07:00
parent 68a6605209
commit 9bb6e27201
2 changed files with 57 additions and 0 deletions

View File

@ -43,6 +43,20 @@ type CacheEntry struct {
DataEntries []*DataCacheEntry
}
// for testing
func (s *BlockStore) getCacheSize() int {
s.Lock.Lock()
defer s.Lock.Unlock()
return len(s.Cache)
}
// for testing
func (s *BlockStore) clearCache() {
s.Lock.Lock()
defer s.Lock.Unlock()
s.Cache = make(map[cacheKey]*CacheEntry)
}
func (e *CacheEntry) ensurePart(partIdx int, create bool) *DataCacheEntry {
for len(e.DataEntries) <= partIdx {
e.DataEntries = append(e.DataEntries, nil)

View File

@ -32,6 +32,7 @@ func cleanupDb(t *testing.T) {
globalDBErr = nil
useTestingDb = false
partDataSize = DefaultPartDataSize
GBS.clearCache()
}
func TestCreate(t *testing.T) {
@ -77,3 +78,45 @@ func TestCreate(t *testing.T) {
t.Fatalf("opts not empty")
}
}
func checkMapsEqual(t *testing.T, m1 map[string]any, m2 map[string]any, msg string) {
if len(m1) != len(m2) {
t.Errorf("%s: map length mismatch", msg)
}
for k, v := range m1 {
if m2[k] != v {
t.Errorf("%s: value mismatch for key %q", msg, k)
}
}
}
func TestSetMeta(t *testing.T) {
initDb(t)
defer cleanupDb(t)
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()
blockId := uuid.New().String()
err := GBS.MakeFile(ctx, blockId, "testfile", nil, FileOptsType{})
if err != nil {
t.Fatalf("error creating file: %v", err)
}
if GBS.getCacheSize() != 0 {
t.Errorf("cache size mismatch -- should have 0 entries after create")
}
err = GBS.WriteMeta(ctx, blockId, "testfile", map[string]any{"a": 5, "b": "hello"})
if err != nil {
t.Fatalf("error setting meta: %v", err)
}
file, err := GBS.Stat(ctx, blockId, "testfile")
if err != nil {
t.Fatalf("error stating file: %v", err)
}
if file == nil {
t.Fatalf("file not found")
}
checkMapsEqual(t, map[string]any{"a": 5, "b": "hello"}, file.Meta, "meta")
if GBS.getCacheSize() != 1 {
t.Errorf("cache size mismatch")
}
}