Skip to content

Commit aa30ec6

Browse files
committed
feat(net): add unit test for SyncService
1 parent f6dd0d9 commit aa30ec6

1 file changed

Lines changed: 130 additions & 4 deletions

File tree

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

Lines changed: 130 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,57 @@
11
package org.tron.core.net.services;
22

3+
import static org.mockito.Mockito.mock;
4+
5+
import com.google.common.cache.Cache;
36
import java.io.File;
47
import java.lang.reflect.Field;
8+
import java.lang.reflect.Method;
59
import java.net.InetSocketAddress;
10+
import java.util.Map;
611
import org.junit.After;
712
import org.junit.Assert;
813
import org.junit.Before;
914
import org.junit.Test;
15+
import org.mockito.Mockito;
16+
import org.springframework.context.ApplicationContext;
1017
import org.tron.common.application.TronApplicationContext;
1118
import org.tron.common.utils.FileUtil;
1219
import org.tron.common.utils.ReflectUtils;
1320
import org.tron.core.Constant;
21+
import org.tron.core.capsule.BlockCapsule;
1422
import org.tron.core.config.DefaultConfig;
1523
import org.tron.core.config.args.Args;
1624
import org.tron.core.net.P2pEventHandlerImpl;
25+
import org.tron.core.net.message.adv.BlockMessage;
1726
import org.tron.core.net.peer.PeerConnection;
27+
import org.tron.core.net.peer.PeerManager;
1828
import org.tron.core.net.peer.TronState;
1929
import org.tron.core.net.service.sync.SyncService;
2030
import org.tron.p2p.connection.Channel;
21-
31+
import org.tron.protos.Protocol;
2232

