1111import io .grpc .ManagedChannelBuilder ;
1212import java .io .IOException ;
1313import java .util .Objects ;
14+ import java .util .concurrent .ExecutorService ;
1415import java .util .concurrent .TimeUnit ;
1516import org .junit .AfterClass ;
1617import org .junit .Assert ;
5354import org .tron .common .application .Application ;
5455import org .tron .common .application .ApplicationFactory ;
5556import org .tron .common .application .TronApplicationContext ;
57+ import org .tron .common .es .ExecutorServiceManager ;
5658import org .tron .common .utils .ByteArray ;
5759import org .tron .common .utils .PublicMethod ;
5860import org .tron .common .utils .Sha256Hash ;
@@ -140,6 +142,9 @@ public class RpcApiServicesTest {
140142 private static ByteString ivk ;
141143 private static ByteString d ;
142144
145+ private static ExecutorService executorService ;
146+ private static final String executorName = "rpc-test-executor" ;
147+
143148 @ BeforeClass
144149 public static void init () throws IOException {
145150 Args .setParam (new String [] {"-d" , temporaryFolder .newFolder ().toString ()}, Constant .TEST_CONF );
@@ -163,16 +168,22 @@ public static void init() throws IOException {
163168 String pBFTNode = String .format ("%s:%d" , Constant .LOCAL_HOST ,
164169 getInstance ().getRpcOnPBFTPort ());
165170
171+ executorService = ExecutorServiceManager .newFixedThreadPool (
172+ executorName , 3 );
173+
166174 channelFull = ManagedChannelBuilder .forTarget (fullNode )
167175 .usePlaintext ()
176+ .executor (executorService )
168177 .intercept (new TimeoutInterceptor (5000 ))
169178 .build ();
170179 channelPBFT = ManagedChannelBuilder .forTarget (pBFTNode )
171180 .usePlaintext ()
181+ .executor (executorService )
172182 .intercept (new TimeoutInterceptor (5000 ))
173183 .build ();
174184 channelSolidity = ManagedChannelBuilder .forTarget (solidityNode )
175185 .usePlaintext ()
186+ .executor (executorService )
176187 .intercept (new TimeoutInterceptor (5000 ))
177188 .build ();
178189 context = new TronApplicationContext (DefaultConfig .class );
@@ -197,19 +208,35 @@ public static void init() throws IOException {
197208
198209 @ AfterClass
199210 public static void destroy () {
200- if (channelFull != null ) {
201- channelFull . shutdownNow ( );
202- }
203- if ( channelPBFT != null ) {
204- channelPBFT . shutdownNow ();
205- }
206- if ( channelSolidity != null ) {
207- channelSolidity . shutdownNow () ;
211+ shutdownChannel (channelFull );
212+ shutdownChannel ( channelPBFT );
213+ shutdownChannel ( channelSolidity );
214+
215+ if ( executorService != null ) {
216+ ExecutorServiceManager . shutdownAndAwaitTermination (
217+ executorService , executorName );
218+ executorService = null ;
208219 }
220+
209221 context .close ();
210222 Args .clearParam ();
211223 }
212224
225+ private static void shutdownChannel (ManagedChannel channel ) {
226+ if (channel == null ) {
227+ return ;
228+ }
229+ try {
230+ channel .shutdown ();
231+ if (!channel .awaitTermination (5 , TimeUnit .SECONDS )) {
232+ channel .shutdownNow ();
233+ }
234+ } catch (InterruptedException e ) {
235+ channel .shutdownNow ();
236+ Thread .currentThread ().interrupt ();
237+ }
238+ }
239+
213240 @ Test
214241 public void testGetBlockByNum () {
215242 NumberMessage message = NumberMessage .newBuilder ().setNum (0 ).build ();
0 commit comments