summaryrefslogtreecommitdiffstats
path: root/test/units/module_utils/test_api.py
blob: 0eaea04659812dfd486899477dc188fcc040623d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
# Copyright: (c) 2020, Abhijeet Kasurde <akasurde@redhat.com>
# 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()