blob: d6d0aee8c6fa4e65d13a2698b50a8d2009055b34 [file] [log] [blame]
Chandan Kumar5e619872017-09-07 22:23:55 +05301# Copyright 2011, VMware, Inc.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15#
16# Borrowed from nova code base, more utilities will be added/borrowed as and
17# when needed.
18
19"""Utilities and helper functions."""
20
Chandan Kumar5e619872017-09-07 22:23:55 +053021import eventlet
Brian Haleyba800452017-12-14 10:30:48 -050022import functools
Chandan Kumar667d3d32017-09-22 12:24:06 +053023import threading
24import time
25
Chandan Kumar5e619872017-09-07 22:23:55 +053026
27class classproperty(object):
28 def __init__(self, f):
29 self.func = f
30
31 def __get__(self, obj, owner):
Chandan Kumar667d3d32017-09-22 12:24:06 +053032 return self.func(owner)
33
Chandan Kumar5e619872017-09-07 22:23:55 +053034
35class WaitTimeout(Exception):
36 """Default exception coming from wait_until_true() function."""
37
38
39class LockWithTimer(object):
40 def __init__(self, threshold):
41 self._threshold = threshold
42 self.timestamp = 0
43 self._lock = threading.Lock()
44
45 def acquire(self):
46 return self._lock.acquire(False)
47
48 def release(self):
49 return self._lock.release()
50
51 def time_to_wait(self):
52 return self.timestamp - time.time() + self._threshold
53
Chandan Kumar667d3d32017-09-22 12:24:06 +053054
Chandan Kumar5e619872017-09-07 22:23:55 +053055def wait_until_true(predicate, timeout=60, sleep=1, exception=None):
56 """
57 Wait until callable predicate is evaluated as True
58 :param predicate: Callable deciding whether waiting should continue.
59 Best practice is to instantiate predicate with functools.partial()
60 :param timeout: Timeout in seconds how long should function wait.
61 :param sleep: Polling interval for results in seconds.
62 :param exception: Exception instance to raise on timeout. If None is passed
63 (default) then WaitTimeout exception is raised.
64 """
65 try:
66 with eventlet.Timeout(timeout):
67 while not predicate():
68 eventlet.sleep(sleep)
69 except eventlet.Timeout:
70 if exception is not None:
71 #pylint: disable=raising-bad-type
72 raise exception
73 raise WaitTimeout("Timed out after %d seconds" % timeout)
Brian Haleyba800452017-12-14 10:30:48 -050074
75
76# TODO(haleyb): move to neutron-lib
77# code copied from neutron repository - neutron/tests/base.py
78def unstable_test(reason):
79 def decor(f):
80 @functools.wraps(f)
81 def inner(self, *args, **kwargs):
82 try:
83 return f(self, *args, **kwargs)
84 except Exception as e:
85 msg = ("%s was marked as unstable because of %s, "
86 "failure was: %s") % (self.id(), reason, e)
87 raise self.skipTest(msg)
88 return inner
89 return decor