Skip to content

Commit

Permalink
Merge pull request #275 from alcarraz/feature/client-simulator-await-…
Browse files Browse the repository at this point in the history
…sessions-end

Wait for all session workers to finish
  • Loading branch information
ar authored Jan 5, 2023
2 parents bdefbda + 6b4b9c3 commit b167f90
Showing 1 changed file with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.io.IOException;
import java.io.FileInputStream;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.jpos.core.Environment;
import org.jpos.iso.*;
Expand All @@ -43,13 +46,16 @@ public class TestRunner
MUX mux;
ISOPackager packager;
Interpreter bsh;

long timeout;
private static final String NL = System.getProperty("line.separator");
public static final long TIMEOUT = 60000;
public TestRunner () {
super();
}
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();
Expand All @@ -61,8 +67,23 @@ protected void initService() throws ISOException {
}
}
protected void startService() {
for (int i=0; i<cfg.getInt("sessions", 1); i++)
new Thread(this).start();
int sessions = cfg.getInt("sessions", 1);
ExecutorService executor = Executors.newCachedThreadPool();
for (int i=0; i<sessions; i++)
executor.execute(this);
executor.shutdown();
if (cfg.getBoolean ("shutdown")) {
Executors.newSingleThreadExecutor().execute( () -> {
try {
if (!executor.awaitTermination(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 {
Expand All @@ -78,8 +99,6 @@ public void run () {
} catch (Throwable t) {
getLog().error (t);
}
if (cfg.getBoolean ("shutdown"))
getServer().shutdown();
}
private void runSuite (List suite, MUX mux, Interpreter bsh)
throws ISOException, IOException, EvalError
Expand Down Expand Up @@ -147,7 +166,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);
}
Expand Down Expand Up @@ -180,7 +199,7 @@ private List<TestCase> initSuite (Element suite)
if (to != null)
tc.setTimeout (Long.parseLong (to));
else
tc.setTimeout (cfg.getLong ("timeout", TIMEOUT));
tc.setTimeout (timeout);
l.add (tc);

}
Expand Down

0 comments on commit b167f90

Please sign in to comment.