|
21 | 21 | import org.tron.core.net.service.adv.AdvService; |
22 | 22 | import org.tron.protos.Protocol; |
23 | 23 |
|
24 | | - |
25 | | - |
26 | 24 | public class FetchInvDataMsgHandlerTest { |
27 | 25 |
|
28 | 26 | @Test |
@@ -62,4 +60,37 @@ public void testProcessMessage() throws Exception { |
62 | 60 | new FetchInvDataMessage(blockIds, Protocol.Inventory.InventoryType.BLOCK)); |
63 | 61 | Assert.assertNotNull(syncBlockIdCache.getIfPresent(blockId)); |
64 | 62 | } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testSyncFetchCheck() { |
| 66 | + BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 10000L); |
| 67 | + List<Sha256Hash> blockIds = new LinkedList<>(); |
| 68 | + blockIds.add(blockId); |
| 69 | + FetchInvDataMessage msg = |
| 70 | + new FetchInvDataMessage(blockIds, Protocol.Inventory.InventoryType.BLOCK); |
| 71 | + |
| 72 | + PeerConnection peer = Mockito.mock(PeerConnection.class); |
| 73 | + Mockito.when(peer.isNeedSyncFromUs()).thenReturn(true); |
| 74 | + Cache<Item, Long> advInvSpread = CacheBuilder.newBuilder().maximumSize(100) |
| 75 | + .expireAfterWrite(1, TimeUnit.HOURS).recordStats().build(); |
| 76 | + Mockito.when(peer.getAdvInvSpread()).thenReturn(advInvSpread); |
| 77 | + |
| 78 | + FetchInvDataMsgHandler fetchInvDataMsgHandler = new FetchInvDataMsgHandler(); |
| 79 | + |
| 80 | + try { |
| 81 | + Mockito.when(peer.getLastSyncBlockId()) |
| 82 | + .thenReturn(new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 1000L)); |
| 83 | + fetchInvDataMsgHandler.processMessage(peer, msg); |
| 84 | + } catch (Exception e) { |
| 85 | + Assert.assertEquals(e.getMessage(), "maxBlockNum: 1000, blockNum: 10000"); |
| 86 | + } |
| 87 | + |
| 88 | + try { |
| 89 | + Mockito.when(peer.getLastSyncBlockId()) |
| 90 | + .thenReturn(new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 20000L)); |
| 91 | + fetchInvDataMsgHandler.processMessage(peer, msg); |
| 92 | + } catch (Exception e) { |
| 93 | + Assert.assertEquals(e.getMessage(), "minBlockNum: 16000, blockNum: 10000"); |
| 94 | + } |
| 95 | + } |
65 | 96 | } |
0 commit comments