blob: fa7bb8b163dc8e14d41b8c4e7d6c1981ba0370bf [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
Brian Haleyba800452017-12-14 10:30:48 -050021import functools
Chandan Kumar667d3d32017-09-22 12:24:06 +053022import threading
23import time
24
Brian Haley33ef4602018-04-26 14:37:49 -040025import eventlet
26
Chandan Kumar5e619872017-09-07 22:23:55 +053027
28class classproperty(object):
29 def __init__(self, f):
30 self.func = f
31
32 def __get__(self, obj, owner):
Chandan Kumar667d3d32017-09-22 12:24:06 +053033 return self.func(owner)
34
Chandan Kumar5e619872017-09-07 22:23:55 +053035
36class WaitTimeout(Exception):
37 """Default exception coming from wait_until_true() function."""
38
39
40class LockWithTimer(object):
41 def __init__(self, threshold):
42 self._threshold = threshold
43 self.timestamp = 0
44 self._lock = threading.Lock()
45
46 def acquire(self):
47 return self._lock.acquire(False)
48
49 def release(self):
50 return self._lock.release()
51
52 def time_to_wait(self):
53 return self.timestamp - time.time() + self._threshold
54
Chandan Kumar667d3d32017-09-22 12:24:06 +053055
Chandan Kumar5e619872017-09-07 22:23:55 +053056def wait_until_true(predicate, timeout=60, sleep=1, exception=None):
Brian Haleyae328b92018-10-09 19:51:54 -040057 """Wait until callable predicate is evaluated as True
58
Chandan Kumar5e619872017-09-07 22:23:55 +053059 :param predicate: Callable deciding whether waiting should continue.
60 Best practice is to instantiate predicate with functools.partial()
61 :param timeout: Timeout in seconds how long should function wait.
62 :param sleep: Polling interval for results in seconds.
63 :param exception: Exception instance to raise on timeout. If None is passed
64 (default) then WaitTimeout exception is raised.
65 """
66 try:
67 with eventlet.Timeout(timeout):
68 while not predicate():
69 eventlet.sleep(sleep)
70 except eventlet.Timeout:
71 if exception is not None:
Brian Haley8aaa73f2018-10-09 19:55:44 -040072 # pylint: disable=raising-bad-type
Chandan Kumar5e619872017-09-07 22:23:55 +053073 raise exception
74 raise WaitTimeout("Timed out after %d seconds" % timeout)
Brian Haleyba800452017-12-14 10:30:48 -050075
76
77# TODO(haleyb): move to neutron-lib
78# code copied from neutron repository - neutron/tests/base.py
79def unstable_test(reason):
80 def decor(f):
81 @functools.wraps(f)
82 def inner(self, *args, **kwargs):
83 try:
84 return f(self, *args, **kwargs)
85 except Exception as e:
86 msg = ("%s was marked as unstable because of %s, "
87 "failure was: %s") % (self.id(), reason, e)
88 raise self.skipTest(msg)
89 return inner
90 return decor