Chandan Kumar | 5e61987 | 2017-09-07 22:23:55 +0530 | [diff] [blame] | 1 | # 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 Haley | ba80045 | 2017-12-14 10:30:48 -0500 | [diff] [blame] | 21 | import functools |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 22 | import threading |
| 23 | import time |
| 24 | |
Brian Haley | 33ef460 | 2018-04-26 14:37:49 -0400 | [diff] [blame] | 25 | import eventlet |
| 26 | |
Chandan Kumar | 5e61987 | 2017-09-07 22:23:55 +0530 | [diff] [blame] | 27 | |
| 28 | class classproperty(object): |
| 29 | def __init__(self, f): |
| 30 | self.func = f |
| 31 | |
| 32 | def __get__(self, obj, owner): |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 33 | return self.func(owner) |
| 34 | |
Chandan Kumar | 5e61987 | 2017-09-07 22:23:55 +0530 | [diff] [blame] | 35 | |
| 36 | class WaitTimeout(Exception): |
| 37 | """Default exception coming from wait_until_true() function.""" |
| 38 | |
| 39 | |
| 40 | class 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 Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 55 | |
Chandan Kumar | 5e61987 | 2017-09-07 22:23:55 +0530 | [diff] [blame] | 56 | def wait_until_true(predicate, timeout=60, sleep=1, exception=None): |
Brian Haley | ae328b9 | 2018-10-09 19:51:54 -0400 | [diff] [blame] | 57 | """Wait until callable predicate is evaluated as True |
| 58 | |
Chandan Kumar | 5e61987 | 2017-09-07 22:23:55 +0530 | [diff] [blame] | 59 | :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 Haley | 8aaa73f | 2018-10-09 19:55:44 -0400 | [diff] [blame] | 72 | # pylint: disable=raising-bad-type |
Chandan Kumar | 5e61987 | 2017-09-07 22:23:55 +0530 | [diff] [blame] | 73 | raise exception |
| 74 | raise WaitTimeout("Timed out after %d seconds" % timeout) |
Brian Haley | ba80045 | 2017-12-14 10:30:48 -0500 | [diff] [blame] | 75 | |
| 76 | |
| 77 | # TODO(haleyb): move to neutron-lib |
| 78 | # code copied from neutron repository - neutron/tests/base.py |
| 79 | def 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 |