From c47fce8d2da2956ff2a7ce7546e4f472da0aafa2 Mon Sep 17 00:00:00 2001 From: vyzo Date: Sat, 17 Jul 2021 20:40:13 +0300 Subject: [PATCH] test protector support --- blockstore/splitstore/splitstore_test.go | 60 +++++++++++++++++++++--- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/blockstore/splitstore/splitstore_test.go b/blockstore/splitstore/splitstore_test.go index 423a765368c..26e5c3cc0b6 100644 --- a/blockstore/splitstore/splitstore_test.go +++ b/blockstore/splitstore/splitstore_test.go @@ -63,6 +63,20 @@ func testSplitStore(t *testing.T, cfg *Config) { t.Fatal(err) } + // create a garbage block that is protected with a rgistered protector + protected := blocks.NewBlock([]byte("protected!")) + err = hot.Put(protected) + if err != nil { + t.Fatal(err) + } + + // and another one that is not protected + unprotected := blocks.NewBlock([]byte("unprotected!")) + err = hot.Put(unprotected) + if err != nil { + t.Fatal(err) + } + // open the splitstore ss, err := Open("", ds, hot, cold, cfg) if err != nil { @@ -70,6 +84,11 @@ func testSplitStore(t *testing.T, cfg *Config) { } defer ss.Close() //nolint + // register our protector + ss.AddProtector(func(protect func(cid.Cid) error) error { + return protect(protected.Cid()) + }) + err = ss.Start(chain) if err != nil { t.Fatal(err) @@ -132,8 +151,8 @@ func testSplitStore(t *testing.T, cfg *Config) { t.Errorf("expected %d blocks, but got %d", 2, coldCnt) } - if hotCnt != 10 { - t.Errorf("expected %d blocks, but got %d", 10, hotCnt) + if hotCnt != 12 { + t.Errorf("expected %d blocks, but got %d", 12, hotCnt) } // trigger a compaction @@ -146,12 +165,41 @@ func testSplitStore(t *testing.T, cfg *Config) { coldCnt = countBlocks(cold) hotCnt = countBlocks(hot) - if coldCnt != 5 { - t.Errorf("expected %d cold blocks, but got %d", 5, coldCnt) + if coldCnt != 6 { + t.Errorf("expected %d cold blocks, but got %d", 6, coldCnt) + } + + if hotCnt != 18 { + t.Errorf("expected %d hot blocks, but got %d", 18, hotCnt) + } + + // ensure our protected block is still there + has, err := hot.Has(protected.Cid()) + if err != nil { + t.Fatal(err) + } + + if !has { + t.Fatal("protected block is missing from hotstore") + } + + // ensure our unprotected block is in the coldstore now + has, err = hot.Has(unprotected.Cid()) + if err != nil { + t.Fatal(err) + } + + if has { + t.Fatal("unprotected block is still in hotstore") + } + + has, err = cold.Has(unprotected.Cid()) + if err != nil { + t.Fatal(err) } - if hotCnt != 17 { - t.Errorf("expected %d hot blocks, but got %d", 17, hotCnt) + if !has { + t.Fatal("unprotected block is missing from coldstore") } // Make sure we can revert without panicking.