diff --git a/framework/src/test/java/org/tron/common/jetty/JettyServerTest.java b/framework/src/test/java/org/tron/common/jetty/JettyServerTest.java index fbb2721f502..2b1f8aec7a8 100644 --- a/framework/src/test/java/org/tron/common/jetty/JettyServerTest.java +++ b/framework/src/test/java/org/tron/common/jetty/JettyServerTest.java @@ -40,6 +40,15 @@ public static void startJetty() throws Exception { } int port = connector.getLocalPort(); serverUri = new URI(String.format("http://%s:%d/", host, port)); + + // Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + stopJetty(); + } catch (Exception e) { + System.err.println("Shutdown hook cleanup failed: " + e.getMessage()); + } + })); } @AfterClass diff --git a/framework/src/test/java/org/tron/core/db/TxCacheDBInitTest.java b/framework/src/test/java/org/tron/core/db/TxCacheDBInitTest.java index b976cf5f2da..c73c88df58b 100644 --- a/framework/src/test/java/org/tron/core/db/TxCacheDBInitTest.java +++ b/framework/src/test/java/org/tron/core/db/TxCacheDBInitTest.java @@ -42,6 +42,15 @@ public static void init() throws IOException { Args.setParam(new String[]{"--output-directory", temporaryFolder.newFolder().toString(), "--p2p-disable", "true"}, Constant.TEST_CONF); context = new TronApplicationContext(DefaultConfig.class); + + // Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + destroy(); + } catch (Exception e) { + logger.error("Shutdown hook cleanup failed", e); + } + })); } @Test diff --git a/framework/src/test/java/org/tron/core/net/BaseNet.java b/framework/src/test/java/org/tron/core/net/BaseNet.java index bdaab1b4301..8796a0b6492 100644 --- a/framework/src/test/java/org/tron/core/net/BaseNet.java +++ b/framework/src/test/java/org/tron/core/net/BaseNet.java @@ -80,6 +80,15 @@ public static void init() throws Exception { do { Thread.sleep(3000); //coverage consumerInvToSpread,consumerInvToFetch in AdvService.init } while (++tryTimes < 100 && tronNetDelegate == null); + + // Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + destroy(); + } catch (Exception e) { + System.err.println("Shutdown hook cleanup failed: " + e.getMessage()); + } + })); } @AfterClass diff --git a/framework/src/test/java/org/tron/core/net/messagehandler/MessageHandlerTest.java b/framework/src/test/java/org/tron/core/net/messagehandler/MessageHandlerTest.java index 7ff29b54bb7..8cdc53355d2 100644 --- a/framework/src/test/java/org/tron/core/net/messagehandler/MessageHandlerTest.java +++ b/framework/src/test/java/org/tron/core/net/messagehandler/MessageHandlerTest.java @@ -55,6 +55,15 @@ public static void init() throws Exception { TronNetService tronNetService = context.getBean(TronNetService.class); Parameter.p2pConfig = new P2pConfig(); ReflectUtils.setFieldValue(tronNetService, "p2pConfig", Parameter.p2pConfig); + + // Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + destroy(); + } catch (Exception e) { + System.err.println("Shutdown hook cleanup failed: " + e.getMessage()); + } + })); } @AfterClass diff --git a/framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java b/framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java index 04a6315f522..106b7f1337e 100644 --- a/framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java +++ b/framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java @@ -49,6 +49,14 @@ public static void init() throws IOException { service = context.getBean(AdvService.class); p2pEventHandler = context.getBean(P2pEventHandlerImpl.class); ctx = (ApplicationContext) ReflectUtils.getFieldObject(p2pEventHandler, "ctx"); + + // Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + after(); + } catch (Exception e) { + System.err.println("Shutdown hook cleanup failed: " + e.getMessage()); } + })); } @AfterClass diff --git a/framework/src/test/java/org/tron/core/net/services/HandShakeServiceTest.java b/framework/src/test/java/org/tron/core/net/services/HandShakeServiceTest.java index 54ee84cee6c..a7a62a9ebd0 100644 --- a/framework/src/test/java/org/tron/core/net/services/HandShakeServiceTest.java +++ b/framework/src/test/java/org/tron/core/net/services/HandShakeServiceTest.java @@ -64,6 +64,15 @@ public static void init() throws Exception { TronNetService tronNetService = context.getBean(TronNetService.class); Parameter.p2pConfig = new P2pConfig(); ReflectUtils.setFieldValue(tronNetService, "p2pConfig", Parameter.p2pConfig); + + // Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + destroy(); + } catch (Exception e) { + System.err.println("Shutdown hook cleanup failed: " + e.getMessage()); + } + })); } @AfterClass diff --git a/framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java b/framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java index 48a25b141dc..7026130976c 100644 --- a/framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java +++ b/framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java @@ -176,6 +176,15 @@ public static void init() throws IOException { manager.getDynamicPropertiesStore().saveAllowShieldedTRC20Transaction(1); Application appTest = ApplicationFactory.create(context); appTest.startup(); + + // Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + destroy(); + } catch (Exception e) { + System.err.println("Shutdown hook cleanup failed: " + e.getMessage()); + } + })); } @AfterClass diff --git a/framework/src/test/java/org/tron/core/services/WalletApiTest.java b/framework/src/test/java/org/tron/core/services/WalletApiTest.java index 8890d4bfd9e..f9485426a07 100644 --- a/framework/src/test/java/org/tron/core/services/WalletApiTest.java +++ b/framework/src/test/java/org/tron/core/services/WalletApiTest.java @@ -39,6 +39,18 @@ public static void init() throws IOException { context = new TronApplicationContext(DefaultConfig.class); appT = ApplicationFactory.create(context); appT.startup(); + + // Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + if (context != null) { + Args.clearParam(); + context.destroy(); + } + } catch (Exception e) { + System.err.println("Shutdown hook cleanup failed: " + e.getMessage()); + } + })); } @Test diff --git a/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java b/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java index d98e2c9267e..29bcf426eb3 100644 --- a/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java +++ b/framework/src/test/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptorTest.java @@ -82,6 +82,15 @@ public static void init() throws IOException { chainBaseManager = context.getBean(ChainBaseManager.class); Application appTest = ApplicationFactory.create(context); appTest.startup(); + + // Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + destroy(); + } catch (Exception e) { + System.err.println("Shutdown hook cleanup failed: " + e.getMessage()); + } + })); } /** diff --git a/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java b/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java index ce7efabef0c..e345462fb74 100644 --- a/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java +++ b/framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java @@ -86,6 +86,15 @@ public static void init() throws IOException { Application appTest = ApplicationFactory.create(context); appTest.startup(); + + // Add shutdown hook for unit test restart before whole class tests finished, check the forkEvery in build.gradlew + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + destroy(); + } catch (Exception e) { + System.err.println("Shutdown hook cleanup failed: " + e.getMessage()); + } + })); } /**