33import io .grpc .ManagedChannel ;
44import io .grpc .ManagedChannelBuilder ;
55import io .grpc .StatusRuntimeException ;
6- import java .io .File ;
6+ import java .io .IOException ;
77import java .util .concurrent .TimeUnit ;
88import lombok .extern .slf4j .Slf4j ;
9-
10- import org .junit .After ;
9+ import org .junit .AfterClass ;
1110import org .junit .Assert ;
12- import org .junit .Before ;
11+ import org .junit .BeforeClass ;
12+ import org .junit .ClassRule ;
1313import org .junit .Rule ;
1414import org .junit .Test ;
1515import org .junit .rules .ExpectedException ;
16+ import org .junit .rules .TemporaryFolder ;
1617import org .tron .api .GrpcAPI ;
1718import org .tron .api .WalletGrpc ;
1819import org .tron .api .WalletSolidityGrpc ;
1920import org .tron .common .application .Application ;
2021import org .tron .common .application .ApplicationFactory ;
2122import org .tron .common .application .TronApplicationContext ;
22- import org .tron .common .utils .FileUtil ;
2323import org .tron .common .utils .PublicMethod ;
2424import org .tron .core .ChainBaseManager ;
2525import org .tron .core .Constant ;
3232@ Slf4j
3333public class LiteFnQueryGrpcInterceptorTest {
3434
35- private TronApplicationContext context ;
36- private ManagedChannel channelFull = null ;
37- private ManagedChannel channelpBFT = null ;
38- private WalletGrpc .WalletBlockingStub blockingStubFull = null ;
39- private WalletSolidityGrpc .WalletSolidityBlockingStub blockingStubSolidity = null ;
40- private WalletSolidityGrpc .WalletSolidityBlockingStub blockingStubpBFT = null ;
41- private RpcApiService rpcApiService ;
42- private RpcApiServiceOnSolidity rpcApiServiceOnSolidity ;
43- private RpcApiServiceOnPBFT rpcApiServiceOnPBFT ;
44- private Application appTest ;
45- private ChainBaseManager chainBaseManager ;
46-
47- private String dbPath = "output_grpc_interceptor_test" ;
35+ private static TronApplicationContext context ;
36+ private static ManagedChannel channelFull = null ;
37+ private static ManagedChannel channelSolidity = null ;
38+ private static ManagedChannel channelpBFT = null ;
39+ private static WalletGrpc .WalletBlockingStub blockingStubFull = null ;
40+ private static WalletSolidityGrpc .WalletSolidityBlockingStub blockingStubSolidity = null ;
41+ private static WalletSolidityGrpc .WalletSolidityBlockingStub blockingStubpBFT = null ;
42+ private static ChainBaseManager chainBaseManager ;
43+ private static final String ERROR_MSG =
44+ "UNAVAILABLE: this API is closed because this node is a lite fullnode" ;
4845
4946 @ Rule
5047 public ExpectedException thrown = ExpectedException .none ();
5148
49+ @ ClassRule
50+ public static TemporaryFolder temporaryFolder = new TemporaryFolder ();
51+
5252 /**
5353 * init logic.
5454 */
55- @ Before
56- public void init () {
57- Args .setParam (new String []{"-d" , dbPath }, Constant .TEST_CONF );
55+ @ BeforeClass
56+ public static void init () throws IOException {
57+ Args .setParam (new String []{"-d" , temporaryFolder . newFolder (). toString () }, Constant .TEST_CONF );
5858 Args .getInstance ().setRpcPort (PublicMethod .chooseRandomPort ());
5959 Args .getInstance ().setRpcOnSolidityPort (PublicMethod .chooseRandomPort ());
6060 Args .getInstance ().setRpcOnPBFTPort (PublicMethod .chooseRandomPort ());
6161 String fullnode = String .format ("%s:%d" , Args .getInstance ().getNodeDiscoveryBindIp (),
6262 Args .getInstance ().getRpcPort ());
63+ String solidityNode = String .format ("%s:%d" , Args .getInstance ().getNodeDiscoveryBindIp (),
64+ Args .getInstance ().getRpcOnSolidityPort ());
6365 String pBFTNode = String .format ("%s:%d" , Args .getInstance ().getNodeDiscoveryBindIp (),
64- Args .getInstance ().getRpcOnPBFTPort ());
66+ Args .getInstance ().getRpcOnPBFTPort ());
6567 channelFull = ManagedChannelBuilder .forTarget (fullnode )
6668 .usePlaintext ()
6769 .build ();
70+ channelSolidity = ManagedChannelBuilder .forTarget (solidityNode )
71+ .usePlaintext ()
72+ .build ();
6873 channelpBFT = ManagedChannelBuilder .forTarget (pBFTNode )
6974 .usePlaintext ()
7075 .build ();
7176 context = new TronApplicationContext (DefaultConfig .class );
7277 blockingStubFull = WalletGrpc .newBlockingStub (channelFull );
73- blockingStubSolidity = WalletSolidityGrpc .newBlockingStub (channelFull );
78+ blockingStubSolidity = WalletSolidityGrpc .newBlockingStub (channelSolidity );
7479 blockingStubpBFT = WalletSolidityGrpc .newBlockingStub (channelpBFT );
75- blockingStubSolidity = WalletSolidityGrpc .newBlockingStub (channelFull );
76- rpcApiService = context .getBean (RpcApiService .class );
77- rpcApiServiceOnSolidity = context .getBean (RpcApiServiceOnSolidity .class );
78- rpcApiServiceOnPBFT = context .getBean (RpcApiServiceOnPBFT .class );
80+ RpcApiService rpcApiService = context .getBean (RpcApiService .class );
81+ RpcApiServiceOnSolidity rpcOnSolidity = context .getBean (RpcApiServiceOnSolidity .class );
82+ RpcApiServiceOnPBFT rpcApiServiceOnPBFT = context .getBean (RpcApiServiceOnPBFT .class );
7983 chainBaseManager = context .getBean (ChainBaseManager .class );
80- appTest = ApplicationFactory .create (context );
84+ Application appTest = ApplicationFactory .create (context );
8185 appTest .addService (rpcApiService );
82- appTest .addService (rpcApiServiceOnSolidity );
86+ appTest .addService (rpcOnSolidity );
8387 appTest .addService (rpcApiServiceOnPBFT );
8488 appTest .initServices (Args .getInstance ());
8589 appTest .startServices ();
@@ -89,40 +93,45 @@ public void init() {
8993 /**
9094 * destroy the context.
9195 */
92- @ After
93- public void destroy () throws InterruptedException {
96+ @ AfterClass
97+ public static void destroy () throws InterruptedException {
9498 if (channelFull != null ) {
9599 channelFull .shutdown ().awaitTermination (5 , TimeUnit .SECONDS );
96100 }
101+ if (channelSolidity != null ) {
102+ channelSolidity .shutdown ().awaitTermination (5 , TimeUnit .SECONDS );
103+ }
97104 if (channelpBFT != null ) {
98105 channelpBFT .shutdown ().awaitTermination (5 , TimeUnit .SECONDS );
99106 }
107+ context .close ();
100108 Args .clearParam ();
101- appTest .shutdownServices ();
102- appTest .shutdown ();
103- context .destroy ();
104- if (FileUtil .deleteDir (new File (dbPath ))) {
105- logger .info ("Release resources successful." );
106- } else {
107- logger .info ("Release resources failure." );
108- }
109109 }
110110
111111 @ Test
112112 public void testGrpcApiThrowStatusRuntimeException () {
113113 final GrpcAPI .NumberMessage message = GrpcAPI .NumberMessage .newBuilder ().setNum (0 ).build ();
114114 chainBaseManager .setNodeType (ChainBaseManager .NodeType .LITE );
115115 thrown .expect (StatusRuntimeException .class );
116- thrown .expectMessage ("UNAVAILABLE: this API is closed because this node is a lite fullnode" );
116+ thrown .expectMessage (ERROR_MSG );
117117 blockingStubFull .getBlockByNum (message );
118118 }
119119
120+ @ Test
121+ public void testGrpcSolidityThrowStatusRuntimeException () {
122+ final GrpcAPI .NumberMessage message = GrpcAPI .NumberMessage .newBuilder ().setNum (0 ).build ();
123+ chainBaseManager .setNodeType (ChainBaseManager .NodeType .LITE );
124+ thrown .expect (StatusRuntimeException .class );
125+ thrown .expectMessage (ERROR_MSG );
126+ blockingStubSolidity .getBlockByNum (message );
127+ }
128+
120129 @ Test
121130 public void testpBFTGrpcApiThrowStatusRuntimeException () {
122131 final GrpcAPI .NumberMessage message = GrpcAPI .NumberMessage .newBuilder ().setNum (0 ).build ();
123132 chainBaseManager .setNodeType (ChainBaseManager .NodeType .LITE );
124133 thrown .expect (StatusRuntimeException .class );
125- thrown .expectMessage ("UNAVAILABLE: this API is closed because this node is a lite fullnode" );
134+ thrown .expectMessage (ERROR_MSG );
126135 blockingStubpBFT .getBlockByNum (message );
127136 }
128137
0 commit comments