OSDN Git Service

add cache test
authorlbqds <lbqds@outlook.com>
Sat, 21 Apr 2018 08:26:05 +0000 (16:26 +0800)
committerlbqds <lbqds@outlook.com>
Sat, 21 Apr 2018 08:26:05 +0000 (16:26 +0800)
database/leveldb/cache_test.go [new file with mode: 0644]

diff --git a/database/leveldb/cache_test.go b/database/leveldb/cache_test.go
new file mode 100644 (file)
index 0000000..f24c35c
--- /dev/null
@@ -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")
+               }
+       }
+}