Skip to content

Commit b730560

Browse files
author
Sunny Jiao
committed
fix unit test resource leakage
1 parent 6c95081 commit b730560

File tree

10 files changed

+92
-0
lines changed

10 files changed

+92
-0
lines changed

framework/src/test/java/org/tron/common/jetty/JettyServerTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public static void startJetty() throws Exception {
4040
}
4141
int port = connector.getLocalPort();
4242
serverUri = new URI(String.format("http://%s:%d/", host, port));
43+
44+
// Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew
45+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
46+
try {
47+
stopJetty();
48+
} catch (Exception e) {
49+
System.err.println("Shutdown hook cleanup failed: " + e.getMessage());
50+
}
51+
}));
4352
}
4453

4554
@AfterClass

framework/src/test/java/org/tron/core/db/TxCacheDBInitTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ public static void init() throws IOException {
4242
Args.setParam(new String[]{"--output-directory", temporaryFolder.newFolder().toString(),
4343
"--p2p-disable", "true"}, Constant.TEST_CONF);
4444
context = new TronApplicationContext(DefaultConfig.class);
45+
46+
// Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew
47+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
48+
try {
49+
destroy();
50+
} catch (Exception e) {
51+
logger.error("Shutdown hook cleanup failed", e);
52+
}
53+
}));
4554
}
4655

4756
@Test

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ public static void init() throws Exception {
8080
do {
8181
Thread.sleep(3000); //coverage consumerInvToSpread,consumerInvToFetch in AdvService.init
8282
} while (++tryTimes < 100 && tronNetDelegate == null);
83+
84+
// Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew
85+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
86+
try {
87+
destroy();
88+
} catch (Exception e) {
89+
System.err.println("Shutdown hook cleanup failed: " + e.getMessage());
90+
}
91+
}));
8392
}
8493

8594
@AfterClass

framework/src/test/java/org/tron/core/net/messagehandler/MessageHandlerTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ public static void init() throws Exception {
5555
TronNetService tronNetService = context.getBean(TronNetService.class);
5656
Parameter.p2pConfig = new P2pConfig();
5757
ReflectUtils.setFieldValue(tronNetService, "p2pConfig", Parameter.p2pConfig);
58+
59+
// Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew
60+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
61+
try {
62+
destroy();
63+
} catch (Exception e) {
64+
System.err.println("Shutdown hook cleanup failed: " + e.getMessage());
65+
}
66+
}));
5867
}
5968

6069
@AfterClass

framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public static void init() throws IOException {
4949
service = context.getBean(AdvService.class);
5050
p2pEventHandler = context.getBean(P2pEventHandlerImpl.class);
5151
ctx = (ApplicationContext) ReflectUtils.getFieldObject(p2pEventHandler, "ctx");
52+
53+
// Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew
54+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
55+
try {
56+
after();
57+
} catch (Exception e) {
58+
System.err.println("Shutdown hook cleanup failed: " + e.getMessage()); }
59+
}));
5260
}
5361

5462
@AfterClass

framework/src/test/java/org/tron/core/net/services/HandShakeServiceTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ public static void init() throws Exception {
6464
TronNetService tronNetService = context.getBean(TronNetService.class);
6565
Parameter.p2pConfig = new P2pConfig();
6666
ReflectUtils.setFieldValue(tronNetService, "p2pConfig", Parameter.p2pConfig);
67+
68+
// Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew
69+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
70+
try {
71+
destroy();
72+
} catch (Exception e) {
73+
System.err.println("Shutdown hook cleanup failed: " + e.getMessage());
74+
}
75+
}));
6776
}
6877

6978
@AfterClass

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ public static void init() throws IOException {
176176
manager.getDynamicPropertiesStore().saveAllowShieldedTRC20Transaction(1);
177177
Application appTest = ApplicationFactory.create(context);
178178
appTest.startup();
179+
180+
// Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew
181+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
182+
try {
183+
destroy();
184+
} catch (Exception e) {
185+
System.err.println("Shutdown hook cleanup failed: " + e.getMessage());
186+
}
187+
}));
179188
}
180189

181190
@AfterClass

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ public static void init() throws IOException {
3939
context = new TronApplicationContext(DefaultConfig.class);
4040
appT = ApplicationFactory.create(context);
4141
appT.startup();
42+
43+
// Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew
44+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
45+
try {
46+
if (context != null) {
47+
Args.clearParam();
48+
context.destroy();
49+
}
50+
} catch (Exception e) {
51+
System.err.println("Shutdown hook cleanup failed: " + e.getMessage());
52+
}
53+
}));
4254
}
4355

4456
@Test

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ public static void init() throws IOException {
8282
chainBaseManager = context.getBean(ChainBaseManager.class);
8383
Application appTest = ApplicationFactory.create(context);
8484
appTest.startup();
85+
86+
// Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew
87+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
88+
try {
89+
destroy();
90+
} catch (Exception e) {
91+
System.err.println("Shutdown hook cleanup failed: " + e.getMessage());
92+
}
93+
}));
8594
}
8695

8796
/**

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ public static void init() throws IOException {
8686

8787
Application appTest = ApplicationFactory.create(context);
8888
appTest.startup();
89+
90+
// Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew
91+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
92+
try {
93+
destroy();
94+
} catch (Exception e) {
95+
System.err.println("Shutdown hook cleanup failed: " + e.getMessage());
96+
}
97+
}));
8998
}
9099

91100
/**

0 commit comments

Comments
 (0)