Skip to content

Commit e242ae1

Browse files
committed
add testcase PeerStatusCheckTest
1 parent f99d216 commit e242ae1

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.tron.core.net.peer;
2+
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.Mockito.spy;
5+
6+
import io.netty.channel.ChannelHandlerContext;
7+
import java.io.IOException;
8+
import java.net.InetSocketAddress;
9+
import org.junit.After;
10+
import org.junit.Assert;
11+
import org.junit.Before;
12+
import org.junit.Rule;
13+
import org.junit.Test;
14+
import org.junit.rules.TemporaryFolder;
15+
import org.mockito.Mockito;
16+
import org.tron.common.application.TronApplicationContext;
17+
import org.tron.common.utils.ReflectUtils;
18+
import org.tron.core.Constant;
19+
import org.tron.core.capsule.BlockCapsule.BlockId;
20+
import org.tron.core.config.DefaultConfig;
21+
import org.tron.core.config.Parameter.NetConstants;
22+
import org.tron.core.config.args.Args;
23+
import org.tron.p2p.connection.Channel;
24+
25+
26+
public class PeerStatusCheckTest {
27+
28+
protected TronApplicationContext context;
29+
private PeerStatusCheck service;
30+
@Rule
31+
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
32+
33+
@Before
34+
public void init() throws IOException {
35+
Args.setParam(new String[] {"--output-directory",
36+
temporaryFolder.newFolder().toString(), "--debug"}, Constant.TEST_CONF);
37+
context = new TronApplicationContext(DefaultConfig.class);
38+
service = context.getBean(PeerStatusCheck.class);
39+
}
40+
41+
/**
42+
* destroy.
43+
*/
44+
@After
45+
public void destroy() {
46+
Args.clearParam();
47+
context.destroy();
48+
}
49+
50+
@Test
51+
public void testCheck() {
52+
int maxConnection = 30;
53+
Assert.assertEquals(maxConnection, Args.getInstance().getMaxConnections());
54+
Assert.assertEquals(0, PeerManager.getPeers().size());
55+
56+
for (int i = 0; i < maxConnection; i++) {
57+
InetSocketAddress inetSocketAddress = new InetSocketAddress("201.0.0." + i, 10001);
58+
Channel c1 = spy(Channel.class);
59+
ReflectUtils.setFieldValue(c1, "inetSocketAddress", inetSocketAddress);
60+
ReflectUtils.setFieldValue(c1, "inetAddress", inetSocketAddress.getAddress());
61+
ReflectUtils.setFieldValue(c1, "ctx", spy(ChannelHandlerContext.class));
62+
Mockito.doNothing().when(c1).send((byte[]) any());
63+
64+
PeerManager.add(context, c1);
65+
}
66+
67+
PeerManager.getPeers().get(0).getSyncBlockRequested()
68+
.put(new BlockId(), System.currentTimeMillis() - NetConstants.SYNC_TIME_OUT - 1000);
69+
ReflectUtils.invokeMethod(service, "statusCheck");
70+
71+
Assert.assertEquals(maxConnection - 1L, PeerManager.getPeers().size());
72+
}
73+
}

0 commit comments

Comments
 (0)