diff options
author | 2021-02-25 14:28:51 +0100 | |
---|---|---|
committer | 2021-02-25 14:28:51 +0100 | |
commit | 7d34cb8563d8c01e5f5d1b038e0fbd507063e853 (patch) | |
tree | f276a5e4f62645022dc27a892d810f623bdf4b37 | |
parent | Add return code 6 for exceptions when loading module (diff) |
Add return code 7 for processes killed by NsJail
-rw-r--r-- | backend/routes/forms/unittesting.py | 47 | ||||
-rw-r--r-- | resources/unittest_template.py | 2 |
2 files changed, 27 insertions, 22 deletions
diff --git a/backend/routes/forms/unittesting.py b/backend/routes/forms/unittesting.py index c00fc4c..57bf5db 100644 --- a/backend/routes/forms/unittesting.py +++ b/backend/routes/forms/unittesting.py @@ -50,7 +50,7 @@ async def _post_eval(code: str) -> Optional[dict[str, str]]: """Post the eval to snekbox and return the response.""" async with httpx.AsyncClient() as client: data = {"input": code} - response = await client.post(SNEKBOX_URL, json=data) + response = await client.post(SNEKBOX_URL, json=data, timeout=10) if not response.status_code == 200: return @@ -97,30 +97,33 @@ async def execute_unittest(form_response: FormResponse, form: Form) -> list[Unit else: return_code = int(response["returncode"]) + # Parse the stdout if the tests ran successfully + if return_code == 0: + stdout = response["stdout"] + passed = bool(int(stdout[0])) + + # If the test failed, we have to populate the result string. + if not passed: + failed_tests = stdout[1:].strip().split(";") + + # Redact failed hidden tests + for i, failed_test in enumerate(failed_tests[:]): + if failed_test in hidden_tests: + failed_tests[i] = f"hidden_test_{hidden_tests[failed_test]}" + + result = ";".join(failed_tests) + else: + result = "" + elif return_code in (5, 6, 99): + result = response["stdout"] + # Killed by NsJail + elif return_code == 137: + return_code = 7 + result = "Timed out or ran out of memory." # Another code has been returned by CPython because of another failure. - if return_code not in (0, 5, 6, 99): + else: return_code = 99 result = "Internal error." - else: - # Parse the stdout if the tests ran successfully - if return_code == 0: - stdout = response["stdout"] - passed = bool(int(stdout[0])) - - # If the test failed, we have to populate the result string. - if not passed: - failed_tests = stdout[1:].strip().split(";") - - # Redact failed hidden tests - for i, failed_test in enumerate(failed_tests[:]): - if failed_test in hidden_tests: - failed_tests[i] = f"hidden_test_{hidden_tests[failed_test]}" - - result = ";".join(failed_tests) - else: - result = "" - else: - result = response["stdout"] unittest_results.append(UnittestResult( question_id=question.id, diff --git a/resources/unittest_template.py b/resources/unittest_template.py index 02d3894..38e3be8 100644 --- a/resources/unittest_template.py +++ b/resources/unittest_template.py @@ -27,6 +27,8 @@ def _exit_sandbox(code: int) -> NoReturn: - 5: Syntax error while parsing user code - 6: Uncaught exception while loading user code - 99: Internal error + + 137 can also be generated by NsJail when killing the process. """ print(RESULT.getvalue(), file=ORIGINAL_STDOUT, end="") sys.exit(code) |