Skip to content

Commit

Permalink
fixed timeout message
Browse files Browse the repository at this point in the history
  • Loading branch information
Albitr committed Jan 20, 2024
1 parent 8b46b34 commit 6b89c1b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.vscode
.venv
bin
__pycache__
Expand Down
16 changes: 8 additions & 8 deletions source/StudentSubmission/common.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
from enum import Enum
from typing import List, Any

class PossibleResults(Enum):
STDOUT = "stdout"
RETURN_VAL = "return_val"
FILE_OUT = "file_out"
FILE_HASH = "file_hash"
MOCK_SIDE_EFFECTS = "mock"
EXCEPTION = "exception"

class ValidationHook(Enum):
PRE_LOAD = 1
POST_LOAD = 2
Expand All @@ -32,6 +24,14 @@ def __reduce__(self):
# Need to be (something,) so that it actually gets processed as a tuple in the pickler
return (MissingFunctionDefinition, (self.functionName,))

class TimeoutError(Exception):
def __init__(self, timeout: int):
super().__init__(f"Submission timed out after {timeout} seconds")
self.timeout = timeout

def __reduce__(self):
return (TimeoutError, (self.timeout,))

Check warning on line 33 in source/StudentSubmission/common.py

View check run for this annotation

Codecov / codecov/patch

source/StudentSubmission/common.py#L33

Added line #L33 was not covered by tests

class InvalidTestCaseSetupCode(Exception):
def __init__(self, *args):
super().__init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from io import StringIO

from Executors.common import MissingOutputDataException, detectFileSystemChanges, filterStdOut
from StudentSubmission.common import TimeoutError
from StudentSubmissionImpl.Python.PythonRunners import GenericPythonRunner

dill.Pickler.dumps, dill.Pickler.loads = dill.dumps, dill.loads
Expand Down Expand Up @@ -213,6 +214,8 @@ def setup(self, environment: ExecutionEnvironment, runner: GenericPythonRunner):

self.inputSharedMem.buf[:len(serializedStdin)] = serializedStdin

self.timeoutTime = environment.timeout

def run(self):
if self.studentSubmissionProcess is None:
raise AttributeError("Process has not be initalized!")
Expand Down Expand Up @@ -252,7 +255,7 @@ def cleanup(self):


if self.timeoutOccurred:
self.exception = TimeoutError(f"Submission timed out after {self.timeoutTime} seconds")
self.exception = TimeoutError(self.timeoutTime)
self._deallocate()
return

Expand Down
27 changes: 27 additions & 0 deletions tests/impl/python/testPythonSubmissionProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from TestingFramework.SingleFunctionMock import SingleFunctionMock
from StudentSubmission.common import MissingFunctionDefinition, InvalidTestCaseSetupCode
from Executors.common import MissingOutputDataException
from StudentSubmission.common import TimeoutError


class TestPythonSubmissionProcess(unittest.TestCase):
Expand Down Expand Up @@ -243,6 +244,32 @@ def testTerminateInfiniteLoopWithInput(self):

self.assertNotIn(PossibleResults.STDOUT, results)

def testCorrectTimeoutError(self):
program = \
"while True:\n"\
" print('OUTPUT LOOP:)')\n"

runner = MainModuleRunner()
runner.setSubmission(compile(program, "test_code", "exec"))

self.environment.timeout = 5

self.runnableSubmission.setup(self.environment, runner)
self.runnableSubmission.run()
self.runnableSubmission.cleanup()

self.runnableSubmission.populateResults(self.environment)

results = self.environment.resultData

with self.assertRaises(TimeoutError) as ex:
raise results[PossibleResults.EXCEPTION]

exceptionText = str(ex.exception)
self.assertIn("timed out after 5 seconds", exceptionText)

self.assertNotIn(PossibleResults.STDOUT, results)

def testHandledExceptionInfiniteRecursion(self):
program = \
"def loop():\n"\
Expand Down

0 comments on commit 6b89c1b

Please sign in to comment.