mirror of
https://gitlab.com/apparmor/apparmor.git
synced 2025-03-04 08:24:42 +01:00
Replace sigalarm-based subprocess timeout with the built-in one
Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
This commit is contained in:
parent
68376e7fee
commit
ed0b539c94
1 changed files with 2 additions and 28 deletions
|
@ -98,12 +98,11 @@ class AATestTemplate(unittest.TestCase, metaclass=AANoCleanupMetaClass):
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
return 127, str(e), ''
|
return 127, str(e), ''
|
||||||
|
|
||||||
timeout_communicate = TimeoutFunction(sp.communicate, timeout)
|
|
||||||
out, outerr = (None, None)
|
out, outerr = (None, None)
|
||||||
try:
|
try:
|
||||||
out, outerr = timeout_communicate(input)
|
out, outerr = sp.communicate(input, timeout)
|
||||||
rc = sp.returncode
|
rc = sp.returncode
|
||||||
except TimeoutFunctionException:
|
except subprocess.TimeoutExpired:
|
||||||
sp.terminate()
|
sp.terminate()
|
||||||
outerr = 'test timed out, killed'
|
outerr = 'test timed out, killed'
|
||||||
rc = TIMEOUT_ERROR_CODE
|
rc = TIMEOUT_ERROR_CODE
|
||||||
|
@ -117,31 +116,6 @@ class AATestTemplate(unittest.TestCase, metaclass=AANoCleanupMetaClass):
|
||||||
|
|
||||||
return rc, out, outerr
|
return rc, out, outerr
|
||||||
|
|
||||||
|
|
||||||
# Timeout handler using alarm() from John P. Speno's Pythonic Avocado
|
|
||||||
class TimeoutFunctionException(Exception):
|
|
||||||
"""Exception to raise on a timeout"""
|
|
||||||
|
|
||||||
|
|
||||||
class TimeoutFunction:
|
|
||||||
def __init__(self, function, timeout):
|
|
||||||
self.timeout = timeout
|
|
||||||
self.function = function
|
|
||||||
|
|
||||||
def handle_timeout(self, signum, frame):
|
|
||||||
raise TimeoutFunctionException()
|
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
|
||||||
old = signal.signal(signal.SIGALRM, self.handle_timeout)
|
|
||||||
signal.alarm(self.timeout)
|
|
||||||
try:
|
|
||||||
result = self.function(*args, **kwargs)
|
|
||||||
finally:
|
|
||||||
signal.signal(signal.SIGALRM, old)
|
|
||||||
signal.alarm(0)
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def filesystem_time_resolution():
|
def filesystem_time_resolution():
|
||||||
"""detect whether the filesystem stores subsecond timestamps"""
|
"""detect whether the filesystem stores subsecond timestamps"""
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue