X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=database%2Fcache_test.go;h=b88ad3a343667d5f83309b0b8c59fe698b784a8c;hb=d762991e4a41d3e4b9dbab0a48548d9ba8f6fbc6;hp=673ab7d56c5619f254a8d0faed9b5f2003398ff1;hpb=db158dcf09436b003defd333f1a665e7e051d820;p=bytom%2Fvapor.git diff --git a/database/cache_test.go b/database/cache_test.go index 673ab7d5..b88ad3a3 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,149 @@ func TestBlockCache(t *testing.T) { }, } } + + newConsensusResult := func(seq uint64) *state.ConsensusResult { + return &state.ConsensusResult{ + Seq: seq, + } + } + blocks := make(map[bc.Hash]*types.Block) - for i := 0; i < maxCachedBlocks+10; i++ { + blockHashes := make(map[uint64]*bc.Hash) + blockIndexHashes := make(map[uint64][]*bc.Hash) + for i := 0; i < maxCachedBlockHeaders+10; i++ { + block := newBlock(uint64(i)) + hash := block.Hash() + blocks[hash] = block + blockHashes[block.Height] = &hash + blockIndexHashes[block.Height] = append(blockIndexHashes[block.Height], &hash) + } + + consensusResults := make(map[uint64]*state.ConsensusResult) + for i := 0; i < maxCachedConsensusResults+10; i++ { + consensusResult := newConsensusResult(uint64(i)) + consensusResults[consensusResult.Seq] = consensusResult + } + + fillBlockHeaderFn := func(hash *bc.Hash) (*types.BlockHeader, error) { + return &blocks[*hash].BlockHeader, nil + } + + fillBlockTxsFn := func(hash *bc.Hash) ([]*types.Tx, error) { + return blocks[*hash].Transactions, nil + } + + fillBlockHashesFn := func(height uint64) ([]*bc.Hash, error) { + return blockIndexHashes[height], nil + } + + fillMainChainHashFn := func(height uint64) (*bc.Hash, error) { + return blockHashes[height], nil + } + + fillConsensusResultFn := func(seq uint64) (*state.ConsensusResult, error) { + return consensusResults[seq], nil + } + + cache := newCache(fillBlockHeaderFn, fillBlockTxsFn, fillBlockHashesFn, fillMainChainHashFn, fillConsensusResultFn) + for i := 0; i < maxCachedBlockHeaders+10; i++ { + block := newBlock(uint64(i)) + hash := block.Hash() + cache.lookupBlockHeader(&hash) + } + + for i := 0; i < 10; i++ { block := newBlock(uint64(i)) - blocks[block.Hash()] = block + hash := block.Hash() + if _, ok := cache.lruBlockHeaders.Get(hash); ok { + t.Fatalf("find old block header") + } } - cache := newBlockCache(func(hash *bc.Hash) (*types.Block, error) { - return blocks[*hash], nil - }) + for i := 10; i < maxCachedBlockHeaders+10; i++ { + block := newBlock(uint64(i)) + hash := block.Hash() + if _, ok := cache.lruBlockHeaders.Get(hash); !ok { + t.Fatalf("can't find new block header") + } + } - for i := 0; i < maxCachedBlocks+10; i++ { + for i := 0; i < maxCachedBlockTransactions+10; i++ { block := newBlock(uint64(i)) hash := block.Hash() - cache.lookup(&hash) + cache.lookupBlockTxs(&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") + if _, ok := cache.lruBlockTxs.Get(hash); ok { + t.Fatalf("find old block transactions") } } - for i := 10; i < maxCachedBlocks+10; i++ { + for i := 10; i < maxCachedBlockTransactions+10; i++ { block := newBlock(uint64(i)) hash := block.Hash() - if b, _ := cache.get(&hash); b == nil { - t.Fatalf("can't find new block") + if _, ok := cache.lruBlockTxs.Get(hash); !ok { + t.Fatalf("can't find new block transactions") + } + } + + for i := 0; i < maxCachedBlockHashes+10; i++ { + block := newBlock(uint64(i)) + cache.lookupBlockHashesByHeight(block.Height) + } + + for i := 0; i < 10; i++ { + block := newBlock(uint64(i)) + if _, ok := cache.lruBlockHashes.Get(block.Height); ok { + t.Fatalf("find old block Hashes for specified height") + } + } + + for i := 10; i < maxCachedBlockHashes+10; i++ { + block := newBlock(uint64(i)) + if _, ok := cache.lruBlockHashes.Get(block.Height); !ok { + t.Fatalf("can't find new block Hashes for specified height") + } + } + + for i := 0; i < maxCachedMainChainHashes+10; i++ { + block := newBlock(uint64(i)) + cache.lookupMainChainHash(block.Height) + } + + for i := 0; i < 10; i++ { + block := newBlock(uint64(i)) + if _, ok := cache.lruMainChainHashes.Get(block.Height); ok { + t.Fatalf("find old main chain block Hash for specified height") + } + } + + for i := 10; i < maxCachedMainChainHashes+10; i++ { + block := newBlock(uint64(i)) + if _, ok := cache.lruMainChainHashes.Get(block.Height); !ok { + t.Fatalf("can't find new main chain block Hash for specified height") + } + } + + for i := 0; i < maxCachedConsensusResults+10; i++ { + consensusResult := newConsensusResult(uint64(i)) + cache.lookupConsensusResult(consensusResult.Seq) + } + + for i := 0; i < 10; i++ { + consensusResult := newConsensusResult(uint64(i)) + if _, ok := cache.lruConsensusResults.Get(consensusResult.Seq); ok { + t.Fatalf("find old vote result") + } + } + + for i := 10; i < maxCachedConsensusResults+10; i++ { + consensusResult := newConsensusResult(uint64(i)) + if _, ok := cache.lruConsensusResults.Get(consensusResult.Seq); !ok { + t.Fatalf("can't find new vote result") } } }