From 8ef249c593ed25b177989f4f15be6602c30c896b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alcarraz?= Date: Fri, 30 Dec 2022 20:16:05 -0500 Subject: [PATCH 1/3] Wait for all session workers to finish before shutting down the server. --- .../java/org/jpos/simulator/TestRunner.java | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/modules/client-simulator/src/main/java/org/jpos/simulator/TestRunner.java b/modules/client-simulator/src/main/java/org/jpos/simulator/TestRunner.java index 50ffb5b5ec..fe678d78bb 100644 --- a/modules/client-simulator/src/main/java/org/jpos/simulator/TestRunner.java +++ b/modules/client-simulator/src/main/java/org/jpos/simulator/TestRunner.java @@ -22,6 +22,10 @@ import java.io.IOException; import java.io.FileInputStream; import java.util.*; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import org.jpos.core.Environment; import org.jpos.iso.*; @@ -43,6 +47,10 @@ public class TestRunner MUX mux; ISOPackager packager; Interpreter bsh; + + long timeout; + + private CountDownLatch finishLatch; private static final String NL = System.getProperty("line.separator"); public static final long TIMEOUT = 60000; public TestRunner () { @@ -50,6 +58,7 @@ public TestRunner () { } protected void initService() throws ISOException { String packagerClass = cfg.get("packager", null); + timeout = cfg.getLong ("timeout", TIMEOUT); if (packagerClass != null) { try { packager = (ISOPackager) Class.forName(packagerClass).newInstance(); @@ -61,8 +70,24 @@ protected void initService() throws ISOException { } } protected void startService() { - for (int i=0; i { + try { + if (!finishLatch.await(timeout, TimeUnit.MILLISECONDS)) { + log.warn("Runners didn't finish before global timeout"); + } + } catch (InterruptedException e) { + log.warn("interrupted while awaiting workers termination", e); + } + getServer().shutdown(); + }); + } } public void run () { try { @@ -78,8 +103,7 @@ public void run () { } catch (Throwable t) { getLog().error (t); } - if (cfg.getBoolean ("shutdown")) - getServer().shutdown(); + finishLatch.countDown(); } private void runSuite (List suite, MUX mux, Interpreter bsh) throws ISOException, IOException, EvalError @@ -147,7 +171,7 @@ private void runSuite (List suite, MUX mux, Interpreter bsh) ); ISOUtil.sleep (100); // let the channel do its logging first if (cfg.getBoolean ("shutdown")) - evt.addMessage ("Shutting down"); + evt.addMessage ("waiting for shutdown"); Logger.log (evt); } @@ -180,7 +204,7 @@ private List initSuite (Element suite) if (to != null) tc.setTimeout (Long.parseLong (to)); else - tc.setTimeout (cfg.getLong ("timeout", TIMEOUT)); + tc.setTimeout (timeout); l.add (tc); } From ad341a986802fc742dfd7fbbd7999d019e1b23d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alcarraz?= Date: Mon, 2 Jan 2023 12:25:31 -0500 Subject: [PATCH 2/3] No need for `CountdownLatch`. Use `ExecutorService.awaitTermination` instead. --- .../src/main/java/org/jpos/simulator/TestRunner.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/modules/client-simulator/src/main/java/org/jpos/simulator/TestRunner.java b/modules/client-simulator/src/main/java/org/jpos/simulator/TestRunner.java index fe678d78bb..d50311f1f1 100644 --- a/modules/client-simulator/src/main/java/org/jpos/simulator/TestRunner.java +++ b/modules/client-simulator/src/main/java/org/jpos/simulator/TestRunner.java @@ -49,8 +49,6 @@ public class TestRunner Interpreter bsh; long timeout; - - private CountDownLatch finishLatch; private static final String NL = System.getProperty("line.separator"); public static final long TIMEOUT = 60000; public TestRunner () { @@ -72,14 +70,13 @@ protected void initService() throws ISOException { protected void startService() { int sessions = cfg.getInt("sessions", 1); ExecutorService executor = Executors.newCachedThreadPool(); - finishLatch = new CountDownLatch(sessions); for (int i=0; i { try { - if (!finishLatch.await(timeout, TimeUnit.MILLISECONDS)) { + if (!executor.awaitTermination(timeout, TimeUnit.MILLISECONDS)) { log.warn("Runners didn't finish before global timeout"); } } catch (InterruptedException e) { @@ -103,7 +100,6 @@ public void run () { } catch (Throwable t) { getLog().error (t); } - finishLatch.countDown(); } private void runSuite (List suite, MUX mux, Interpreter bsh) throws ISOException, IOException, EvalError From 6b4b9c3343253b72cf36c3641a00e6e5a8b48b6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alcarraz?= Date: Thu, 5 Jan 2023 07:17:20 -0500 Subject: [PATCH 3/3] No need for `CountdownLatch`. Forgot to remove import in last commit. --- .../src/main/java/org/jpos/simulator/TestRunner.java | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/client-simulator/src/main/java/org/jpos/simulator/TestRunner.java b/modules/client-simulator/src/main/java/org/jpos/simulator/TestRunner.java index d50311f1f1..1bf1ece439 100644 --- a/modules/client-simulator/src/main/java/org/jpos/simulator/TestRunner.java +++ b/modules/client-simulator/src/main/java/org/jpos/simulator/TestRunner.java @@ -22,7 +22,6 @@ import java.io.IOException; import java.io.FileInputStream; import java.util.*; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit;