diff options
author | Lucian Musat <george.l.musat@intel.com> | 2015-06-10 13:52:40 +0300 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2015-06-11 23:59:17 +0100 |
commit | ad0aa8d548e21e6e262c4b0d9fe7eb72b1b2c24f (patch) | |
tree | f273087f44eb657005350ca90cd01a4edc1eaf74 /meta/lib/oeqa/utils | |
parent | f08baeed2cf480dfbd186493bfeaace193256ba7 (diff) | |
download | poky-ad0aa8d548e21e6e262c4b0d9fe7eb72b1b2c24f.tar.gz |
oeqa/utils: Added timeout decorator for testcases.
(From OE-Core rev: c6fe26ade5734efb5250e00c56fdbb4095b0018b)
Signed-off-by: Lucian Musat <george.l.musat@intel.com>
Signed-off-by: Ross Burton <ross.burton@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/lib/oeqa/utils')
-rw-r--r-- | meta/lib/oeqa/utils/decorators.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/meta/lib/oeqa/utils/decorators.py b/meta/lib/oeqa/utils/decorators.py index 61a1a739ea..b9fc76c9ee 100644 --- a/meta/lib/oeqa/utils/decorators.py +++ b/meta/lib/oeqa/utils/decorators.py | |||
@@ -11,6 +11,8 @@ import logging | |||
11 | import sys | 11 | import sys |
12 | import unittest | 12 | import unittest |
13 | import threading | 13 | import threading |
14 | import signal | ||
15 | from functools import wraps | ||
14 | 16 | ||
15 | #get the "result" object from one of the upper frames provided that one of these upper frames is a unittest.case frame | 17 | #get the "result" object from one of the upper frames provided that one of these upper frames is a unittest.case frame |
16 | class getResults(object): | 18 | class getResults(object): |
@@ -160,3 +162,27 @@ def LogResults(original_class): | |||
160 | 162 | ||
161 | original_class.run = run | 163 | original_class.run = run |
162 | return original_class | 164 | return original_class |
165 | |||
166 | class TimeOut(BaseException): | ||
167 | pass | ||
168 | |||
169 | def timeout(seconds): | ||
170 | def decorator(fn): | ||
171 | if hasattr(signal, 'alarm'): | ||
172 | @wraps(fn) | ||
173 | def wrapped_f(*args, **kw): | ||
174 | current_frame = sys._getframe() | ||
175 | def raiseTimeOut(signal, frame): | ||
176 | if frame is not current_frame: | ||
177 | raise TimeOut('%s seconds' % seconds) | ||
178 | prev_handler = signal.signal(signal.SIGALRM, raiseTimeOut) | ||
179 | try: | ||
180 | signal.alarm(seconds) | ||
181 | return fn(*args, **kw) | ||
182 | finally: | ||
183 | signal.alarm(0) | ||
184 | signal.signal(signal.SIGALRM, prev_handler) | ||
185 | return wrapped_f | ||
186 | else: | ||
187 | return fn | ||
188 | return decorator \ No newline at end of file | ||