22
33import com .alibaba .fastjson .JSON ;
44import com .google .protobuf .ByteString ;
5+ import java .net .InetSocketAddress ;
56import javax .annotation .Resource ;
67import lombok .extern .slf4j .Slf4j ;
8+ import org .junit .After ;
79import org .junit .Assert ;
810import org .junit .BeforeClass ;
911import org .junit .Test ;
12+ import org .springframework .context .ApplicationContext ;
1013import org .tron .common .BaseTest ;
1114import org .tron .common .entity .NodeInfo ;
15+ import org .tron .common .utils .PublicMethod ;
16+ import org .tron .common .utils .ReflectUtils ;
1217import org .tron .common .utils .Sha256Hash ;
1318import org .tron .core .Constant ;
1419import org .tron .core .capsule .BlockCapsule ;
1520import org .tron .core .config .args .Args ;
21+ import org .tron .core .net .P2pEventHandlerImpl ;
22+ import org .tron .core .net .TronNetService ;
23+ import org .tron .core .net .peer .PeerConnection ;
24+ import org .tron .core .net .peer .PeerManager ;
25+ import org .tron .p2p .P2pConfig ;
26+ import org .tron .p2p .connection .Channel ;
1627import org .tron .program .Version ;
1728
1829
@@ -23,6 +34,10 @@ public class NodeInfoServiceTest extends BaseTest {
2334 protected NodeInfoService nodeInfoService ;
2435 @ Resource
2536 protected WitnessProductBlockService witnessProductBlockService ;
37+ @ Resource
38+ private P2pEventHandlerImpl p2pEventHandler ;
39+ @ Resource
40+ private TronNetService tronNetService ;
2641
2742
2843 @ BeforeClass
@@ -31,6 +46,13 @@ public static void init() {
3146 Constant .TEST_CONF );
3247 }
3348
49+ @ After
50+ public void clearPeers () {
51+ for (PeerConnection p : PeerManager .getPeers ()) {
52+ PeerManager .remove (p .getChannel ());
53+ }
54+ }
55+
3456 @ Test
3557 public void test () {
3658 BlockCapsule blockCapsule1 = new BlockCapsule (1 , Sha256Hash .ZERO_HASH ,
@@ -40,11 +62,31 @@ public void test() {
4062 witnessProductBlockService .validWitnessProductTwoBlock (blockCapsule1 );
4163 witnessProductBlockService .validWitnessProductTwoBlock (blockCapsule2 );
4264
65+ addPeer ();
66+
4367 //test setConnectInfo
4468 NodeInfo nodeInfo = nodeInfoService .getNodeInfo ();
4569 Assert .assertEquals (nodeInfo .getConfigNodeInfo ().getCodeVersion (), Version .getVersion ());
4670 Assert .assertEquals (1 , nodeInfo .getCheatWitnessInfoMap ().size ());
4771 logger .info ("{}" , JSON .toJSONString (nodeInfo ));
4872 }
4973
74+ private void addPeer () {
75+ int port = PublicMethod .chooseRandomPort ();
76+ P2pConfig p2pConfig = new P2pConfig ();
77+ p2pConfig .setIp ("127.0.0.1" );
78+ p2pConfig .setPort (port );
79+ ReflectUtils .setFieldValue (tronNetService , "p2pConfig" , p2pConfig );
80+ TronNetService .getP2pService ().start (p2pConfig );
81+
82+ ApplicationContext ctx = (ApplicationContext ) ReflectUtils .getFieldObject (p2pEventHandler ,
83+ "ctx" );
84+ InetSocketAddress inetSocketAddress1 =
85+ new InetSocketAddress ("127.0.0.1" , 10001 );
86+ Channel c1 = new Channel ();
87+ ReflectUtils .setFieldValue (c1 , "inetSocketAddress" , inetSocketAddress1 );
88+ ReflectUtils .setFieldValue (c1 , "inetAddress" , inetSocketAddress1 .getAddress ());
89+
90+ PeerManager .add (ctx , c1 );
91+ }
5092}
0 commit comments