X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=blobdiff_plain;f=database%2Fcache_test.go;h=9e71dc205f71834f54d10e52b2674d70baf98f22;hp=673ab7d56c5619f254a8d0faed9b5f2003398ff1;hb=0515c69584c7cfb67df0eebc095001e177979200;hpb=db158dcf09436b003defd333f1a665e7e051d820 diff --git a/database/cache_test.go b/database/cache_test.go index 673ab7d5..9e71dc20 100644 --- a/database/cache_test.go +++ b/database/cache_test.go @@ -5,6 +5,7 @@ import ( "github.com/vapor/protocol/bc" "github.com/vapor/protocol/bc/types" + "github.com/vapor/protocol/state" ) func TestBlockCache(t *testing.T) { @@ -15,35 +16,74 @@ 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 < maxCachedBlocks+10; i++ { + 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 + } + + fillBlockTxsFn := func(hash *bc.Hash) ([]*types.Tx, error) { + return blocks[*hash].Transactions, nil + } + + fillVoteResultFn := func(seq uint64) (*state.VoteResult, error) { + return voteResults[seq], nil + } - cache := newBlockCache(func(hash *bc.Hash) (*types.Block, error) { - return blocks[*hash], nil - }) + cache := newBlockCache(fillBlockHeaderFn, fillBlockTxsFn, fillVoteResultFn) - for i := 0; i < maxCachedBlocks+10; i++ { + for i := 0; i < maxCachedBlockHeaders+10; i++ { block := newBlock(uint64(i)) hash := block.Hash() - cache.lookup(&hash) + cache.lookupBlockHeader(&hash, block.Height) } for i := 0; i < 10; i++ { block := newBlock(uint64(i)) hash := block.Hash() - if b, _ := cache.get(&hash); b != nil { + if b, _ := cache.getBlockHeader(&hash); b != nil { t.Fatalf("find old block") } } - for i := 10; i < maxCachedBlocks+10; i++ { + for i := 10; i < maxCachedBlockHeaders+10; i++ { block := newBlock(uint64(i)) hash := block.Hash() - if b, _ := cache.get(&hash); b == nil { + if b, _ := cache.getBlockHeader(&hash); b == nil { 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") + } + } }