1919import java .util .concurrent .Executors ;
2020import java .util .concurrent .TimeUnit ;
2121import lombok .extern .slf4j .Slf4j ;
22- import org .junit .After ;
23- import org .junit .Before ;
22+ import org .junit .AfterClass ;
23+ import org .junit .BeforeClass ;
2424import org .tron .common .application .Application ;
2525import org .tron .common .application .ApplicationFactory ;
2626import org .tron .common .application .TronApplicationContext ;
3434import org .tron .core .services .RpcApiService ;
3535
3636@ Slf4j
37- public abstract class BaseNet {
37+ public class BaseNet {
3838
3939 private static String dbPath = "output-net" ;
4040 private static String dbDirectory = "net-database" ;
4141 private static String indexDirectory = "net-index" ;
4242 private static int port = 10000 ;
4343
44- protected TronApplicationContext context ;
44+ protected static TronApplicationContext context ;
4545
46- private RpcApiService rpcApiService ;
47- private Application appT ;
48- private TronNetDelegate tronNetDelegate ;
46+ private static RpcApiService rpcApiService ;
47+ private static Application appT ;
48+ private static TronNetDelegate tronNetDelegate ;
4949
50- private ExecutorService executorService = Executors .newFixedThreadPool (1 );
50+ private static ExecutorService executorService = Executors .newFixedThreadPool (1 );
5151
5252 public static Channel connect (ByteToMessageDecoder decoder ) throws InterruptedException {
5353 NioEventLoopGroup group = new NioEventLoopGroup (1 );
@@ -73,49 +73,51 @@ protected void initChannel(Channel ch) throws Exception {
7373 return b .connect (Constant .LOCAL_HOST , port ).sync ().channel ();
7474 }
7575
76- @ Before
77- public void init () throws Exception {
78- executorService .execute (new Runnable () {
79- @ Override
80- public void run () {
81- logger . info ( "Full node running." );
82- Args . setParam (
83- new String []{
84- "--output- directory" , dbPath ,
85- "--storage-db-directory" , dbDirectory ,
86- "--storage-index-directory" , indexDirectory
87- },
88- "config.conf"
89- );
90- CommonParameter parameter = Args . getInstance ();
91- parameter .setNodeListenPort ( port );
92- parameter . getSeedNode (). getAddressList (). clear ( );
93- parameter . setNodeExternalIp ( Constant . LOCAL_HOST );
94- context = new TronApplicationContext ( DefaultConfig .class );
95- appT = ApplicationFactory . create ( context );
96- rpcApiService = context . getBean ( RpcApiService . class );
97- appT .addService ( rpcApiService );
98- appT .initServices ( parameter );
99- appT . startServices ();
100- appT . startup ( );
101- tronNetDelegate = context . getBean ( TronNetDelegate . class );
102- rpcApiService . blockUntilShutdown ();
76+ @ BeforeClass
77+ public static void init () throws Exception {
78+ executorService .execute (() -> {
79+ logger . info ( "Full node running." );
80+ Args . setParam (
81+ new String []{
82+ "--output-directory" , dbPath ,
83+ "--storage-db-directory" , dbDirectory ,
84+ "--storage-index- directory" , indexDirectory
85+ } ,
86+ "config.conf"
87+ );
88+ CommonParameter parameter = Args . getInstance ();
89+ parameter . setNodeListenPort ( port );
90+ parameter . getSeedNode (). getAddressList (). clear ();
91+ parameter .setNodeExternalIp ( Constant . LOCAL_HOST );
92+ context = new TronApplicationContext ( DefaultConfig . class );
93+ appT = ApplicationFactory . create ( context );
94+ rpcApiService = context . getBean ( RpcApiService .class );
95+ appT . addService ( rpcApiService );
96+ appT . initServices ( parameter );
97+ appT .startServices ( );
98+ appT .startup ( );
99+ try {
100+ Thread . sleep ( 2000 );
101+ } catch ( InterruptedException e ) {
102+ //ignore
103103 }
104+ tronNetDelegate = context .getBean (TronNetDelegate .class );
105+ rpcApiService .blockUntilShutdown ();
104106 });
105107 int tryTimes = 0 ;
106- while (++ tryTimes < 100 && tronNetDelegate == null ) {
107- Thread .sleep (3000 );
108- }
108+ do {
109+ Thread .sleep (3000 ); //coverage consumerInvToSpread,consumerInvToFetch in AdvService.init
110+ } while (++ tryTimes < 100 && tronNetDelegate == null );
109111 }
110112
111- @ After
112- public void destroy () {
113+ @ AfterClass
114+ public static void destroy () {
113115 Collection <PeerConnection > peerConnections = ReflectUtils
114116 .invokeMethod (tronNetDelegate , "getActivePeer" );
115117 for (PeerConnection peer : peerConnections ) {
116118 peer .getChannel ().close ();
117119 }
118-
120+ Args . clearParam ();
119121 context .destroy ();
120122 FileUtil .deleteDir (new File (dbPath ));
121123 }
0 commit comments