diff options
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 | ||