11package org .tron .core .net .services ;
22
3+ import static org .mockito .Mockito .mock ;
4+
5+ import com .google .common .cache .Cache ;
36import java .io .File ;
47import java .lang .reflect .Field ;
8+ import java .lang .reflect .Method ;
59import java .net .InetSocketAddress ;
10+ import java .util .ArrayList ;
11+ import java .util .Collections ;
12+ import java .util .Map ;
613import org .junit .After ;
714import org .junit .Assert ;
815import org .junit .Before ;
916import org .junit .Test ;
17+ import org .mockito .Mockito ;
18+ import org .springframework .context .ApplicationContext ;
1019import org .tron .common .application .TronApplicationContext ;
1120import org .tron .common .utils .FileUtil ;
1221import org .tron .common .utils .ReflectUtils ;
1322import org .tron .core .Constant ;
23+ import org .tron .core .capsule .BlockCapsule ;
1424import org .tron .core .config .DefaultConfig ;
1525import org .tron .core .config .args .Args ;
1626import org .tron .core .net .P2pEventHandlerImpl ;
27+ import org .tron .core .net .message .adv .BlockMessage ;
1728import org .tron .core .net .peer .PeerConnection ;
29+ import org .tron .core .net .peer .PeerManager ;
1830import org .tron .core .net .peer .TronState ;
1931import org .tron .core .net .service .sync .SyncService ;
2032import org .tron .p2p .connection .Channel ;
21-
33+ import org . tron . protos . Protocol ;
2234
2335public class SyncServiceTest {
2436 protected TronApplicationContext context ;
2537 private SyncService service ;
2638 private PeerConnection peer ;
2739 private P2pEventHandlerImpl p2pEventHandler ;
40+ private ApplicationContext ctx ;
2841 private String dbPath = "output-sync-service-test" ;
42+ private InetSocketAddress inetSocketAddress =
43+ new InetSocketAddress ("127.0.0.2" , 10001 );
44+
45+ public SyncServiceTest () {
46+ }
2947
3048 /**
3149 * init context.
3250 */
3351 @ Before
34- public void init () {
52+ public void init () throws Exception {
3553 Args .setParam (new String []{"--output-directory" , dbPath , "--debug" },
36- Constant .TEST_CONF );
54+ Constant .TEST_CONF );
3755 context = new TronApplicationContext (DefaultConfig .class );
3856 service = context .getBean (SyncService .class );
57+ p2pEventHandler = context .getBean (P2pEventHandlerImpl .class );
58+ ctx = (ApplicationContext ) ReflectUtils .getFieldObject (p2pEventHandler , "ctx" );
3959 }
4060
4161 /**
@@ -49,7 +69,7 @@ public void destroy() {
4969 }
5070
5171 @ Test
52- public void test () {
72+ public void testStartSync () {
5373 try {
5474 ReflectUtils .setFieldValue (service , "fetchFlag" , true );
5575 ReflectUtils .setFieldValue (service , "handleFlag" , true );
@@ -58,18 +78,135 @@ public void test() {
5878 Assert .assertTrue ((boolean ) ReflectUtils .getFieldObject (service , "handleFlag" ));
5979 peer = context .getBean (PeerConnection .class );
6080 Assert .assertNull (peer .getSyncChainRequested ());
81+
6182 Channel c1 = new Channel ();
62- InetSocketAddress a1 = new InetSocketAddress ("127.0.0.1" , 10001 );
63- Field field = c1 .getClass ().getDeclaredField ("inetSocketAddress" );
64- field .setAccessible (true );
65- field .set (c1 , a1 .getAddress ());
83+ ReflectUtils .setFieldValue (c1 , "inetSocketAddress" , inetSocketAddress );
84+ ReflectUtils .setFieldValue (c1 , "inetAddress" , inetSocketAddress .getAddress ());
85+
6686 peer .setChannel (c1 );
87+
6788 service .startSync (peer );
89+
6890 ReflectUtils .setFieldValue (peer , "tronState" , TronState .SYNCING );
91+
6992 service .startSync (peer );
7093 } catch (Exception e ) {
7194 // no need to deal with
7295 }
7396 service .close ();
7497 }
98+
99+ @ Test
100+ public void testProcessBlock () {
101+ peer = context .getBean (PeerConnection .class );
102+ Assert .assertNull (peer .getSyncChainRequested ());
103+ Channel c1 = new Channel ();
104+ ReflectUtils .setFieldValue (c1 , "inetSocketAddress" , inetSocketAddress );
105+ ReflectUtils .setFieldValue (c1 , "inetAddress" , inetSocketAddress .getAddress ());
106+ peer .setChannel (c1 );
107+ service .processBlock (peer ,
108+ new BlockMessage (new BlockCapsule (Protocol .Block .newBuilder ().build ())));
109+ boolean fetchFlag = (boolean ) ReflectUtils .getFieldObject (service , "fetchFlag" );
110+ boolean handleFlag = (boolean ) ReflectUtils .getFieldObject (service , "handleFlag" );
111+ Assert .assertTrue (fetchFlag );
112+ Assert .assertTrue (handleFlag );
113+ }
114+
115+ @ Test
116+ public void testOnDisconnect () {
117+ Cache <BlockCapsule .BlockId , PeerConnection > requestBlockIds =
118+ (Cache ) ReflectUtils .getFieldObject (service , "requestBlockIds" );
119+ peer = context .getBean (PeerConnection .class );
120+ Assert .assertNull (peer .getSyncChainRequested ());
121+ Channel c1 = mock (Channel .class );
122+ Mockito .when (c1 .getInetSocketAddress ()).thenReturn (inetSocketAddress );
123+ Mockito .when (c1 .getInetAddress ()).thenReturn (inetSocketAddress .getAddress ());
124+ peer .setChannel (c1 );
125+ BlockCapsule .BlockId blockId = new BlockCapsule .BlockId ();
126+ requestBlockIds .put (blockId , peer );
127+ peer .getSyncBlockRequested ().put (blockId , System .currentTimeMillis ());
128+ service .onDisconnect (peer );
129+ Assert .assertTrue (requestBlockIds .getIfPresent (blockId ) == null );
130+ }
131+
132+ @ Test
133+ public void testStartFetchSyncBlock () throws Exception {
134+ Field field = PeerManager .class .getDeclaredField ("peers" );
135+ field .setAccessible (true );
136+ field .set (PeerManager .class , Collections .synchronizedList (new ArrayList <>()));
137+
138+ BlockCapsule .BlockId blockId = new BlockCapsule .BlockId ();
139+
140+ Method method = service .getClass ().getDeclaredMethod ("startFetchSyncBlock" );
141+ method .setAccessible (true );
142+
143+ Cache <BlockCapsule .BlockId , PeerConnection > requestBlockIds =
144+ (Cache <BlockCapsule .BlockId , PeerConnection >)
145+ ReflectUtils .getFieldObject (service , "requestBlockIds" );
146+
147+ Channel c1 = mock (Channel .class );
148+ Mockito .when (c1 .getInetSocketAddress ()).thenReturn (inetSocketAddress );
149+ Mockito .when (c1 .getInetAddress ()).thenReturn (inetSocketAddress .getAddress ());
150+
151+ PeerManager .add (ctx , c1 );
152+ peer = PeerManager .getPeers ().get (0 );
153+
154+ method .invoke (service );
155+ Assert .assertTrue (peer .getSyncBlockRequested ().get (blockId ) == null );
156+
157+ peer .getSyncBlockToFetch ().add (blockId );
158+ method .invoke (service );
159+ Assert .assertTrue (peer .getSyncBlockToFetch ().size () == 1 );
160+ Assert .assertTrue (peer .getSyncBlockRequested ().get (blockId ) == null );
161+
162+ peer .setFetchAble (true );
163+ method .invoke (service );
164+ Assert .assertTrue (peer .getSyncBlockToFetch ().size () == 1 );
165+ Assert .assertTrue (peer .getSyncBlockRequested ().get (blockId ) != null );
166+ Assert .assertTrue (requestBlockIds .getIfPresent (blockId ) != null );
167+
168+ peer .getSyncBlockRequested ().remove (blockId );
169+ method .invoke (service );
170+ Assert .assertTrue (peer .getSyncBlockRequested ().get (blockId ) == null );
171+ }
172+
173+ @ Test
174+ public void testHandleSyncBlock () throws Exception {
175+
176+ Field field = PeerManager .class .getDeclaredField ("peers" );
177+ field .setAccessible (true );
178+ field .set (PeerManager .class , Collections .synchronizedList (new ArrayList <>()));
179+
180+ Method method = service .getClass ().getDeclaredMethod ("handleSyncBlock" );
181+ method .setAccessible (true );
182+
183+ Map <BlockMessage , PeerConnection > blockJustReceived =
184+ (Map <BlockMessage , PeerConnection >)
185+ ReflectUtils .getFieldObject (service , "blockJustReceived" );
186+
187+ BlockCapsule blockCapsule = new BlockCapsule (Protocol .Block .newBuilder ().build ());
188+
189+ BlockCapsule .BlockId blockId = blockCapsule .getBlockId ();
190+
191+ InetSocketAddress a1 = new InetSocketAddress ("127.0.0.1" , 10001 );
192+ Channel c1 = mock (Channel .class );
193+ Mockito .when (c1 .getInetSocketAddress ()).thenReturn (a1 );
194+ Mockito .when (c1 .getInetAddress ()).thenReturn (a1 .getAddress ());
195+ PeerManager .add (ctx , c1 );
196+ peer = PeerManager .getPeers ().get (0 );
197+
198+ blockJustReceived .put (new BlockMessage (blockCapsule ), peer );
199+
200+ peer .getSyncBlockToFetch ().add (blockId );
201+
202+ Cache <BlockCapsule .BlockId , PeerConnection > requestBlockIds =
203+ (Cache <BlockCapsule .BlockId , PeerConnection >)
204+ ReflectUtils .getFieldObject (service , "requestBlockIds" );
205+
206+ requestBlockIds .put (blockId , peer );
207+
208+ method .invoke (service );
209+
210+ Assert .assertTrue (requestBlockIds .getIfPresent (blockId ) == null );
211+ }
75212}
0 commit comments