summaryrefslogtreecommitdiff
path: root/oslo_messaging/tests/test_utils.py
blob: 256a69439c0e7f71ca9df931c6d18e0f0321cb9c (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

# Copyright 2013 Red Hat, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import threading

from oslo_messaging._drivers import common
from oslo_messaging import _utils as utils
from oslo_messaging.tests import utils as test_utils

import six
from six.moves import mock


class VersionIsCompatibleTestCase(test_utils.BaseTestCase):
    def test_version_is_compatible_same(self):
        self.assertTrue(utils.version_is_compatible('1.23', '1.23'))

    def test_version_is_compatible_newer_minor(self):
        self.assertTrue(utils.version_is_compatible('1.24', '1.23'))

    def test_version_is_compatible_older_minor(self):
        self.assertFalse(utils.version_is_compatible('1.22', '1.23'))

    def test_version_is_compatible_major_difference1(self):
        self.assertFalse(utils.version_is_compatible('2.23', '1.23'))

    def test_version_is_compatible_major_difference2(self):
        self.assertFalse(utils.version_is_compatible('1.23', '2.23'))

    def test_version_is_compatible_newer_rev(self):
        self.assertFalse(utils.version_is_compatible('1.23', '1.23.1'))

    def test_version_is_compatible_newer_rev_both(self):
        self.assertFalse(utils.version_is_compatible('1.23.1', '1.23.2'))

    def test_version_is_compatible_older_rev_both(self):
        self.assertTrue(utils.version_is_compatible('1.23.2', '1.23.1'))

    def test_version_is_compatible_older_rev(self):
        self.assertTrue(utils.version_is_compatible('1.24', '1.23.1'))

    def test_version_is_compatible_no_rev_is_zero(self):
        self.assertTrue(utils.version_is_compatible('1.23.0', '1.23'))


class TimerTestCase(test_utils.BaseTestCase):
    def test_no_duration_no_callback(self):
        t = common.DecayingTimer()
        t.start()
        remaining = t.check_return()
        self.assertIsNone(remaining)

    def test_no_duration_but_maximum(self):
        t = common.DecayingTimer()
        t.start()
        remaining = t.check_return(maximum=2)
        self.assertEqual(2, remaining)

    @mock.patch('oslo_utils.timeutils.now')
    def test_duration_expired_no_callback(self, now):
        now.return_value = 0
        t = common.DecayingTimer(2)
        t.start()

        now.return_value = 3
        remaining = t.check_return()
        self.assertEqual(0, remaining)

    @mock.patch('oslo_utils.timeutils.now')
    def test_duration_callback(self, now):
        now.return_value = 0
        t = common.DecayingTimer(2)
        t.start()

        now.return_value = 3
        callback = mock.Mock()
        remaining = t.check_return(callback)
        self.assertEqual(0, remaining)
        callback.assert_called_once_with()

    @mock.patch('oslo_utils.timeutils.now')
    def test_duration_callback_with_args(self, now):
        now.return_value = 0
        t = common.DecayingTimer(2)
        t.start()

        now.return_value = 3
        callback = mock.Mock()
        remaining = t.check_return(callback, 1, a='b')
        self.assertEqual(0, remaining)
        callback.assert_called_once_with(1, a='b')


class EventCompatTestCase(test_utils.BaseTestCase):
    @mock.patch('oslo_messaging._utils._Event.clear')
    def test_event_api_compat(self, mock_clear):
        with mock.patch('oslo_messaging._utils._is_monkey_patched',
                        return_value=True):
            e_event = utils.Event()
        self.assertIsInstance(e_event, utils._Event)

        with mock.patch('oslo_messaging._utils._is_monkey_patched',
                        return_value=False):
            t_event = utils.Event()
        if six.PY3:
            t_event_cls = threading.Event
        else:
            t_event_cls = threading._Event
        self.assertIsInstance(t_event, t_event_cls)

        public_methods = [m for m in dir(t_event) if not m.startswith("_") and
                          callable(getattr(t_event, m))]

        for method in public_methods:
            self.assertTrue(hasattr(e_event, method))