File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ import math
2+ import time
3+ from unittest import TestCase
4+
5+ from snekbox .utils .timed import time_limit
6+
7+
8+ class TimedTests (TestCase ):
9+ def test_sleep (self ):
10+ """Test that a sleep can be interrupted."""
11+ _finished = False
12+ start = time .perf_counter ()
13+ with self .assertRaises (TimeoutError ):
14+ with time_limit (1 ):
15+ time .sleep (2 )
16+ _finished = True
17+ end = time .perf_counter ()
18+ self .assertLess (end - start , 2 )
19+ self .assertFalse (_finished )
20+
21+ def test_iter (self ):
22+ """Test that a long-running built-in function can be interrupted."""
23+ _result = 0
24+ start = time .perf_counter ()
25+ with self .assertRaises (TimeoutError ):
26+ with time_limit (1 ):
27+ _result = math .factorial (2 ** 30 )
28+ end = time .perf_counter ()
29+ self .assertEqual (_result , 0 )
30+ self .assertLess (end - start , 2 )
You can’t perform that action at this time.
0 commit comments