diff --git a/pkg/blockstore/blockstore_cache.go b/pkg/blockstore/blockstore_cache.go index dc6ba26bc..8b72fa5a8 100644 --- a/pkg/blockstore/blockstore_cache.go +++ b/pkg/blockstore/blockstore_cache.go @@ -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) diff --git a/pkg/blockstore/blockstore_test.go b/pkg/blockstore/blockstore_test.go index 2fad077f1..4f4dec6a8 100644 --- a/pkg/blockstore/blockstore_test.go +++ b/pkg/blockstore/blockstore_test.go @@ -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") + } +}