github.com/aergoio/aergo@v1.3.1/syncer/hashfetcher_test.go (about)

     1  package syncer
     2  
     3  import (
     4  	"github.com/aergoio/aergo/chain"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/aergoio/aergo/message"
     9  	"github.com/aergoio/aergo/types"
    10  )
    11  
    12  func TestHashFetcher_normal(t *testing.T) {
    13  	remoteChainLen := 100
    14  	localChainLen := 99
    15  	targetNo := uint64(99)
    16  
    17  	//ancestor = 0
    18  	remoteChain := chain.InitStubBlockChain(nil, remoteChainLen)
    19  	localChain := chain.InitStubBlockChain(remoteChain.Blocks[0:1], localChainLen)
    20  
    21  	remoteChains := []*chain.StubBlockChain{remoteChain}
    22  	peers := makeStubPeerSet(remoteChains)
    23  
    24  	//set debug property
    25  	testCfg := *SyncerCfg
    26  	testCfg.maxHashReqSize = TestMaxHashReqSize
    27  	testCfg.maxBlockReqSize = TestMaxBlockFetchSize
    28  	testCfg.debugContext = &SyncerDebug{t: t, expAncestor: 0}
    29  	testCfg.debugContext.debugHashFetcher = true
    30  	testCfg.debugContext.targetNo = targetNo
    31  
    32  	//set ctx because finder is skipped
    33  	ctx := types.NewSyncCtx(1, "peer-0", targetNo, uint64(localChain.Best), nil)
    34  	ancestorInfo := remoteChain.GetBlockInfo(0)
    35  
    36  	syncer := NewTestSyncer(t, localChain, remoteChain, peers, &testCfg)
    37  	syncer.realSyncer.ctx = ctx
    38  	syncer.realSyncer.Seq = 1
    39  	seq := syncer.realSyncer.GetSeq()
    40  
    41  	syncer.start()
    42  
    43  	//ancestor of ctx will be set by FinderResult
    44  	syncer.stubRequester.TellTo(message.SyncerSvc, &message.FinderResult{Seq: seq, Ancestor: ancestorInfo, Err: nil})
    45  
    46  	syncer.waitStop()
    47  }
    48  
    49  //test if hashfetcher stops successfully while waiting to send HashSet to resultCh
    50  func TestHashFetcher_quit(t *testing.T) {
    51  	remoteChainLen := 100
    52  	localChainLen := 99
    53  	targetNo := uint64(99)
    54  
    55  	//ancestor = 0
    56  	remoteChain := chain.InitStubBlockChain(nil, remoteChainLen)
    57  	localChain := chain.InitStubBlockChain(remoteChain.Blocks[0:1], localChainLen)
    58  
    59  	remoteChains := []*chain.StubBlockChain{remoteChain}
    60  	peers := makeStubPeerSet(remoteChains)
    61  
    62  	//set debug property
    63  	testCfg := *SyncerCfg
    64  	testCfg.maxHashReqSize = TestMaxHashReqSize
    65  	testCfg.maxBlockReqSize = TestMaxBlockFetchSize
    66  	testCfg.debugContext = &SyncerDebug{t: t, expAncestor: 0}
    67  	testCfg.debugContext.debugHashFetcher = true
    68  	testCfg.debugContext.BfWaitTime = time.Second * 1000
    69  
    70  	//set ctx because finder is skipped
    71  	ctx := types.NewSyncCtx(1, "peer-0", targetNo, uint64(localChain.Best), nil)
    72  	ancestorInfo := remoteChain.GetBlockInfo(0)
    73  
    74  	syncer := NewTestSyncer(t, localChain, remoteChain, peers, &testCfg)
    75  	syncer.realSyncer.ctx = ctx
    76  
    77  	syncer.start()
    78  
    79  	//ancestor of ctx will be set by FinderResult
    80  	syncer.stubRequester.TellTo(message.SyncerSvc, &message.FinderResult{Seq: syncer.realSyncer.GetSeq(), Ancestor: ancestorInfo, Err: nil})
    81  
    82  	//test if hashfetcher stop
    83  	go func() {
    84  		time.Sleep(time.Second * 1)
    85  		stopSyncer(syncer.stubRequester, syncer.realSyncer.GetSeq(), NameBlockFetcher, ErrQuitBlockFetcher)
    86  	}()
    87  	syncer.waitStop()
    88  }
    89  
    90  func TestHashFetcher_ResponseError(t *testing.T) {
    91  	//TODO test hashfetcher error
    92  	/*
    93  		//make remoteBlockChain
    94  		remoteChain := chain.InitStubBlockChain(nil, 10)
    95  		ancestor := remoteChain.GetBlockByNo(0)
    96  
    97  		ctx := types.NewSyncCtx("p1", 5, 1)
    98  		ctx.SetAncestor(ancestor)
    99  
   100  		syncer := NewStubSyncer(ctx, false, true, false, nil, remoteChain, TestMaxHashReqSize, TestMaxBlockFetchSize)
   101  		syncer.hf.Start()
   102  
   103  		//hashset 2~4, 5~7, 8~9
   104  		//receive GetHash message
   105  		msg := syncer.stubRequester.recvMessage()
   106  		assert.IsTypef(t, &message.GetHashes{}, msg, "invalid message from hf")
   107  		syncer.handleMessageManual(t, msg, ErrGetHashesRspError)
   108  
   109  		//stop
   110  		msg = syncer.stubRequester.recvMessage()
   111  		assert.IsTypef(t, &message.SyncStop{}, msg, "need syncer stop msg")
   112  		syncer.handleMessageManual(t, msg, nil)
   113  
   114  		assert.True(t, syncer.isStop, "hashfetcher finished")
   115  	*/
   116  }