-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsupport.py
More file actions
67 lines (58 loc) · 1.79 KB
/
support.py
File metadata and controls
67 lines (58 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#support.py
# unittest support functions
from time import clock
import sys
import unittest
import functools
import contextlib
import cStringIO
import stackless
import stacklesslib.errors
from stacklesslib.util import timeout
from stacklesslib import app, main
# This is the total timeout for running tests. Adjust as needed.
TIMEOUT = 10
def timesafe(t=1.0):
"""Decorate a unittest with this to make it error after 't' seconds"""
def helper(func):
@functools.wraps(func)
def testmethod(self):
try:
with timeout(t):
func(self)
except stacklesslib.errors.TimeoutError:
self.fail("test case timed out")
return testmethod
return helper
@contextlib.contextmanager
def captured_stderr():
old = sys.stderr
sys.stderr = cStringIO.StringIO()
try:
yield sys.stderr
finally:
sys.stderr = old
class StacklessTestSuite(unittest.TestSuite):
def run(self, results):
err = []
def tasklet_run():
try:
unittest.TestSuite.run(self, results)
except:
err.append(sys.exc_info())
app.install_stackless()
tasklet = stackless.tasklet(tasklet_run)()
deadline = clock() + TIMEOUT
while tasklet.alive:
main.mainloop.loop()
if clock() > deadline:
raise stacklesslib.errors.TimeoutError("unittest took too long. Deadlock?")
if err:
try:
raise err[0][0], err[0][1], err[0][2]
finally:
err = None
def load_tests(loader, tests, pattern): # test loader protocol
suite = StacklessTestSuite()
suite.addTests(tests)
return suite