Skip to content

Commit a3afa12

Browse files
committed
feat(net):add unit test for block synchronization
1 parent 7082714 commit a3afa12

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.tron.core.net.services;
2+
3+
import org.junit.After;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.tron.common.application.TronApplicationContext;
8+
import org.tron.common.utils.FileUtil;
9+
import org.tron.common.utils.ReflectUtils;
10+
import org.tron.core.Constant;
11+
import org.tron.core.config.DefaultConfig;
12+
import org.tron.core.config.args.Args;
13+
import org.tron.core.net.P2pEventHandlerImpl;
14+
import org.tron.core.net.peer.PeerConnection;
15+
import org.tron.core.net.peer.TronState;
16+
import org.tron.core.net.service.sync.SyncService;
17+
import org.tron.p2p.connection.Channel;
18+
19+
import java.io.File;
20+
import java.lang.reflect.Field;
21+
import java.net.InetSocketAddress;
22+
23+
public class SyncServiceTest {
24+
protected TronApplicationContext context;
25+
private SyncService service;
26+
private PeerConnection peer;
27+
private P2pEventHandlerImpl p2pEventHandler;
28+
private String dbPath = "output-sync-service-test";
29+
30+
/**
31+
* init context.
32+
*/
33+
@Before
34+
public void init() {
35+
Args.setParam(new String[]{"--output-directory", dbPath, "--debug"},
36+
Constant.TEST_CONF);
37+
context = new TronApplicationContext(DefaultConfig.class);
38+
service = context.getBean(SyncService.class);
39+
}
40+
41+
/**
42+
* destroy.
43+
*/
44+
@After
45+
public void destroy() {
46+
Args.clearParam();
47+
FileUtil.deleteDir(new File(dbPath));
48+
}
49+
50+
@Test
51+
public void test() {
52+
try {
53+
ReflectUtils.setFieldValue(service, "fetchFlag", true);
54+
ReflectUtils.setFieldValue(service, "handleFlag", true);
55+
service.init();
56+
Assert.assertTrue((boolean) ReflectUtils.getFieldObject(service, "fetchFlag"));
57+
Assert.assertTrue((boolean) ReflectUtils.getFieldObject(service, "handleFlag"));
58+
peer = context.getBean(PeerConnection.class);
59+
Assert.assertNull(peer.getSyncChainRequested());
60+
Channel c1 = new Channel();
61+
InetSocketAddress a1 =new InetSocketAddress("127.0.0.1", 10001);
62+
Field field = c1.getClass().getDeclaredField("inetSocketAddress");
63+
field.setAccessible(true);
64+
field.set(c1, a1.getAddress());
65+
peer.setChannel(c1);
66+
service.startSync(peer);
67+
ReflectUtils.setFieldValue(peer, "tronState", TronState.SYNCING);
68+
service.startSync(peer);
69+
} catch (Exception e) {
70+
// no need to deal with
71+
}
72+
service.close();
73+
}
74+
}

0 commit comments

Comments
 (0)