OSDN Git Service

add parallel fast sync support (#238)
[bytom/vapor.git] / netsync / chainmgr / block_process_test.go
1 package chainmgr
2
3 import (
4         "io/ioutil"
5         "os"
6         "sync"
7         "testing"
8         "time"
9
10         dbm "github.com/vapor/database/leveldb"
11         "github.com/vapor/test/mock"
12 )
13
14 func TestBlockProcess(t *testing.T) {
15         tmp, err := ioutil.TempDir(".", "")
16         if err != nil {
17                 t.Fatal(err)
18         }
19         defer os.RemoveAll(tmp)
20
21         testDB := dbm.NewDB("testdb", "leveldb", tmp)
22         defer testDB.Close()
23
24         s := newStorage(testDB)
25         mockChain := mock.NewChain(nil)
26         blockNum := 200
27         blocks := mockBlocks(nil, uint64(blockNum))
28         for i := 0; i <= blockNum/2; i++ {
29                 mockChain.SetBlockByHeight(uint64(i), blocks[i])
30                 mockChain.SetBestBlockHeader(&blocks[i].BlockHeader)
31         }
32
33         if err := s.writeBlocks("testPeer", blocks); err != nil {
34                 t.Fatal(err)
35         }
36
37         bp := newBlockProcessor(mockChain, s, nil)
38         downloadNotifyCh := make(chan struct{}, 1)
39         ProcessStopCh := make(chan struct{})
40         var wg sync.WaitGroup
41         go func() {
42                 time.Sleep(1 * time.Second)
43                 close(downloadNotifyCh)
44         }()
45         wg.Add(1)
46         bp.process(downloadNotifyCh, ProcessStopCh, &wg)
47         if bp.chain.BestBlockHeight() != uint64(blockNum) {
48                 t.Fatalf("TestBlockProcess fail: got %d want %d", bp.chain.BestBlockHeight(), blockNum)
49         }
50 }