summaryrefslogtreecommitdiff
path: root/t/unit/utils/test_functional.py
blob: ab3852db1dac4aa5d2bb8636cd12da7d35d9bdf0 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import pickle

import pytest

from itertools import count

from case import Mock, mock

from kombu.utils import functional as utils
from kombu.utils.functional import (
    ChannelPromise, LRUCache, fxrange, fxrangemax, memoize, lazy,
    maybe_evaluate, maybe_list, reprcall, reprkwargs, retry_over_time,
    accepts_argument,
)


class test_ChannelPromise:

    def test_repr(self):
        obj = Mock(name='cb')
        assert 'promise' in repr(ChannelPromise(obj))
        obj.assert_not_called()


class test_shufflecycle:

    def test_shuffles(self):
        prev_repeat, utils.repeat = utils.repeat, Mock()
        try:
            utils.repeat.return_value = list(range(10))
            values = {'A', 'B', 'C'}
            cycle = utils.shufflecycle(values)
            seen = set()
            for i in range(10):
                next(cycle)
            utils.repeat.assert_called_with(None)
            assert seen.issubset(values)
            with pytest.raises(StopIteration):
                next(cycle)
                next(cycle)
        finally:
            utils.repeat = prev_repeat


def double(x):
    return x * 2


class test_LRUCache:

    def test_expires(self):
        limit = 100
        x = LRUCache(limit=limit)
        slots = list(range(limit * 2))
        for i in slots:
            x[i] = i
        assert list(x.keys()) == list(slots[limit:])
        assert x.items()
        assert x.values()

    def test_is_pickleable(self):
        x = LRUCache(limit=10)
        x.update(luke=1, leia=2)
        y = pickle.loads(pickle.dumps(x))
        assert y.limit == y.limit
        assert y == x

    def test_update_expires(self):
        limit = 100
        x = LRUCache(limit=limit)
        slots = list(range(limit * 2))
        for i in slots:
            x.update({i: i})

        assert list(x.keys()) == list(slots[limit:])

    def test_least_recently_used(self):
        x = LRUCache(3)

        x[1], x[2], x[3] = 1, 2, 3
        assert list(x.keys()), [1, 2 == 3]

        x[4], x[5] = 4, 5
        assert list(x.keys()), [3, 4 == 5]

        # access 3, which makes it the last used key.
        x[3]
        x[6] = 6
        assert list(x.keys()), [5, 3 == 6]

        x[7] = 7
        assert list(x.keys()), [3, 6 == 7]

    def test_update_larger_than_cache_size(self):
        x = LRUCache(2)
        x.update({x: x for x in range(100)})
        assert list(x.keys()), [98 == 99]

    def test_items(self):
        c = LRUCache()
        c.update(a=1, b=2, c=3)
        assert list(c.items())

    def test_incr(self):
        c = LRUCache()
        c.update(a='1')
        c.incr('a')
        assert c['a'] == '2'


def test_memoize():
    counter = count(1)

    @memoize(maxsize=2)
    def x(i):
        return next(counter)

    assert x(1) == 1
    assert x(1) == 1
    assert x(2) == 2
    assert x(3) == 3
    assert x(1) == 4
    x.clear()
    assert x(3) == 5


class test_lazy:

    def test__str__(self):
        assert (str(lazy(lambda: 'the quick brown fox')) ==
                'the quick brown fox')

    def test__repr__(self):
        assert repr(lazy(lambda: 'fi fa fo')).strip('u') == "'fi fa fo'"

    def test_evaluate(self):
        assert lazy(lambda: 2 + 2)() == 4
        assert lazy(lambda x: x * 4, 2) == 8
        assert lazy(lambda x: x * 8, 2)() == 16

    def test_cmp(self):
        assert lazy(lambda: 10) == lazy(lambda: 10)
        assert lazy(lambda: 10) != lazy(lambda: 20)

    def test__reduce__(self):
        x = lazy(double, 4)
        y = pickle.loads(pickle.dumps(x))
        assert x() == y()

    def test__deepcopy__(self):
        from copy import deepcopy
        x = lazy(double, 4)
        y = deepcopy(x)
        assert x._fun == y._fun
        assert x._args == y._args
        assert x() == y()


@pytest.mark.parametrize('obj,expected', [
    (lazy(lambda: 10), 10),
    (20, 20),
])
def test_maybe_evaluate(obj, expected):
    assert maybe_evaluate(obj) == expected


