forked from ansible/ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: time.clock compatible code (ansible#70650)
time.clock is removed in Python 3.8. Add time.clock compatible code. Fixes: ansible#70649 Signed-off-by: Abhijeet Kasurde <[email protected]>
- Loading branch information
Showing
4 changed files
with
113 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bugfixes: | ||
- api - time.clock is removed in Python 3.8, add backward compatible code (https://github.com/ansible/ansible/issues/70649). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,27 +4,9 @@ | |
# still belong to the author of the module, and may assign their own license | ||
# to the complete work. | ||
# | ||
# (c) 2015 Brian Ccoa, <[email protected]> | ||
# | ||
# Redistribution and use in source and binary forms, with or without modification, | ||
# are permitted provided that the following conditions are met: | ||
# | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
# Copyright: (c) 2015, Brian Coca, <[email protected]> | ||
# | ||
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) | ||
""" | ||
This module adds shared support for generic api modules | ||
|
@@ -44,6 +26,7 @@ | |
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
import sys | ||
import time | ||
|
||
|
||
|
@@ -91,12 +74,16 @@ def wrapper(f): | |
last = [0.0] | ||
|
||
def ratelimited(*args, **kwargs): | ||
if sys.version_info >= (3, 8): | ||
real_time = time.process_time | ||
else: | ||
real_time = time.clock | ||
if minrate is not None: | ||
elapsed = time.clock() - last[0] | ||
elapsed = real_time() - last[0] | ||
left = minrate - elapsed | ||
if left > 0: | ||
time.sleep(left) | ||
last[0] = time.clock() | ||
last[0] = real_time() | ||
ret = f(*args, **kwargs) | ||
return ret | ||
|
||
|
@@ -107,14 +94,13 @@ def ratelimited(*args, **kwargs): | |
def retry(retries=None, retry_pause=1): | ||
"""Retry decorator""" | ||
def wrapper(f): | ||
retry_count = 0 | ||
|
||
def retried(*args, **kwargs): | ||
retry_count = 0 | ||
if retries is not None: | ||
ret = None | ||
while True: | ||
# pylint doesn't understand this is a closure | ||
retry_count += 1 # pylint: disable=undefined-variable | ||
retry_count += 1 | ||
if retry_count >= retries: | ||
raise Exception("Retry limit exceeded: %d" % retries) | ||
try: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright: (c) 2020, Abhijeet Kasurde <[email protected]> | ||
# Copyright: (c) 2020, Ansible Project | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
|
||
from ansible.module_utils.api import rate_limit, retry | ||
|
||
import pytest | ||
|
||
|
||
class TestRateLimit: | ||
|
||
def test_ratelimit(self): | ||
@rate_limit(rate=1, rate_limit=1) | ||
def login_database(): | ||
return "success" | ||
r = login_database() | ||
|
||
assert r == 'success' | ||
|
||
|
||
class TestRetry: | ||
|
||
def test_no_retry_required(self): | ||
self.counter = 0 | ||
|
||
@retry(retries=4, retry_pause=2) | ||
def login_database(): | ||
self.counter += 1 | ||
return 'success' | ||
|
||
r = login_database() | ||
|
||
assert r == 'success' | ||
assert self.counter == 1 | ||
|
||
def test_catch_exception(self): | ||
|
||
@retry(retries=1) | ||
def login_database(): | ||
return 'success' | ||
|
||
with pytest.raises(Exception): | ||
login_database() |