Skip to content

Commit 811582e

Browse files
BarbatosBarbatos
authored andcommitted
test(p2p): improve test coverage from 35% to 76%
Add comprehensive unit tests to meet CI coverage gate requirements (changed files > 60%, overall delta < -0.1%). New test files (27): - connection/: Channel, ChannelManager, HandshakeService, KeepAlive, NodeDetect, ConnPoolService, UpgradeController, P2pProtobufDecoder, MessageHandler, PeerClient, P2pChannelInitializer, StatusMessage, P2pDisconnectMessage - dns/: DnsManager, AliClient, AwsClient, PublishService - discover/: FindNodeMessage, NeighborsMessage, Message, PacketDecoder, MessageHandler, DiscoverTask - utils/web3j: Numeric, Strings, Hash, ECKeyPair - stats: StatsManager - exception: DnsException - P2pService, P2pConfig Exclude proto-generated code from jacoco coverage calculation (consistent with checkstyle exclusion).
1 parent cfc9f63 commit 811582e

33 files changed

Lines changed: 5744 additions & 0 deletions

p2p/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,9 @@ jacocoTestReport {
9191
xml.enabled = true
9292
html.enabled = true
9393
}
94+
afterEvaluate {
95+
classDirectories.from = classDirectories.files.collect {
96+
fileTree(dir: it, exclude: '**/protos/**')
97+
}
98+
}
9499
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.tron.p2p;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class P2pConfigTest {
7+
8+
@Test
9+
public void testDefaultValues() {
10+
P2pConfig config = new P2pConfig();
11+
Assert.assertNotNull(config.getSeedNodes());
12+
Assert.assertTrue(config.getSeedNodes().isEmpty());
13+
Assert.assertNotNull(config.getActiveNodes());
14+
Assert.assertTrue(config.getActiveNodes().isEmpty());
15+
Assert.assertNotNull(config.getTrustNodes());
16+
Assert.assertTrue(config.getTrustNodes().isEmpty());
17+
Assert.assertNotNull(config.getNodeID());
18+
Assert.assertEquals(64, config.getNodeID().length);
19+
Assert.assertEquals(18888, config.getPort());
20+
Assert.assertEquals(1, config.getNetworkId());
21+
Assert.assertEquals(8, config.getMinConnections());
22+
Assert.assertEquals(50, config.getMaxConnections());
23+
Assert.assertEquals(2, config.getMinActiveConnections());
24+
Assert.assertEquals(2, config.getMaxConnectionsWithSameIp());
25+
Assert.assertTrue(config.isDiscoverEnable());
26+
Assert.assertFalse(config.isDisconnectionPolicyEnable());
27+
Assert.assertFalse(config.isNodeDetectEnable());
28+
Assert.assertNotNull(config.getTreeUrls());
29+
Assert.assertTrue(config.getTreeUrls().isEmpty());
30+
Assert.assertNotNull(config.getPublishConfig());
31+
}
32+
33+
@Test
34+
public void testSettersAndGetters() {
35+
P2pConfig config = new P2pConfig();
36+
37+
config.setPort(19999);
38+
Assert.assertEquals(19999, config.getPort());
39+
40+
config.setNetworkId(42);
41+
Assert.assertEquals(42, config.getNetworkId());
42+
43+
config.setMinConnections(10);
44+
Assert.assertEquals(10, config.getMinConnections());
45+
46+
config.setMaxConnections(100);
47+
Assert.assertEquals(100, config.getMaxConnections());
48+
49+
config.setMinActiveConnections(5);
50+
Assert.assertEquals(5, config.getMinActiveConnections());
51+
52+
config.setMaxConnectionsWithSameIp(3);
53+
Assert.assertEquals(3, config.getMaxConnectionsWithSameIp());
54+
55+
config.setDiscoverEnable(false);
56+
Assert.assertFalse(config.isDiscoverEnable());
57+
58+
config.setDisconnectionPolicyEnable(true);
59+
Assert.assertTrue(config.isDisconnectionPolicyEnable());
60+
61+
config.setNodeDetectEnable(true);
62+
Assert.assertTrue(config.isNodeDetectEnable());
63+
64+
byte[] customId = new byte[64];
65+
config.setNodeID(customId);
66+
Assert.assertArrayEquals(customId, config.getNodeID());
67+
68+
config.setIp("10.0.0.1");
69+
Assert.assertEquals("10.0.0.1", config.getIp());
70+
71+
config.setLanIp("192.168.0.1");
72+
Assert.assertEquals("192.168.0.1", config.getLanIp());
73+
74+
config.setIpv6("::1");
75+
Assert.assertEquals("::1", config.getIpv6());
76+
}
77+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package org.tron.p2p;
2+
3+
import java.lang.reflect.Field;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
import org.junit.After;
9+
import org.junit.Assert;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.tron.p2p.base.Parameter;
13+
import org.tron.p2p.exception.P2pException;
14+
15+
public class P2pServiceTest {
16+
17+
private P2pService p2pService;
18+
19+
@Before
20+
public void init() {
21+
p2pService = new P2pService();
22+
// Reset handler state
23+
Parameter.handlerList = new ArrayList<>();
24+
Parameter.handlerMap = new HashMap<>();
25+
}
26+
27+
@After
28+
public void cleanup() {
29+
try {
30+
p2pService.close();
31+
} catch (Exception e) {
32+
// ignore cleanup errors
33+
}
34+
}
35+
36+
@Test
37+
public void testGetVersion() {
38+
Assert.assertEquals(Parameter.version, p2pService.getVersion());
39+
}
40+
41+
@Test
42+
public void testRegisterHandler() throws P2pException {
43+
P2pEventHandler handler = new P2pEventHandler() {
44+
{
45+
Set<Byte> types = new HashSet<>();
46+
types.add((byte) 0x50);
47+
this.messageTypes = types;
48+
}
49+
};
50+
p2pService.register(handler);
51+
Assert.assertTrue(Parameter.handlerList.contains(handler));
52+
Assert.assertEquals(handler, Parameter.handlerMap.get((byte) 0x50));
53+
}
54+
55+
@Test(expected = P2pException.class)
56+
public void testRegisterDuplicateTypeThrows() throws P2pException {
57+
P2pEventHandler handler1 = new P2pEventHandler() {
58+
{
59+
Set<Byte> types = new HashSet<>();
60+
types.add((byte) 0x60);
61+
this.messageTypes = types;
62+
}
63+
};
64+
P2pEventHandler handler2 = new P2pEventHandler() {
65+
{
66+
Set<Byte> types = new HashSet<>();
67+
types.add((byte) 0x60);
68+
this.messageTypes = types;
69+
}
70+
};
71+
p2pService.register(handler1);
72+
p2pService.register(handler2); // should throw
73+
}
74+
75+
@Test
76+
public void testRegisterHandlerWithNullMessageTypes() throws P2pException {
77+
P2pEventHandler handler = new P2pEventHandler() {};
78+
// messageTypes is null by default
79+
p2pService.register(handler);
80+
Assert.assertTrue(Parameter.handlerList.contains(handler));
81+
}
82+
83+
@Test
84+
public void testCloseIdempotent() throws Exception {
85+
// Set up minimal config to allow close without NPE
86+
// The close method checks isShutdown flag
87+
Field isShutdownField = P2pService.class.getDeclaredField("isShutdown");
88+
isShutdownField.setAccessible(true);
89+
90+
// First close
91+
isShutdownField.set(p2pService, false);
92+
// We can't call start() without real network, but we can test the idempotent close
93+
isShutdownField.set(p2pService, true);
94+
// Second close should be a no-op
95+
p2pService.close();
96+
Assert.assertTrue((boolean) isShutdownField.get(p2pService));
97+
}
98+
99+
@Test
100+
public void testGetP2pStats() {
101+
// statsManager is initialized in constructor, getP2pStats should work
102+
Assert.assertNotNull(p2pService.getP2pStats());
103+
}
104+
}

0 commit comments

Comments
 (0)