2333
public class SyncServiceTest {
2434
protected TronApplicationContext context;
2535
private SyncService service;
2636
private PeerConnection peer;
2737
private P2pEventHandlerImpl p2pEventHandler;
38+
private ApplicationContext ctx;
2839
private String dbPath = "output-sync-service-test";
2940

41+
public SyncServiceTest() {
42+
}
43+
3044
/**
3145
* init context.
3246
*/
3347
@Before
34-
public void init() {
48+
public void init() throws Exception {
3549
Args.setParam(new String[]{"--output-directory", dbPath, "--debug"},
36-
Constant.TEST_CONF);
50+
Constant.TEST_CONF);
3751
context = new TronApplicationContext(DefaultConfig.class);
3852
service = context.getBean(SyncService.class);
53+
p2pEventHandler = context.getBean(P2pEventHandlerImpl.class);
54+
ctx = (ApplicationContext) ReflectUtils.getFieldObject(p2pEventHandler, "ctx");
3955
}
4056

4157
/**
@@ -49,7 +65,7 @@ public void destroy() {
4965
}
5066

5167
@Test
52-
public void test() {
68+
public void testStartSync() {
5369
try {
5470
ReflectUtils.setFieldValue(service, "fetchFlag", true);
5571
ReflectUtils.setFieldValue(service, "handleFlag", true);
@@ -72,4 +88,114 @@ public void test() {
7288
}
7389
service.close();
7490
}
91+
92+
@Test
93+
public void testProcessBlock() throws Exception {
94+
peer = context.getBean(PeerConnection.class);
95+
Assert.assertNull(peer.getSyncChainRequested());
96+
Channel c1 = new Channel();
97+
InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001);
98+
Field field = c1.getClass().getDeclaredField("inetSocketAddress");
99+
field.setAccessible(true);
100+
field.set(c1, a1.getAddress());
101+
peer.setChannel(c1);
102+
service.processBlock(peer,
103+
new BlockMessage(new BlockCapsule(Protocol.Block.newBuilder().build())));
104+
boolean fetchFlag = (boolean) ReflectUtils.getFieldObject(service, "fetchFlag");
105+
boolean handleFlag = (boolean) ReflectUtils.getFieldObject(service, "handleFlag");
106+
Assert.assertTrue(fetchFlag);
107+
Assert.assertTrue(handleFlag);
108+
}
109+
110+
@Test
111+
public void testOnDisconnect() throws Exception {
112+
Cache<BlockCapsule.BlockId, PeerConnection> requestBlockIds =
113+
(Cache) ReflectUtils.getFieldObject(service, "requestBlockIds");
114+
peer = context.getBean(PeerConnection.class);
115+
Assert.assertNull(peer.getSyncChainRequested());
116+
Channel c1 = new Channel();
117+
InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001);
118+
Field field = c1.getClass().getDeclaredField("inetSocketAddress");
119+
field.setAccessible(true);
120+
field.set(c1, a1.getAddress());
121+
peer.setChannel(c1);
122+
BlockCapsule.BlockId blockId = new BlockCapsule.BlockId();
123+
requestBlockIds.put(blockId, peer);
124+
peer.getSyncBlockToFetch().push(blockId);
125+
service.onDisconnect(peer);
126+
Assert.assertTrue(requestBlockIds.getIfPresent(blockId) == null);
127+
}
128+
129+
@Test
130+
public void testStartFetchSyncBlock() throws Exception {
131+
BlockCapsule.BlockId blockId = new BlockCapsule.BlockId();
132+
InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001);
133+
134+
Method method = service.getClass().getDeclaredMethod("startFetchSyncBlock");
135+
method.setAccessible(true);
136+
137+
Cache<BlockCapsule.BlockId, PeerConnection> requestBlockIds =
138+
(Cache<BlockCapsule.BlockId, PeerConnection>)
139+
ReflectUtils.getFieldObject(service, "requestBlockIds");
140+
141+
Channel c1 = mock(Channel.class);
142+
Mockito.when(c1.getInetSocketAddress()).thenReturn(a1);
143+
Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress());
144+
145+
PeerManager.add(ctx, c1);
146+
peer = PeerManager.getPeers().get(0);
147+
148+
method.invoke(service);
149+
Assert.assertTrue(peer.getSyncBlockRequested().get(blockId) == null);
150+
151+
peer.getSyncBlockToFetch().add(blockId);
152+
method.invoke(service);
153+
Assert.assertTrue(peer.getSyncBlockToFetch().size() == 1);
154+
Assert.assertTrue(peer.getSyncBlockRequested().get(blockId) == null);
155+
156+
peer.setFetchAble(true);
157+
method.invoke(service);
158+
Assert.assertTrue(peer.getSyncBlockToFetch().size() == 1);
159+
Assert.assertTrue(peer.getSyncBlockRequested().get(blockId) != null);
160+
Assert.assertTrue(requestBlockIds.getIfPresent(blockId) != null);
161+
162+
peer.getSyncBlockRequested().remove(blockId);
163+
method.invoke(service);
164+
Assert.assertTrue(peer.getSyncBlockRequested().get(blockId) == null);
165+
}
166+
167+
@Test
168+
public void testHandleSyncBlock() throws Exception {
169+
Method method = service.getClass().getDeclaredMethod("handleSyncBlock");
170+
method.setAccessible(true);
171+
172+
Map<BlockMessage, PeerConnection> blockJustReceived =
173+
(Map<BlockMessage, PeerConnection>)
174+
ReflectUtils.getFieldObject(service, "blockJustReceived");
175+
176+
BlockCapsule blockCapsule = new BlockCapsule(Protocol.Block.newBuilder().build());
177+
178+
BlockCapsule.BlockId blockId = blockCapsule.getBlockId();
179+
180+
InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001);
181+
Channel c1 = mock(Channel.class);
182+
Mockito.when(c1.getInetSocketAddress()).thenReturn(a1);
183+
Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress());
184+
PeerManager.add(ctx, c1);
185+
peer = PeerManager.getPeers().get(0);
186+
187+
blockJustReceived.put(new BlockMessage(blockCapsule), peer);
188+
189+
peer.getSyncBlockToFetch().add(blockId);
190+
191+
Cache<BlockCapsule.BlockId, PeerConnection> requestBlockIds =
192+
(Cache<BlockCapsule.BlockId, PeerConnection>)
193+
ReflectUtils.getFieldObject(service, "requestBlockIds");
194+
195+
requestBlockIds.put(blockId, peer);
196+
197+
method.invoke(service);
198+
199+
Assert.assertTrue(requestBlockIds.getIfPresent(blockId) == null);
200+
}
75201
}

0 commit comments

Comments
 (0)