Skip to content

Commit 06eed97

Browse files
author
Sunny Jiao
committed
fix more resource release
1 parent 9596909 commit 06eed97

File tree

9 files changed

+59
-38
lines changed

9 files changed

+59
-38
lines changed

framework/src/test/java/org/tron/common/logsfilter/NativeMessageQueueTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,20 @@ public void publishTrigger() {
5555

5656
public void startSubscribeThread() {
5757
Thread thread = new Thread(() -> {
58-
ZContext context = new ZContext();
59-
ZMQ.Socket subscriber = context.createSocket(SocketType.SUB);
58+
try (ZContext context = new ZContext()) {
59+
ZMQ.Socket subscriber = context.createSocket(SocketType.SUB);
6060

61-
Assert.assertEquals(true, subscriber.connect(String.format("tcp://localhost:%d", bindPort)));
62-
Assert.assertEquals(true, subscriber.subscribe(topic));
61+
Assert.assertEquals(true, subscriber.connect(String.format("tcp://localhost:%d", bindPort)));
62+
Assert.assertEquals(true, subscriber.subscribe(topic));
6363

64-
while (!Thread.currentThread().isInterrupted()) {
65-
byte[] message = subscriber.recv();
66-
String triggerMsg = new String(message);
64+
while (!Thread.currentThread().isInterrupted()) {
65+
byte[] message = subscriber.recv();
66+
String triggerMsg = new String(message);
6767

68-
Assert.assertEquals(true, triggerMsg.contains(dataToSend) || triggerMsg.contains(topic));
68+
Assert.assertEquals(true, triggerMsg.contains(dataToSend) || triggerMsg.contains(topic));
6969

70+
}
71+
// ZMQ.Socket will be automatically closed when ZContext is closed
7072
}
7173
});
7274
thread.start();

framework/src/test/java/org/tron/common/utils/client/Configuration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public static Config getByPath(final String configurationPath) {
2828
if (config == null) {
2929
File configFile = new File(System.getProperty("user.dir") + '/' + configurationPath);
3030
if (configFile.exists()) {
31-
try {
32-
config = ConfigFactory
33-
.parseReader(new InputStreamReader(new FileInputStream(configurationPath)));
31+
try (FileInputStream fis = new FileInputStream(configurationPath);
32+
InputStreamReader isr = new InputStreamReader(fis)) {
33+
config = ConfigFactory.parseReader(isr);
3434
logger.info("use user defined config file in current dir");
35-
} catch (FileNotFoundException e) {
35+
} catch (Exception e) {
3636
logger.error("load user defined config file exception: " + e.getMessage());
3737
}
3838
} else {

framework/src/test/java/org/tron/core/jsonrpc/LogBlockQueryTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.concurrent.ExecutorService;
66
import java.util.concurrent.Executors;
77
import javax.annotation.Resource;
8+
import org.junit.After;
89
import org.junit.Assert;
910
import org.junit.Before;
1011
import org.junit.Test;
@@ -49,6 +50,13 @@ public void setup() throws Exception {
4950
sectionBloomStore.put(1, 3, bitSet2);
5051
}
5152

53+
@After
54+
public void tearDown() {
55+
if (sectionExecutor != null && !sectionExecutor.isShutdown()) {
56+
sectionExecutor.shutdown();
57+
}
58+
}
59+
5260
@Test
5361
public void testPartialMatch() throws Exception {
5462
// Create a basic LogFilterWrapper
@@ -97,4 +105,4 @@ public void testPartialMatch() throws Exception {
97105
Assert.assertNotNull(result);
98106
Assert.assertTrue(result.isEmpty());
99107
}
100-
}
108+
}

framework/src/test/java/org/tron/core/jsonrpc/SectionBloomStoreTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ public void testWriteAndQuery() {
140140
Assert.assertTrue(possibleBlockList.contains(10000L));
141141
} catch (Exception e) {
142142
Assert.fail();
143+
} finally {
144+
sectionExecutor.shutdown();
143145
}
144146

145147
//query multi address

framework/src/test/java/org/tron/core/net/BaseNet.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ public static void destroy() {
9191
peer.getChannel().close();
9292
}
9393
}
94+
if (executorService != null && !executorService.isShutdown()) {
95+
executorService.shutdown();
96+
}
9497
Args.clearParam();
9598
context.destroy();
9699
}

framework/src/test/java/org/tron/core/services/DelegationServiceTest.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,6 @@ public DelegationServiceTest(TronApplicationContext context) {
3030
manager = context.getBean(Manager.class);
3131
}
3232

33-
public static void testGrpc() {
34-
WalletBlockingStub walletStub = WalletGrpc
35-
.newBlockingStub(ManagedChannelBuilder.forTarget(fullnode)
36-
.usePlaintext()
37-
.build());
38-
BytesMessage.Builder builder = BytesMessage.newBuilder();
39-
builder.setValue(ByteString.copyFromUtf8("TLTDZBcPoJ8tZ6TTEeEqEvwYFk2wgotSfD"));
40-
System.out
41-
.println("getBrokerageInfo: " + walletStub.getBrokerageInfo(builder.build()).getNum());
42-
System.out.println("getRewardInfo: " + walletStub.getRewardInfo(builder.build()).getNum());
43-
UpdateBrokerageContract.Builder updateBrokerageContract = UpdateBrokerageContract.newBuilder();
44-
updateBrokerageContract.setOwnerAddress(
45-
ByteString.copyFrom(decodeFromBase58Check("TN3zfjYUmMFK3ZsHSsrdJoNRtGkQmZLBLz")))
46-
.setBrokerage(10);
47-
TransactionExtention transactionExtention = walletStub
48-
.updateBrokerage(updateBrokerageContract.build());
49-
System.out.println("UpdateBrokerage: " + transactionExtention);
50-
}
51-
5233
private void testPay(int cycle) {
5334
double rate = 0.2;
5435
if (cycle == 0) {

framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
112112
public class RpcApiServicesTest {
113113
private static TronApplicationContext context;
114+
private static ManagedChannel channelFull = null;
115+
private static ManagedChannel channelPBFT = null;
116+
private static ManagedChannel channelSolidity = null;
114117
private static DatabaseBlockingStub databaseBlockingStubFull = null;
115118
private static DatabaseBlockingStub databaseBlockingStubSolidity = null;
116119
private static DatabaseBlockingStub databaseBlockingStubPBFT = null;
@@ -152,13 +155,13 @@ public static void init() throws IOException {
152155
String pBFTNode = String.format("%s:%d", Constant.LOCAL_HOST,
153156
getInstance().getRpcOnPBFTPort());
154157

155-
ManagedChannel channelFull = ManagedChannelBuilder.forTarget(fullNode)
158+
channelFull = ManagedChannelBuilder.forTarget(fullNode)
156159
.usePlaintext()
157160
.build();
158-
ManagedChannel channelPBFT = ManagedChannelBuilder.forTarget(pBFTNode)
161+
channelPBFT = ManagedChannelBuilder.forTarget(pBFTNode)
159162
.usePlaintext()
160163
.build();
161-
ManagedChannel channelSolidity = ManagedChannelBuilder.forTarget(solidityNode)
164+
channelSolidity = ManagedChannelBuilder.forTarget(solidityNode)
162165
.usePlaintext()
163166
.build();
164167
context = new TronApplicationContext(DefaultConfig.class);
@@ -183,6 +186,15 @@ public static void init() throws IOException {
183186

184187
@AfterClass
185188
public static void destroy() {
189+
if (channelFull != null) {
190+
channelFull.shutdown();
191+
}
192+
if (channelPBFT != null) {
193+
channelPBFT.shutdown();
194+
}
195+
if (channelSolidity != null) {
196+
channelSolidity.shutdown();
197+
}
186198
context.close();
187199
Args.clearParam();
188200
}

framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
public class RpcApiAccessInterceptorTest {
4343

4444
private static TronApplicationContext context;
45+
private static ManagedChannel channelFull = null;
46+
private static ManagedChannel channelPBFT = null;
47+
private static ManagedChannel channelSolidity = null;
4548
private static WalletGrpc.WalletBlockingStub blockingStubFull = null;
4649
private static WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubSolidity = null;
4750
private static WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubPBFT = null;
@@ -68,13 +71,13 @@ public static void init() throws IOException {
6871
String pBFTNode = String.format("%s:%d", Constant.LOCAL_HOST,
6972
Args.getInstance().getRpcOnPBFTPort());
7073

71-
ManagedChannel channelFull = ManagedChannelBuilder.forTarget(fullNode)
74+
channelFull = ManagedChannelBuilder.forTarget(fullNode)
7275
.usePlaintext()
7376
.build();
74-
ManagedChannel channelPBFT = ManagedChannelBuilder.forTarget(pBFTNode)
77+
channelPBFT = ManagedChannelBuilder.forTarget(pBFTNode)
7578
.usePlaintext()
7679
.build();
77-
ManagedChannel channelSolidity = ManagedChannelBuilder.forTarget(solidityNode)
80+
channelSolidity = ManagedChannelBuilder.forTarget(solidityNode)
7881
.usePlaintext()
7982
.build();
8083

@@ -93,6 +96,15 @@ public static void init() throws IOException {
9396
*/
9497
@AfterClass
9598
public static void destroy() {
99+
if (channelFull != null) {
100+
channelFull.shutdown();
101+
}
102+
if (channelPBFT != null) {
103+
channelPBFT.shutdown();
104+
}
105+
if (channelSolidity != null) {
106+
channelSolidity.shutdown();
107+
}
96108
context.close();
97109
Args.clearParam();
98110
}

framework/src/test/java/org/tron/core/zksnark/LibrustzcashTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ public void calBenchmarkSpendConcurrent() throws Exception {
298298

299299
countDownLatch.await();
300300

301+
generatePool.shutdown();
301302
logger.info("generate cost time:" + (System.currentTimeMillis() - startGenerate));
302303
}
303304

0 commit comments

Comments
 (0)