diff options
author | 2021-12-26 18:40:11 +0000 | |
---|---|---|
committer | 2021-12-26 18:40:11 +0000 | |
commit | e5c07edcb90018ac14787048e87f13c1384faf9f (patch) | |
tree | c2a2f478e2a27093c18fef5db49a645e87d9ee41 /tests/test_integration.py | |
parent | Excluded the snekbox/config_pb2.py from coverage (#130) (diff) | |
parent | CI: disable setup-python action on self-hosted runner (diff) |
Merge pull request #127 from python-discord/feat/nsjail/102/cgroupv2
Diffstat (limited to 'tests/test_integration.py')
-rw-r--r-- | tests/test_integration.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/test_integration.py b/tests/test_integration.py new file mode 100644 index 0000000..b76b005 --- /dev/null +++ b/tests/test_integration.py @@ -0,0 +1,43 @@ +import json +import unittest +import urllib.request +from multiprocessing.dummy import Pool + +from tests.gunicorn_utils import run_gunicorn + + +def run_code_in_snekbox(code: str) -> tuple[str, int]: + body = {"input": code} + json_data = json.dumps(body).encode("utf-8") + + req = urllib.request.Request("http://localhost:8060/eval") + req.add_header("Content-Type", "application/json; charset=utf-8") + req.add_header("Content-Length", str(len(json_data))) + + with urllib.request.urlopen(req, json_data, timeout=30) as response: + response_data = response.read().decode("utf-8") + + return response_data, response.status + + +class IntegrationTests(unittest.TestCase): + + def test_memory_limit_separate_per_process(self): + """ + Each NsJail process should have its own memory limit. + + The memory used by one process should not contribute to the memory cap of other processes. + See https://github.com/python-discord/snekbox/issues/83 + """ + with run_gunicorn(): + code = "import time; ' ' * 33000000; time.sleep(0.1)" + processes = 3 + + args = [code] * processes + with Pool(processes) as p: + results = p.map(run_code_in_snekbox, args) + + responses, statuses = zip(*results) + + self.assertTrue(all(status == 200 for status in statuses)) + self.assertTrue(all(json.loads(response)["returncode"] == 0 for response in responses)) |