aboutsummaryrefslogtreecommitdiffstats
path: root/tests/limits/test_timed.py
diff options
context:
space:
mode:
authorGravatar Mark <[email protected]>2023-11-05 10:16:18 -0800
committerGravatar GitHub <[email protected]>2023-11-05 10:16:18 -0800
commit841b52be9b960e61af0bcc61454eedc893641626 (patch)
tree635611c04770cffd33036a2a2380f978017ee621 /tests/limits/test_timed.py
parentMerge #195 - Python 3.12 (diff)
parentRemove Python 3.13 from image (diff)
Merge #194 - refactor file structure and nsjail module
Diffstat (limited to 'tests/limits/test_timed.py')
-rw-r--r--tests/limits/test_timed.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/limits/test_timed.py b/tests/limits/test_timed.py
new file mode 100644
index 0000000..8a1119b
--- /dev/null
+++ b/tests/limits/test_timed.py
@@ -0,0 +1,30 @@
+import math
+import time
+from unittest import TestCase
+
+from snekbox.limits.timed import time_limit
+
+
+class TimedTests(TestCase):
+ def test_sleep(self):
+ """Test that a sleep can be interrupted."""
+ _finished = False
+ start = time.perf_counter()
+ with self.assertRaises(TimeoutError):
+ with time_limit(1):
+ time.sleep(2)
+ _finished = True
+ end = time.perf_counter()
+ self.assertLess(end - start, 2)
+ self.assertFalse(_finished)
+
+ def test_iter(self):
+ """Test that a long-running built-in function can be interrupted."""
+ _result = 0
+ start = time.perf_counter()
+ with self.assertRaises(TimeoutError):
+ with time_limit(1):
+ _result = math.factorial(2**30)
+ end = time.perf_counter()
+ self.assertEqual(_result, 0)
+ self.assertLess(end - start, 2)