From b5f7ff699c9aeb05ae299274e22c50a7c1dc84f3 Mon Sep 17 00:00:00 2001 From: sawka Date: Mon, 13 May 2024 12:00:18 -0700 Subject: [PATCH] fix size bug, add more multi/writeat testing --- pkg/blockstore/blockstore.go | 2 +- pkg/blockstore/blockstore_test.go | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/blockstore/blockstore.go b/pkg/blockstore/blockstore.go index 4329624fc..6c8714710 100644 --- a/pkg/blockstore/blockstore.go +++ b/pkg/blockstore/blockstore.go @@ -303,7 +303,7 @@ func (s *BlockStore) loadDataParts(ctx context.Context, blockId string, name str } func (s *BlockStore) writeAt_nolock(entry *CacheEntry, offset int64, data []byte) { - endWrite := entry.FileEntry.File.Size + int64(len(data)) + endWrite := offset + int64(len(data)) entry.writeAt(offset, data) if endWrite > entry.FileEntry.File.Size { entry.FileEntry.File.Size = endWrite diff --git a/pkg/blockstore/blockstore_test.go b/pkg/blockstore/blockstore_test.go index 5f44f9f32..c00c0b79d 100644 --- a/pkg/blockstore/blockstore_test.go +++ b/pkg/blockstore/blockstore_test.go @@ -6,7 +6,6 @@ package blockstore import ( "bytes" "context" - "fmt" "testing" "time" @@ -158,6 +157,17 @@ func checkFileData(t *testing.T, ctx context.Context, blockId string, name strin } } +func checkFileDataAt(t *testing.T, ctx context.Context, blockId string, name string, offset int64, data string) { + _, rdata, err := GBS.ReadAt(ctx, blockId, name, offset, int64(len(data))) + if err != nil { + t.Errorf("error reading data for file %q: %v", name, err) + return + } + if string(rdata) != data { + t.Errorf("data mismatch for file %q: expected %q, got %q", name, data, string(rdata)) + } +} + func TestAppend(t *testing.T) { initDb(t) defer cleanupDb(t) @@ -220,5 +230,8 @@ func TestMultiPart(t *testing.T) { if string(barr) != data[42:52] { t.Errorf("data mismatch: expected %q, got %q", data[42:52], string(barr)) } - fmt.Print(GBS.dump()) + GBS.WriteAt(ctx, blockId, fileName, 49, []byte("world")) + checkFileSize(t, ctx, blockId, fileName, 80) + checkFileDataAt(t, ctx, blockId, fileName, 49, "world") + checkFileDataAt(t, ctx, blockId, fileName, 48, "8world4") }