mirror of
https://git.yoctoproject.org/poky
synced 2026-04-18 03:32:13 +02:00
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>
This commit is contained in:
committed by
Richard Purdie
parent
f08baeed2c
commit
ad0aa8d548
@@ -11,6 +11,8 @@ import logging
|
||||
import sys
|
||||
import unittest
|
||||
import threading
|
||||
import signal
|
||||
from functools import wraps
|
||||
|
||||
#get the "result" object from one of the upper frames provided that one of these upper frames is a unittest.case frame
|
||||
class getResults(object):
|
||||
@@ -160,3 +162,27 @@ def LogResults(original_class):
|
||||
|
||||
original_class.run = run
|
||||
return original_class
|
||||
|
||||
class TimeOut(BaseException):
|
||||
pass
|
||||
|
||||
def timeout(seconds):
|
||||
def decorator(fn):
|
||||
if hasattr(signal, 'alarm'):
|
||||
@wraps(fn)
|
||||
def wrapped_f(*args, **kw):
|
||||
current_frame = sys._getframe()
|
||||
def raiseTimeOut(signal, frame):
|
||||
if frame is not current_frame:
|
||||
raise TimeOut('%s seconds' % seconds)
|
||||
prev_handler = signal.signal(signal.SIGALRM, raiseTimeOut)
|
||||
try:
|
||||
signal.alarm(seconds)
|
||||
return fn(*args, **kw)
|
||||
finally:
|
||||
signal.alarm(0)
|
||||
signal.signal(signal.SIGALRM, prev_handler)
|
||||
return wrapped_f
|
||||
else:
|
||||
return fn
|
||||
return decorator
|
||||
Reference in New Issue
Block a user