From de9c63991388b6b571f73ca0b3e9702682de6ce6 Mon Sep 17 00:00:00 2001 From: Matthew Hague <matthew.hague@rhul.ac.uk> Date: Tue, 14 Dec 2021 09:56:41 +0000 Subject: [PATCH] CodeTester timeout and notes about runWithTimeout --- .../uk/ac/rhul/cs/javatester/CodeTester.java | 17 ++++++++++++++++- .../java/uk/ac/rhul/cs/javatester/Utils.java | 5 +++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/uk/ac/rhul/cs/javatester/CodeTester.java b/src/main/java/uk/ac/rhul/cs/javatester/CodeTester.java index a5e478c..6304618 100644 --- a/src/main/java/uk/ac/rhul/cs/javatester/CodeTester.java +++ b/src/main/java/uk/ac/rhul/cs/javatester/CodeTester.java @@ -175,6 +175,20 @@ public class CodeTester { */ public String getLLastCall() { return llastCall; } + /** + * Default constructor + */ + public CodeTester() { } + + /** + * Constructor with timeout convenience + * + * @param timeout timeout in seconds + */ + public CodeTester(int timeout) { + setTimeout(timeout); + } + /** * Convenience method for throwing a FailedTestException with a * message constructed with expand. @@ -1075,7 +1089,8 @@ public class CodeTester { return Utils.runWithTimeout(getTimeout(), () -> { try { - return m.invoke(tis, params); + Object o = m.invoke(tis, params); + return o; } catch (Exception e) { // this will end up at ExecutionException below, // sometimes... diff --git a/src/main/java/uk/ac/rhul/cs/javatester/Utils.java b/src/main/java/uk/ac/rhul/cs/javatester/Utils.java index 7e1c22a..1389a4c 100644 --- a/src/main/java/uk/ac/rhul/cs/javatester/Utils.java +++ b/src/main/java/uk/ac/rhul/cs/javatester/Utils.java @@ -19,6 +19,11 @@ public class Utils { /** * Run the function with a timeout and return the result. * + * TODO: this cannot stop infinite loops in student code as + * Java refuses to kill threads. Really needs to use a + * separate process. Or call System.exit at the end of your program + * (not nice). + * * @param seconds the timeout in seconds, or 0 or less if no timeout * @param f the function to call * @return the return value of the function -- GitLab