class test_retry_over_time:

    class Predicate(Exception):
        pass

    def setup(self):
        self.index = 0

    def myfun(self):
        if self.index < 9:
            raise self.Predicate()
        return 42

    def errback(self, exc, intervals, retries):
        interval = next(intervals)
        sleepvals = (None, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 16.0)
        self.index += 1
        assert interval == sleepvals[self.index]
        return interval

    @mock.sleepdeprived(module=utils)
    def test_simple(self):
        prev_count, utils.count = utils.count, Mock()
        try:
            utils.count.return_value = list(range(1))
            x = retry_over_time(self.myfun, self.Predicate,
                                errback=None, interval_max=14)
            assert x is None
            utils.count.return_value = list(range(10))
            cb = Mock()
            x = retry_over_time(self.myfun, self.Predicate,
                                errback=self.errback, callback=cb,
                                interval_max=14)
            assert x == 42
            assert self.index == 9
            cb.assert_called_with()
        finally:
            utils.count = prev_count

    def test_retry_timeout(self):

        with pytest.raises(self.Predicate):
            retry_over_time(
                self.myfun, self.Predicate,
                errback=self.errback, interval_max=14, timeout=1
            )
        assert self.index == 1

        # no errback
        with pytest.raises(self.Predicate):
            retry_over_time(
                self.myfun, self.Predicate,
                errback=None, timeout=1,
            )

    @mock.sleepdeprived(module=utils)
    def test_retry_zero(self):
        with pytest.raises(self.Predicate):
            retry_over_time(
                self.myfun, self.Predicate,
                max_retries=0, errback=self.errback, interval_max=14,
            )
        assert self.index == 0
        # no errback
        with pytest.raises(self.Predicate):
            retry_over_time(
                self.myfun, self.Predicate,
                max_retries=0, errback=None, interval_max=14,
            )

    @mock.sleepdeprived(module=utils)
    def test_retry_once(self):
        with pytest.raises(self.Predicate):
            retry_over_time(
                self.myfun, self.Predicate,
                max_retries=1, errback=self.errback, interval_max=14,
            )
        assert self.index == 1
        # no errback
        with pytest.raises(self.Predicate):
            retry_over_time(
                self.myfun, self.Predicate,
                max_retries=1, errback=None, interval_max=14,
            )

    @mock.sleepdeprived(module=utils)
    def test_retry_always(self):
        Predicate = self.Predicate

        class Fun:

            def __init__(self):
                self.calls = 0

            def __call__(self, *args, **kwargs):
                try:
                    if self.calls >= 10:
                        return 42
                    raise Predicate()
                finally:
                    self.calls += 1
        fun = Fun()

        assert retry_over_time(
            fun, self.Predicate,
            max_retries=None, errback=None, interval_max=14) == 42
        assert fun.calls == 11


@pytest.mark.parametrize('obj,expected', [
    (None, None),
    (1, [1]),
    ([1, 2, 3], [1, 2, 3]),
])
def test_maybe_list(obj, expected):
    assert maybe_list(obj) == expected


def test_fxrange__no_repeatlast():
    assert list(fxrange(1.0, 3.0, 1.0)) == [1.0, 2.0, 3.0]


@pytest.mark.parametrize('args,expected', [
    ((1.0, 3.0, 1.0, 30.0),
     [1.0, 2.0, 3.0, 3.0, 3.0, 3.0,
      3.0, 3.0, 3.0, 3.0, 3.0]),
    ((1.0, None, 1.0, 30.0),
     [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]),
])
def test_fxrangemax(args, expected):
    assert list(fxrangemax(*args)) == expected


def test_reprkwargs():
    assert reprkwargs({'foo': 'bar', 1: 2, 'k': 'v'})


def test_reprcall():
    assert reprcall('add', (2, 2), {'copy': True})


class test_accepts_arg:
    def function(self, foo, bar, baz="baz"):
        pass

    def test_valid_argument(self):
        assert accepts_argument(self.function, 'self')
        assert accepts_argument(self.function, 'foo')
        assert accepts_argument(self.function, 'baz')

    def test_invalid_argument(self):
        assert not accepts_argument(self.function, 'random_argument')

    def test_raise_exception(self):
        with pytest.raises(Exception):
            accepts_argument(None, 'foo')