Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions nose/tools/nontrivial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"""Tools not exempt from being descended into in tracebacks"""

import time


__all__ = ['make_decorator', 'raises', 'set_trace', 'timed', 'with_setup',
'TimeExpired', 'istest', 'nottest']

Expand Down Expand Up @@ -83,22 +80,25 @@ def set_trace():


def timed(limit):
"""Test must finish within specified time limit to pass.
import multiprocessing
from multiprocessing.pool import ThreadPool

"""Test must finish within specified time limit to pass.
Example use::

@timed(.1)
def test_that_fails():
time.sleep(.2)
"""
def decorate(func):
def newfunc(*arg, **kw):
start = time.time()
result = func(*arg, **kw)
end = time.time()
if end - start > limit:
pool = ThreadPool(1)
async_result = pool.apply_async(func, args=arg, kwds=kw)

try:
return async_result.get(timeout=limit)
except multiprocessing.TimeoutError:
raise TimeExpired("Time limit (%s) exceeded" % limit)
return result

newfunc = make_decorator(func)(newfunc)
return newfunc
return decorate
Expand Down
3 changes: 2 additions & 1 deletion unit_tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ def quick():
def check_result():
return 42
check_result = timed(.2)(check_result)
result = check_result()

assert 42 == check_result()
assert 42 == result, result

quick()
try:
Expand Down