X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=blobdiff_plain;f=netsync%2Fchainmgr%2Fblock_process_test.go;h=7663dc8491bbcfad9c60b332010a5ecc6143368f;hp=5bc2075fca268f0161a7cd5168201fa7f4e35b29;hb=669d176c004324fe81a26261a6e41ddea95b6f17;hpb=bc213b29d91743bb9cb23c043f2856f47b34bb3e;ds=sidebyside diff --git a/netsync/chainmgr/block_process_test.go b/netsync/chainmgr/block_process_test.go index 5bc2075f..7663dc84 100644 --- a/netsync/chainmgr/block_process_test.go +++ b/netsync/chainmgr/block_process_test.go @@ -8,6 +8,8 @@ import ( "time" dbm "github.com/vapor/database/leveldb" + "github.com/vapor/netsync/peers" + "github.com/vapor/protocol/bc/types" "github.com/vapor/test/mock" ) @@ -21,30 +23,52 @@ func TestBlockProcess(t *testing.T) { testDB := dbm.NewDB("testdb", "leveldb", tmp) defer testDB.Close() + cases := []struct { + blocks []*types.Block + startHeight uint64 + stopHeight uint64 + }{ + { + blocks: mockBlocks(nil, 200), + startHeight: 100, + stopHeight: 200, + }, + { + blocks: mockBlocks(nil, 200), + startHeight: 110, + stopHeight: 100, + }, + { + blocks: mockErrorBlocks(nil, 200, 150), + startHeight: 100, + stopHeight: 149, + }, + } s := newStorage(testDB) mockChain := mock.NewChain(nil) - blockNum := 200 - blocks := mockBlocks(nil, uint64(blockNum)) - for i := 0; i <= blockNum/2; i++ { - mockChain.SetBlockByHeight(uint64(i), blocks[i]) - mockChain.SetBestBlockHeader(&blocks[i].BlockHeader) - } + for i, c := range cases { + for i := 0; i <= len(c.blocks)/2; i++ { + mockChain.SetBlockByHeight(uint64(i), c.blocks[i]) + mockChain.SetBestBlockHeader(&c.blocks[i].BlockHeader) + } - if err := s.writeBlocks("testPeer", blocks); err != nil { - t.Fatal(err) - } + if err := s.writeBlocks("testPeer", c.blocks); err != nil { + t.Fatal(err) + } + + bp := newBlockProcessor(mockChain, s, peers.NewPeerSet(nil)) + downloadNotifyCh := make(chan struct{}, 1) + ProcessStopCh := make(chan struct{}) + var wg sync.WaitGroup + go func() { + time.Sleep(1 * time.Second) + close(downloadNotifyCh) + }() + wg.Add(1) - bp := newBlockProcessor(mockChain, s, nil) - downloadNotifyCh := make(chan struct{}, 1) - ProcessStopCh := make(chan struct{}) - var wg sync.WaitGroup - go func() { - time.Sleep(1 * time.Second) - close(downloadNotifyCh) - }() - wg.Add(1) - bp.process(downloadNotifyCh, ProcessStopCh, uint64(blockNum/2), &wg) - if bp.chain.BestBlockHeight() != uint64(blockNum) { - t.Fatalf("TestBlockProcess fail: got %d want %d", bp.chain.BestBlockHeight(), blockNum) + bp.process(downloadNotifyCh, ProcessStopCh, c.startHeight, &wg) + if bp.chain.BestBlockHeight() != c.stopHeight { + t.Fatalf("TestBlockProcess index: %d fail: got %d want %d", i, bp.chain.BestBlockHeight(), c.stopHeight) + } } }