OSDN Git Service

V0.1 vote result cache (#182)
[bytom/vapor.git] / database / cache_test.go
index d7116ec..9e71dc2 100644 (file)
@@ -5,6 +5,7 @@ import (
 
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
 
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
+       "github.com/vapor/protocol/state"
 )
 
 func TestBlockCache(t *testing.T) {
 )
 
 func TestBlockCache(t *testing.T) {
@@ -15,11 +16,21 @@ func TestBlockCache(t *testing.T) {
                        },
                }
        }
                        },
                }
        }
+       newVoteResult := func(seq uint64) *state.VoteResult {
+               return &state.VoteResult{
+                       Seq: seq,
+               }
+       }
        blocks := make(map[bc.Hash]*types.Block)
        for i := 0; i < maxCachedBlockHeaders+10; i++ {
                block := newBlock(uint64(i))
                blocks[block.Hash()] = block
        }
        blocks := make(map[bc.Hash]*types.Block)
        for i := 0; i < maxCachedBlockHeaders+10; i++ {
                block := newBlock(uint64(i))
                blocks[block.Hash()] = block
        }
+       voteResults := make(map[uint64]*state.VoteResult)
+       for i := 0; i < maxCachedVoteResults+10; i++ {
+               voteResult := newVoteResult(uint64(i))
+               voteResults[voteResult.Seq] = voteResult
+       }
 
        fillBlockHeaderFn := func(hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
                return &blocks[*hash].BlockHeader, nil
 
        fillBlockHeaderFn := func(hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
                return &blocks[*hash].BlockHeader, nil
@@ -29,7 +40,11 @@ func TestBlockCache(t *testing.T) {
                return blocks[*hash].Transactions, nil
        }
 
                return blocks[*hash].Transactions, nil
        }
 
-       cache := newBlockCache(fillBlockHeaderFn, fillBlockTxsFn)
+       fillVoteResultFn := func(seq uint64) (*state.VoteResult, error) {
+               return voteResults[seq], nil
+       }
+
+       cache := newBlockCache(fillBlockHeaderFn, fillBlockTxsFn, fillVoteResultFn)
 
        for i := 0; i < maxCachedBlockHeaders+10; i++ {
                block := newBlock(uint64(i))
 
        for i := 0; i < maxCachedBlockHeaders+10; i++ {
                block := newBlock(uint64(i))
@@ -52,4 +67,23 @@ func TestBlockCache(t *testing.T) {
                        t.Fatalf("can't find new block")
                }
        }
                        t.Fatalf("can't find new block")
                }
        }
+
+       for i := 0; i < maxCachedVoteResults+10; i++ {
+               voteResult := newVoteResult(uint64(i))
+               cache.lookupVoteResult(voteResult.Seq)
+       }
+
+       for i := 0; i < 10; i++ {
+               voteResult := newVoteResult(uint64(i))
+               if v, _ := cache.getVoteResult(voteResult.Seq); v != nil {
+                       t.Fatalf("find old vote result")
+               }
+       }
+
+       for i := 10; i < maxCachedVoteResults+10; i++ {
+               voteResult := newVoteResult(uint64(i))
+               if v, _ := cache.getVoteResult(voteResult.Seq); v == nil {
+                       t.Fatalf("can't find new vote result")
+               }
+       }
 }
 }