77import java .lang .reflect .Field ;
88import java .lang .reflect .Method ;
99import java .net .InetSocketAddress ;
10+ import java .util .ArrayList ;
11+ import java .util .Collections ;
1012import java .util .Map ;
1113import org .junit .After ;
1214import org .junit .Assert ;
@@ -37,6 +39,8 @@ public class SyncServiceTest {
3739 private P2pEventHandlerImpl p2pEventHandler ;
3840 private ApplicationContext ctx ;
3941 private String dbPath = "output-sync-service-test" ;
42+ private InetSocketAddress inetSocketAddress =
43+ new InetSocketAddress ("127.0.0.2" , 10001 );
4044
4145 public SyncServiceTest () {
4246 }
@@ -74,14 +78,17 @@ public void testStartSync() {
7478 Assert .assertTrue ((boolean ) ReflectUtils .getFieldObject (service , "handleFlag" ));
7579 peer = context .getBean (PeerConnection .class );
7680 Assert .assertNull (peer .getSyncChainRequested ());
81+
7782 Channel c1 = new Channel ();
78- InetSocketAddress a1 = new InetSocketAddress ("127.0.0.1" , 10001 );
79- Field field = c1 .getClass ().getDeclaredField ("inetSocketAddress" );
80- field .setAccessible (true );
81- field .set (c1 , a1 .getAddress ());
83+ ReflectUtils .setFieldValue (c1 , "inetSocketAddress" , inetSocketAddress );
84+ ReflectUtils .setFieldValue (c1 , "inetAddress" , inetSocketAddress .getAddress ());
85+
8286 peer .setChannel (c1 );
87+
8388 service .startSync (peer );
89+
8490 ReflectUtils .setFieldValue (peer , "tronState" , TronState .SYNCING );
91+
8592 service .startSync (peer );
8693 } catch (Exception e ) {
8794 // no need to deal with
@@ -90,14 +97,12 @@ public void testStartSync() {
9097 }
9198
9299 @ Test
93- public void testProcessBlock () throws Exception {
100+ public void testProcessBlock () {
94101 peer = context .getBean (PeerConnection .class );
95102 Assert .assertNull (peer .getSyncChainRequested ());
96103 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 ());
104+ ReflectUtils .setFieldValue (c1 , "inetSocketAddress" , inetSocketAddress );
105+ ReflectUtils .setFieldValue (c1 , "inetAddress" , inetSocketAddress .getAddress ());
101106 peer .setChannel (c1 );
102107 service .processBlock (peer ,
103108 new BlockMessage (new BlockCapsule (Protocol .Block .newBuilder ().build ())));
@@ -108,28 +113,29 @@ public void testProcessBlock() throws Exception {
108113 }
109114
110115 @ Test
111- public void testOnDisconnect () throws Exception {
116+ public void testOnDisconnect () {
112117 Cache <BlockCapsule .BlockId , PeerConnection > requestBlockIds =
113118 (Cache ) ReflectUtils .getFieldObject (service , "requestBlockIds" );
114119 peer = context .getBean (PeerConnection .class );
115120 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+ Channel c1 = mock (Channel .class );
122+ Mockito .when (c1 .getInetSocketAddress ()).thenReturn (inetSocketAddress );
123+ Mockito .when (c1 .getInetAddress ()).thenReturn (inetSocketAddress .getAddress ());
121124 peer .setChannel (c1 );
122125 BlockCapsule .BlockId blockId = new BlockCapsule .BlockId ();
123126 requestBlockIds .put (blockId , peer );
124- peer .getSyncBlockToFetch ().push (blockId );
127+ peer .getSyncBlockRequested ().put (blockId , System . currentTimeMillis () );
125128 service .onDisconnect (peer );
126129 Assert .assertTrue (requestBlockIds .getIfPresent (blockId ) == null );
127130 }
128131
129132 @ Test
130133 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+
131138 BlockCapsule .BlockId blockId = new BlockCapsule .BlockId ();
132- InetSocketAddress a1 = new InetSocketAddress ("127.0.0.1" , 10001 );
133139
134140 Method method = service .getClass ().getDeclaredMethod ("startFetchSyncBlock" );
135141 method .setAccessible (true );
@@ -139,8 +145,8 @@ public void testStartFetchSyncBlock() throws Exception {
139145 ReflectUtils .getFieldObject (service , "requestBlockIds" );
140146
141147 Channel c1 = mock (Channel .class );
142- Mockito .when (c1 .getInetSocketAddress ()).thenReturn (a1 );
143- Mockito .when (c1 .getInetAddress ()).thenReturn (a1 .getAddress ());
148+ Mockito .when (c1 .getInetSocketAddress ()).thenReturn (inetSocketAddress );
149+ Mockito .when (c1 .getInetAddress ()).thenReturn (inetSocketAddress .getAddress ());
144150
145151 PeerManager .add (ctx , c1 );
146152 peer = PeerManager .getPeers ().get (0 );
@@ -166,6 +172,11 @@ public void testStartFetchSyncBlock() throws Exception {
166172
167173 @ Test
168174 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+
169180 Method method = service .getClass ().getDeclaredMethod ("handleSyncBlock" );
170181 method .setAccessible (true );
171182
0 commit comments