OSDN Git Service

netsync add test case (#365)
[bytom/vapor.git] / netsync / chainmgr / block_process_test.go
index 5bc2075..7663dc8 100644 (file)
@@ -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)
+               }
        }
 }