From 96b1afaa3aa2404f0594fe90cf7b6811a21d9092 Mon Sep 17 00:00:00 2001 From: lbqds Date: Sat, 21 Apr 2018 16:26:05 +0800 Subject: [PATCH] add cache test --- database/leveldb/cache_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 database/leveldb/cache_test.go diff --git a/database/leveldb/cache_test.go b/database/leveldb/cache_test.go new file mode 100644 index 00000000..f24c35c2 --- /dev/null +++ b/database/leveldb/cache_test.go @@ -0,0 +1,49 @@ +package leveldb + +import ( + "testing" + + "github.com/bytom/protocol/bc" + "github.com/bytom/protocol/bc/types" +) + +func TestBlockCache(t *testing.T) { + newBlock := func(h uint64) *types.Block { + return &types.Block{ + BlockHeader: types.BlockHeader{ + Height: h, + }, + } + } + blocks := make(map[bc.Hash]*types.Block) + for i := 0; i < maxCachedBlocks + 10; i++ { + block := newBlock(uint64(i)) + blocks[block.Hash()] = block + } + + cache := newBlockCache(func(hash *bc.Hash) *types.Block { + return blocks[*hash] + }) + + for i := 0; i < maxCachedBlocks + 10; i++ { + block := newBlock(uint64(i)) + hash := block.Hash() + cache.lookup(&hash) + } + + for i := 0; i < 10; i++ { + block := newBlock(uint64(i)) + hash := block.Hash() + if b, _ := cache.get(&hash); b != nil { + t.Fatalf("find old block") + } + } + + for i := 10; i < maxCachedBlocks + 10; i++ { + block := newBlock(uint64(i)) + hash := block.Hash() + if b, _ := cache.get(&hash); b == nil { + t.Fatalf("can't find new block") + } + } +} -- 2.11.0