aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/common
diff options
context:
space:
mode:
authorRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>2018-07-26 15:21:45 +0100
committerRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>2018-07-26 15:32:46 +0100
commitb76f841ab41ce2965e6867a04177beb0affd0c10 (patch)
tree978e2ccd75a655e185ec5f667761233af9759490 /yardstick/common
parent305b69cc6b840ced701a09bca0435937dcb42723 (diff)
Enable "wait_until_true" when used ouf the main thread
"util.wait_until_true" uses "util.Timer" to create an active wait for a condition. "Timer" class uses "signal" to create a watchdog to track the time lapsed. When used out of the main thread, "Timer" raises the following error: ValueError: signal only works in main thread To make "util.wait_until_true" usable always, a new waitting method is implemented. JIRA: YARDSTICK-1358 Change-Id: Ifb5ba0b17b5beca0af5ceab4f6431d58b7928762 Signed-off-by: Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Diffstat (limited to 'yardstick/common')
-rw-r--r--yardstick/common/utils.py25
1 files changed, 21 insertions, 4 deletions
diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py
index 83ddbd470..c74dd675e 100644
--- a/yardstick/common/utils.py
+++ b/yardstick/common/utils.py
@@ -28,6 +28,7 @@ import socket
import subprocess
import sys
import time
+import threading
import six
from flask import jsonify
@@ -475,6 +476,9 @@ class Timer(object):
def __del__(self): # pragma: no cover
signal.alarm(0)
+ def delta_time_sec(self):
+ return (datetime.datetime.now() - self.start).total_seconds()
+
def read_meminfo(ssh_client):
"""Read "/proc/meminfo" file and parse all keys and values"""
@@ -521,17 +525,30 @@ def open_relative_file(path, task_path):
def wait_until_true(predicate, timeout=60, sleep=1, exception=None):
"""Wait until callable predicate is evaluated as True
+ When in a thread different from the main one, Timer(timeout) will fail
+ because signal is not handled. In this case
+
:param predicate: (func) callable deciding whether waiting should continue
:param timeout: (int) timeout in seconds how long should function wait
:param sleep: (int) polling interval for results in seconds
:param exception: exception instance to raise on timeout. If None is passed
(default) then WaitTimeout exception is raised.
"""
- try:
- with Timer(timeout=timeout):
- while not predicate():
+ if isinstance(threading.current_thread(), threading._MainThread):
+ try:
+ with Timer(timeout=timeout):
+ while not predicate():
+ time.sleep(sleep)
+ except exceptions.TimerTimeout:
+ if exception and issubclass(exception, Exception):
+ raise exception # pylint: disable=raising-bad-type
+ raise exceptions.WaitTimeout
+ else:
+ with Timer() as timer:
+ while timer.delta_time_sec() < timeout:
+ if predicate():
+ return
time.sleep(sleep)
- except exceptions.TimerTimeout:
if exception and issubclass(exception, Exception):
raise exception # pylint: disable=raising-bad-type
raise exceptions.WaitTimeout