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
|
# Copyright 2017 Google LLC
#
# 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 datetime
import itertools
import mock
from google.api_core import timeout
def test__exponential_timeout_generator_base_2():
gen = timeout._exponential_timeout_generator(1.0, 60.0, 2.0, deadline=None)
result = list(itertools.islice(gen, 8))
assert result == [1, 2, 4, 8, 16, 32, 60, 60]
@mock.patch("google.api_core.datetime_helpers.utcnow", autospec=True)
def test__exponential_timeout_generator_base_deadline(utcnow):
# Make each successive call to utcnow() advance one second.
utcnow.side_effect = [
datetime.datetime.min + datetime.timedelta(seconds=n) for n in range(15)
]
gen = timeout._exponential_timeout_generator(1.0, 60.0, 2.0, deadline=30.0)
result = list(itertools.islice(gen, 14))
# Should grow until the cumulative time is > 30s, then start decreasing as
# the cumulative time approaches 60s.
assert result == [1, 2, 4, 8, 16, 24, 23, 22, 21, 20, 19, 18, 17, 16]
class TestConstantTimeout(object):
def test_constructor(self):
timeout_ = timeout.ConstantTimeout()
assert timeout_._timeout is None
def test_constructor_args(self):
timeout_ = timeout.ConstantTimeout(42.0)
assert timeout_._timeout == 42.0
def test___str__(self):
timeout_ = timeout.ConstantTimeout(1)
assert str(timeout_) == "<ConstantTimeout timeout=1.0>"
def test_apply(self):
target = mock.Mock(spec=["__call__", "__name__"], __name__="target")
timeout_ = timeout.ConstantTimeout(42.0)
wrapped = timeout_(target)
wrapped()
target.assert_called_once_with(timeout=42.0)
def test_apply_passthrough(self):
target = mock.Mock(spec=["__call__", "__name__"], __name__="target")
timeout_ = timeout.ConstantTimeout(42.0)
wrapped = timeout_(target)
wrapped(1, 2, meep="moop")
target.assert_called_once_with(1, 2, meep="moop", timeout=42.0)
class TestExponentialTimeout(object):
def test_constructor(self):
timeout_ = timeout.ExponentialTimeout()
assert timeout_._initial == timeout._DEFAULT_INITIAL_TIMEOUT
assert timeout_._maximum == timeout._DEFAULT_MAXIMUM_TIMEOUT
assert timeout_._multiplier == timeout._DEFAULT_TIMEOUT_MULTIPLIER
assert timeout_._deadline == timeout._DEFAULT_DEADLINE
def test_constructor_args(self):
timeout_ = timeout.ExponentialTimeout(1, 2, 3, 4)
assert timeout_._initial == 1
assert timeout_._maximum == 2
assert timeout_._multiplier == 3
assert timeout_._deadline == 4
def test_with_timeout(self):
original_timeout = timeout.ExponentialTimeout()
timeout_ = original_timeout.with_deadline(42)
assert original_timeout is not timeout_
assert timeout_._initial == timeout._DEFAULT_INITIAL_TIMEOUT
assert timeout_._maximum == timeout._DEFAULT_MAXIMUM_TIMEOUT
assert timeout_._multiplier == timeout._DEFAULT_TIMEOUT_MULTIPLIER
assert timeout_._deadline == 42
def test___str__(self):
timeout_ = timeout.ExponentialTimeout(1, 2, 3, 4)
assert str(timeout_) == (
"<ExponentialTimeout initial=1.0, maximum=2.0, multiplier=3.0, "
"deadline=4.0>"
)
def test_apply(self):
target = mock.Mock(spec=["__call__", "__name__"], __name__="target")
timeout_ = timeout.ExponentialTimeout(1, 10, 2)
wrapped = timeout_(target)
wrapped()
target.assert_called_with(timeout=1)
wrapped()
target.assert_called_with(timeout=2)
wrapped()
target.assert_called_with(timeout=4)
def test_apply_passthrough(self):
target = mock.Mock(spec=["__call__", "__name__"], __name__="target")
timeout_ = timeout.ExponentialTimeout(42.0, 100, 2)
wrapped = timeout_(target)
wrapped(1, 2, meep="moop")
target.assert_called_once_with(1, 2, meep="moop", timeout=42.0)
|