Skip to content

Commit c929ee3

Browse files
committed
feat(net): add unit test for P2pEventHandlerImpl
1 parent 4108020 commit c929ee3

2 files changed

Lines changed: 111 additions & 1 deletion

File tree

framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.tron.core.net;
22

33
import static org.tron.core.net.message.MessageTypes.INVENTORY;
4-
import static org.tron.core.net.message.MessageTypes.TRX;
54

65
import java.util.HashSet;
76
import java.util.Set;
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package org.tron.core.net;
2+
3+
import static org.mockito.Mockito.mock;
4+
5+
import java.lang.reflect.Method;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
import org.mockito.Mockito;
11+
import org.tron.common.parameter.CommonParameter;
12+
import org.tron.common.utils.Sha256Hash;
13+
import org.tron.core.Constant;
14+
import org.tron.core.config.args.Args;
15+
import org.tron.core.net.message.adv.InventoryMessage;
16+
import org.tron.core.net.peer.PeerConnection;
17+
import org.tron.core.net.service.statistics.PeerStatistics;
18+
import org.tron.protos.Protocol;
19+
20+
public class P2pEventHandlerImplTest {
21+
22+
@Test
23+
public void testProcessInventoryMessage() throws Exception {
24+
String[] a = new String[0];
25+
Args.setParam(a, Constant.TESTNET_CONF);
26+
CommonParameter parameter = CommonParameter.getInstance();
27+
parameter.setMaxTps(10);
28+
29+
PeerStatistics peerStatistics = new PeerStatistics();
30+
31+
PeerConnection peer = mock(PeerConnection.class);
32+
Mockito.when(peer.getPeerStatistics()).thenReturn(peerStatistics);
33+
34+
P2pEventHandlerImpl p2pEventHandler = new P2pEventHandlerImpl();
35+
36+
Method method = p2pEventHandler.getClass()
37+
.getDeclaredMethod("processMessage", PeerConnection.class, byte[].class);
38+
method.setAccessible(true);
39+
40+
int count = peer.getPeerStatistics().messageStatistics.tronInTrxInventoryElement
41+
.getCount(10);
42+
43+
Assert.assertEquals(0, count);
44+
45+
List<Sha256Hash> list = new ArrayList<>();
46+
for (int i = 0; i < 10; i++) {
47+
list.add(new Sha256Hash(i, new byte[32]));
48+
}
49+
50+
InventoryMessage msg = new InventoryMessage(list, Protocol.Inventory.InventoryType.TRX);
51+
52+
method.invoke(p2pEventHandler, peer, msg.getSendBytes());
53+
54+
count = peer.getPeerStatistics().messageStatistics.tronInTrxInventoryElement.getCount(10);
55+
56+
Assert.assertEquals(10, count);
57+
58+
list.clear();
59+
for (int i = 0; i < 100; i++) {
60+
list.add(new Sha256Hash(i, new byte[32]));
61+
}
62+
63+
msg = new InventoryMessage(list, Protocol.Inventory.InventoryType.TRX);
64+
65+
method.invoke(p2pEventHandler, peer, msg.getSendBytes());
66+
67+
count = peer.getPeerStatistics().messageStatistics.tronInTrxInventoryElement.getCount(10);
68+
69+
Assert.assertEquals(110, count);
70+
71+
list.clear();
72+
for (int i = 0; i < 100; i++) {
73+
list.add(new Sha256Hash(i, new byte[32]));
74+
}
75+
76+
msg = new InventoryMessage(list, Protocol.Inventory.InventoryType.TRX);
77+
78+
method.invoke(p2pEventHandler, peer, msg.getSendBytes());
79+
80+
count = peer.getPeerStatistics().messageStatistics.tronInTrxInventoryElement.getCount(10);
81+
82+
Assert.assertEquals(110, count);
83+
84+
list.clear();
85+
for (int i = 0; i < 200; i++) {
86+
list.add(new Sha256Hash(i, new byte[32]));
87+
}
88+
89+
msg = new InventoryMessage(list, Protocol.Inventory.InventoryType.BLOCK);
90+
91+
method.invoke(p2pEventHandler, peer, msg.getSendBytes());
92+
93+
count = peer.getPeerStatistics().messageStatistics.tronInBlockInventoryElement.getCount(10);
94+
95+
Assert.assertEquals(200, count);
96+
97+
list.clear();
98+
for (int i = 0; i < 100; i++) {
99+
list.add(new Sha256Hash(i, new byte[32]));
100+
}
101+
102+
msg = new InventoryMessage(list, Protocol.Inventory.InventoryType.BLOCK);
103+
104+
method.invoke(p2pEventHandler, peer, msg.getSendBytes());
105+
106+
count = peer.getPeerStatistics().messageStatistics.tronInBlockInventoryElement.getCount(10);
107+
108+
Assert.assertEquals(300, count);
109+
110+
}
111+
}

0 commit comments

Comments
 (0)