github.com/aergoio/aergo@v1.3.1/syncer/finder_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 ) 10 11 func testFullscanSucceed(t *testing.T, expAncestor uint64) { 12 logger.Debug().Uint64("expAncestor", expAncestor).Msg("testfullscan") 13 14 remoteChainLen := 11 15 localChainLen := 10 16 targetNo := uint64(11) 17 18 remoteChain := chain.InitStubBlockChain(nil, remoteChainLen) 19 localChain := chain.InitStubBlockChain(remoteChain.Blocks[0:expAncestor+1], localChainLen-int(expAncestor+1)) 20 21 remoteChains := []*chain.StubBlockChain{remoteChain} 22 peers := makeStubPeerSet(remoteChains) 23 24 //set debug property 25 testCfg := *SyncerCfg 26 testCfg.useFullScanOnly = true 27 testCfg.debugContext = &SyncerDebug{t: t, debugFinder: true, expAncestor: int(expAncestor)} 28 29 syncer := NewTestSyncer(t, localChain, remoteChain, peers, &testCfg) 30 31 syncer.start() 32 33 syncReq := &message.SyncStart{PeerID: targetPeerID, TargetNo: targetNo} 34 syncer.stubRequester.TellTo(message.SyncerSvc, syncReq) 35 36 syncer.waitStop() 37 } 38 39 func TestFinder_fullscan_found(t *testing.T) { 40 for i := 0; i < 10; i++ { 41 testFullscanSucceed(t, uint64(i)) 42 } 43 } 44 45 func TestFinder_fullscan_notfound(t *testing.T) { 46 remoteChainLen := 1002 47 localChainLen := 1000 48 targetNo := uint64(1000) 49 50 remoteChain := chain.InitStubBlockChain(nil, remoteChainLen) 51 localChain := chain.InitStubBlockChain(nil, localChainLen) 52 53 remoteChains := []*chain.StubBlockChain{remoteChain} 54 peers := makeStubPeerSet(remoteChains) 55 56 //set debug property 57 testCfg := *SyncerCfg 58 testCfg.useFullScanOnly = true 59 testCfg.debugContext = &SyncerDebug{t: t, debugFinder: true, expAncestor: -1} 60 61 syncer := NewTestSyncer(t, localChain, remoteChain, peers, &testCfg) 62 63 syncer.start() 64 65 syncReq := &message.SyncStart{PeerID: targetPeerID, TargetNo: targetNo} 66 syncer.stubRequester.TellTo(message.SyncerSvc, syncReq) 67 68 syncer.waitStop() 69 } 70 71 //test finder stop when close finder.quitCh 72 func TestFinder_timeout(t *testing.T) { 73 logger.Debug().Int("expAncestor", -1).Msg("testfullscan") 74 75 remoteChainLen := 1001 76 localChainLen := 1000 77 targetNo := uint64(1000) 78 79 remoteChain := chain.InitStubBlockChain(nil, remoteChainLen) 80 localChain := chain.InitStubBlockChain(remoteChain.Blocks[0:1], localChainLen-1) 81 82 remoteChains := []*chain.StubBlockChain{remoteChain} 83 peers := makeStubPeerSet(remoteChains) 84 85 //set debug property 86 testCfg := *SyncerCfg 87 testCfg.fetchTimeOut = time.Millisecond * 500 88 testCfg.debugContext = &SyncerDebug{t: t, debugFinder: true, expAncestor: -1, expErrResult: ErrHubFutureTimeOut} 89 peers[0].timeDelaySec = time.Second * 1 90 91 syncer := NewTestSyncer(t, localChain, remoteChain, peers, &testCfg) 92 93 syncer.start() 94 95 syncReq := &message.SyncStart{PeerID: targetPeerID, TargetNo: targetNo} 96 syncer.stubRequester.TellTo(message.SyncerSvc, syncReq) 97 98 syncer.waitStop() 99 }