Skip to content

Commit 0779184

Browse files
authored
fix(testcase): remove peer from PeerManager after testcase (#6486)
* remove peer from PeerManager after testcase * add coverage of NodeInfoServiceTest
1 parent 8aed63c commit 0779184

File tree

16 files changed

+185
-94
lines changed

16 files changed

+185
-94
lines changed

framework/src/main/java/org/tron/common/backup/BackupManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void handleEvent(UdpEvent udpEvent) {
126126
logger.warn("Receive keep alive message from {} is not my member", sender.getHostString());
127127
return;
128128
}
129-
129+
logger.info("Receive keep alive message from {}", sender);
130130
lastKeepAliveTime = System.currentTimeMillis();
131131

132132
KeepAliveMessage keepAliveMessage = (KeepAliveMessage) msg;

framework/src/main/java/org/tron/core/net/peer/PeerStatusCheck.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public void statusCheck() {
4242

4343
long now = System.currentTimeMillis();
4444

45+
if (tronNetDelegate == null) {
46+
//only occurs in mock test. TODO fix test
47+
return;
48+
}
4549
tronNetDelegate.getActivePeer().forEach(peer -> {
4650

4751
boolean isDisconnected = false;

framework/src/test/java/org/tron/common/BaseTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.tron.core.config.args.Args;
2626
import org.tron.core.db.Manager;
2727
import org.tron.core.exception.BalanceInsufficientException;
28+
import org.tron.core.net.peer.PeerConnection;
29+
import org.tron.core.net.peer.PeerManager;
2830
import org.tron.core.store.AccountStore;
2931
import org.tron.protos.Protocol;
3032

@@ -68,6 +70,12 @@ public static void destroy() {
6870
Args.clearParam();
6971
}
7072

73+
public void closePeer() {
74+
for (PeerConnection p : PeerManager.getPeers()) {
75+
PeerManager.remove(p.getChannel());
76+
}
77+
}
78+
7179
public Protocol.Block getSignedBlock(ByteString witness, long time, byte[] privateKey) {
7280
long blockTime = System.currentTimeMillis() / 3000 * 3000;
7381
if (time != 0) {

framework/src/test/java/org/tron/core/net/P2pEventHandlerImplTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import java.util.ArrayList;
77
import java.util.List;
88
import org.junit.Assert;
9+
import org.junit.BeforeClass;
910
import org.junit.Test;
1011
import org.mockito.Mockito;
12+
import org.tron.common.BaseTest;
1113
import org.tron.common.parameter.CommonParameter;
1214
import org.tron.common.utils.Sha256Hash;
1315
import org.tron.core.Constant;
@@ -20,12 +22,15 @@
2022
import org.tron.protos.Protocol;
2123
import org.tron.protos.Protocol.Inventory.InventoryType;
2224

23-
public class P2pEventHandlerImplTest {
25+
public class P2pEventHandlerImplTest extends BaseTest {
26+
27+
@BeforeClass
28+
public static void init() throws Exception {
29+
Args.setParam(new String[] {"--output-directory", dbPath(), "--debug"}, Constant.TEST_CONF);
30+
}
2431

2532
@Test
2633
public void testProcessInventoryMessage() throws Exception {
27-
String[] a = new String[0];
28-
Args.setParam(a, Constant.TESTNET_CONF);
2934
CommonParameter parameter = CommonParameter.getInstance();
3035
parameter.setMaxTps(10);
3136

@@ -114,9 +119,6 @@ public void testProcessInventoryMessage() throws Exception {
114119

115120
@Test
116121
public void testUpdateLastInteractiveTime() throws Exception {
117-
String[] a = new String[0];
118-
Args.setParam(a, Constant.TESTNET_CONF);
119-
120122
PeerConnection peer = new PeerConnection();
121123
P2pEventHandlerImpl p2pEventHandler = new P2pEventHandlerImpl();
122124

framework/src/test/java/org/tron/core/net/messagehandler/MessageHandlerTest.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
import static org.mockito.Mockito.mock;
44

55
import com.google.protobuf.ByteString;
6-
import java.lang.reflect.Field;
76
import java.net.InetSocketAddress;
8-
import java.util.ArrayList;
9-
import java.util.Collections;
7+
import org.junit.After;
108
import org.junit.AfterClass;
119
import org.junit.Assert;
12-
import org.junit.Before;
1310
import org.junit.BeforeClass;
1411
import org.junit.ClassRule;
1512
import org.junit.Test;
@@ -63,14 +60,10 @@ public static void destroy() {
6360
context.destroy();
6461
}
6562

66-
@Before
63+
@After
6764
public void clearPeers() {
68-
try {
69-
Field field = PeerManager.class.getDeclaredField("peers");
70-
field.setAccessible(true);
71-
field.set(PeerManager.class, Collections.synchronizedList(new ArrayList<>()));
72-
} catch (NoSuchFieldException | IllegalAccessException e) {
73-
//ignore
65+
for (PeerConnection p : PeerManager.getPeers()) {
66+
PeerManager.remove(p.getChannel());
7467
}
7568
}
7669

framework/src/test/java/org/tron/core/net/messagehandler/PbftMsgHandlerTest.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44

55
import com.google.protobuf.ByteString;
66
import java.io.File;
7-
import java.lang.reflect.Field;
87
import java.net.InetSocketAddress;
9-
import java.util.ArrayList;
10-
import java.util.Collections;
118
import org.bouncycastle.util.encoders.Hex;
9+
import org.junit.After;
1210
import org.junit.AfterClass;
1311
import org.junit.Assert;
14-
import org.junit.Before;
1512
import org.junit.BeforeClass;
1613
import org.junit.Test;
1714
import org.mockito.Mockito;
@@ -65,14 +62,10 @@ public static void destroy() {
6562
FileUtil.deleteDir(new File(dbPath));
6663
}
6764

68-
@Before
65+
@After
6966
public void clearPeers() {
70-
try {
71-
Field field = PeerManager.class.getDeclaredField("peers");
72-
field.setAccessible(true);
73-
field.set(PeerManager.class, Collections.synchronizedList(new ArrayList<>()));
74-
} catch (NoSuchFieldException | IllegalAccessException e) {
75-
//ignore
67+
for (PeerConnection p : PeerManager.getPeers()) {
68+
PeerManager.remove(p.getChannel());
7669
}
7770
}
7871

framework/src/test/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandlerTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.tron.core.net.message.sync.BlockInventoryMessage;
2525
import org.tron.core.net.message.sync.SyncBlockChainMessage;
2626
import org.tron.core.net.peer.PeerConnection;
27+
import org.tron.core.net.peer.PeerManager;
2728
import org.tron.p2p.connection.Channel;
2829

2930
public class SyncBlockChainMsgHandlerTest {
@@ -109,6 +110,9 @@ public void testProcessMessage() throws Exception {
109110

110111
@AfterClass
111112
public static void destroy() {
113+
for (PeerConnection p : PeerManager.getPeers()) {
114+
PeerManager.remove(p.getChannel());
115+
}
112116
context.destroy();
113117
Args.clearParam();
114118
}

framework/src/test/java/org/tron/core/net/peer/PeerConnectionTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
import java.util.LinkedList;
88
import java.util.List;
99

10+
import org.junit.AfterClass;
1011
import org.junit.Assert;
12+
import org.junit.BeforeClass;
1113
import org.junit.Test;
1214
import org.mockito.Mockito;
1315
import org.tron.common.overlay.message.Message;
16+
import org.tron.common.parameter.CommonParameter;
1417
import org.tron.common.utils.Pair;
1518
import org.tron.common.utils.ReflectUtils;
1619
import org.tron.common.utils.Sha256Hash;
1720
import org.tron.core.capsule.BlockCapsule;
21+
import org.tron.core.config.args.Args;
1822
import org.tron.core.net.message.adv.InventoryMessage;
1923
import org.tron.core.net.message.handshake.HelloMessage;
2024
import org.tron.core.net.message.keepalive.PingMessage;
@@ -26,6 +30,18 @@
2630

2731
public class PeerConnectionTest {
2832

33+
@BeforeClass
34+
public static void initArgs() {
35+
CommonParameter.getInstance().setRateLimiterSyncBlockChain(10);
36+
CommonParameter.getInstance().setRateLimiterFetchInvData(10);
37+
CommonParameter.getInstance().setRateLimiterDisconnect(10);
38+
}
39+
40+
@AfterClass
41+
public static void destroy() {
42+
Args.clearParam();
43+
}
44+
2945
@Test
3046
public void testVariableDefaultValue() {
3147
PeerConnection peerConnection = new PeerConnection();

framework/src/test/java/org/tron/core/net/peer/PeerManagerTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,40 @@
88
import java.util.Collections;
99
import java.util.List;
1010

11+
import org.junit.After;
12+
import org.junit.AfterClass;
1113
import org.junit.Assert;
14+
import org.junit.BeforeClass;
1215
import org.junit.Test;
1316
import org.mockito.Mockito;
1417
import org.springframework.context.ApplicationContext;
18+
import org.tron.common.parameter.CommonParameter;
1519
import org.tron.common.utils.ReflectUtils;
20+
import org.tron.core.config.args.Args;
1621
import org.tron.p2p.connection.Channel;
1722

1823
public class PeerManagerTest {
1924
List<InetSocketAddress> relayNodes = new ArrayList<>();
2025

26+
@BeforeClass
27+
public static void initArgs() {
28+
CommonParameter.getInstance().setRateLimiterSyncBlockChain(10);
29+
CommonParameter.getInstance().setRateLimiterFetchInvData(10);
30+
CommonParameter.getInstance().setRateLimiterDisconnect(10);
31+
}
32+
33+
@AfterClass
34+
public static void destroy() {
35+
Args.clearParam();
36+
}
37+
38+
@After
39+
public void clearPeers() {
40+
for (PeerConnection p : PeerManager.getPeers()) {
41+
PeerManager.remove(p.getChannel());
42+
}
43+
}
44+
2145
@Test
2246
public void testAdd() throws Exception {
2347
Field field = PeerManager.class.getDeclaredField("peers");

framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public static void init() throws IOException {
5353

5454
@AfterClass
5555
public static void after() {
56+
for (PeerConnection p : PeerManager.getPeers()) {
57+
PeerManager.remove(p.getChannel());
58+
}
5659
Args.clearParam();
5760
context.destroy();
5861
}

0 commit comments

Comments
 (0)