diff --git a/blockstore/splitstore/splitstore_test.go b/blockstore/splitstore/splitstore_test.go index db5144039..e5314b80f 100644 --- a/blockstore/splitstore/splitstore_test.go +++ b/blockstore/splitstore/splitstore_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "sync" + "sync/atomic" "testing" "time" @@ -14,6 +15,7 @@ import ( cid "github.com/ipfs/go-cid" datastore "github.com/ipfs/go-datastore" + dssync "github.com/ipfs/go-datastore/sync" logging "github.com/ipfs/go-log/v2" ) @@ -25,8 +27,6 @@ func init() { } func testSplitStore(t *testing.T, cfg *Config) { - t.Helper() - chain := &mockChain{} // genesis genBlock := mock.MkBlock(nil, 0, 0) @@ -34,7 +34,7 @@ func testSplitStore(t *testing.T, cfg *Config) { chain.push(genTs) // the myriads of stores - ds := datastore.NewMapDatastore() + ds := dssync.MutexWrap(datastore.NewMapDatastore()) hot := blockstore.NewMemorySync() cold := blockstore.NewMemorySync() @@ -90,9 +90,16 @@ func testSplitStore(t *testing.T, cfg *Config) { } } + waitForCompaction := func() { + for atomic.LoadInt32(&ss.compacting) == 1 { + time.Sleep(100 * time.Millisecond) + } + } + curTs := genTs for i := 1; i < 5; i++ { curTs = mkBlock(curTs, i) + waitForCompaction() } mkGarbageBlock(genTs, 1) @@ -117,17 +124,17 @@ func testSplitStore(t *testing.T, cfg *Config) { hotCnt := countBlocks(hot) if coldCnt != 1 { - t.Fatalf("expected %d blocks, but got %d", 1, coldCnt) + t.Errorf("expected %d blocks, but got %d", 1, coldCnt) } - if hotCnt != 4 { - t.Fatalf("expected %d blocks, but got %d", 4, hotCnt) + if hotCnt != 5 { + t.Errorf("expected %d blocks, but got %d", 5, hotCnt) } // trigger a compaction for i := 5; i < 10; i++ { curTs = mkBlock(curTs, i) - time.Sleep(time.Second) + waitForCompaction() } coldCnt = countBlocks(cold) @@ -135,31 +142,31 @@ func testSplitStore(t *testing.T, cfg *Config) { if !cfg.EnableFullCompaction { if coldCnt != 5 { - t.Fatalf("expected %d cold blocks, but got %d", 5, coldCnt) + t.Errorf("expected %d cold blocks, but got %d", 5, coldCnt) } if hotCnt != 5 { - t.Fatalf("expected %d hot blocks, but got %d", 5, hotCnt) + t.Errorf("expected %d hot blocks, but got %d", 5, hotCnt) } } if cfg.EnableFullCompaction && !cfg.EnableGC { if coldCnt != 3 { - t.Fatalf("expected %d cold blocks, but got %d", 3, coldCnt) + t.Errorf("expected %d cold blocks, but got %d", 3, coldCnt) } if hotCnt != 7 { - t.Fatalf("expected %d hot blocks, but got %d", 7, hotCnt) + t.Errorf("expected %d hot blocks, but got %d", 7, hotCnt) } } if cfg.EnableFullCompaction && cfg.EnableGC { if coldCnt != 2 { - t.Fatalf("expected %d cold blocks, but got %d", 2, coldCnt) + t.Errorf("expected %d cold blocks, but got %d", 2, coldCnt) } if hotCnt != 7 { - t.Fatalf("expected %d hot blocks, but got %d", 7, hotCnt) + t.Errorf("expected %d hot blocks, but got %d", 7, hotCnt) } } }