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
|
# 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
from unittest import mock
from google.api_core import timeout as timeouts
def test__exponential_timeout_generator_base_2():
gen = timeouts._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 = timeouts._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 TestTimeToDeadlineTimeout(object):
def test_constructor(self):
timeout_ = timeouts.TimeToDeadlineTimeout()
assert timeout_._timeout is None
def test_constructor_args(self):
timeout_ = timeouts.TimeToDeadlineTimeout(42.0)
assert timeout_._timeout == 42.0
def test___str__(self):
timeout_ = timeouts.TimeToDeadlineTimeout(1)
assert str(timeout_) == "<TimeToDeadlineTimeout timeout=1.0>"
def test_apply(self):
target = mock.Mock(spec=["__call__", "__name__"], __name__="target")
datetime.datetime.now(tz=datetime.timezone.utc)
datetime.timedelta(seconds=1)
now = datetime.datetime.now(tz=datetime.timezone.utc)
times = [
now,
now + datetime.timedelta(seconds=0.0009),
now + datetime.timedelta(seconds=1),
now + datetime.timedelta(seconds=39),
now + datetime.timedelta(seconds=42),
now + datetime.timedelta(seconds=43),
]
def _clock():
return times.pop(0)
timeout_ = timeouts.TimeToDeadlineTimeout(42.0, _clock)
wrapped = timeout_(target)
wrapped()
target.assert_called_with(timeout=42.0)
wrapped()
target.assert_called_with(timeout=41.0)
wrapped()
target.assert_called_with(timeout=3.0)
wrapped()
target.assert_called_with(timeout=0.0)
wrapped()
target.assert_called_with(timeout=0.0)
def test_apply_no_timeout(self):
target = mock.Mock(spec=["__call__", "__name__"], __name__="target")
datetime.datetime.now(tz=datetime.timezone.utc)
datetime.timedelta(seconds=1)
now = datetime.datetime.now(tz=datetime.timezone.utc)
times = [
now,
now + datetime.timedelta(seconds=0.0009),
now + datetime.timedelta(seconds=1),
now + datetime.timedelta(seconds=2),
]
def _clock():
return times.pop(0)
timeout_ = timeouts.TimeToDeadlineTimeout(clock=_clock)
wrapped = timeout_(target)
wrapped()
target.assert_called_with()
wrapped()
target.assert_called_with()
def test_apply_passthrough(self):
target = mock.Mock(spec=["__call__", "__name__"], __name__="target")
timeout_ = timeouts.TimeToDeadlineTimeout(42.0)
wrapped = timeout_(target)
wrapped(1, 2, meep="moop")
target.assert_called_once_with(1, 2, meep="moop", timeout=42.0)
class TestConstantTimeout(object):
def test_constructor(self):
timeout_ = timeouts.ConstantTimeout()
assert timeout_._timeout is None
def test_constructor_args(self):
timeout_ = timeouts.ConstantTimeout(42.0)
assert timeout_._timeout == 42.0
def test___str__(self):
timeout_ = timeouts.ConstantTimeout(1)
assert str(timeout_) == "<ConstantTimeout timeout=1.0>"
def test_apply(self):
target = mock.Mock(spec=["__call__", "__name__"], __name__="target")
timeout_ = timeouts.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_ = timeouts.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_ = timeouts.ExponentialTimeout()
assert timeout_._initial == timeouts._DEFAULT_INITIAL_TIMEOUT
assert timeout_._maximum == timeouts._DEFAULT_MAXIMUM_TIMEOUT
assert timeout_._multiplier == timeouts._DEFAULT_TIMEOUT_MULTIPLIER
assert timeout_._deadline == timeouts._DEFAULT_DEADLINE
def test_constructor_args(self):
timeout_ = timeouts.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 = timeouts.ExponentialTimeout()
timeout_ = original_timeout.with_deadline(42)
assert original_timeout is not timeout_
assert timeout_._initial == timeouts._DEFAULT_INITIAL_TIMEOUT
assert timeout_._maximum == timeouts._DEFAULT_MAXIMUM_TIMEOUT
assert timeout_._multiplier == timeouts._DEFAULT_TIMEOUT_MULTIPLIER
assert timeout_._deadline == 42
def test___str__(self):
timeout_ = timeouts.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_ = timeouts.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_ = timeouts.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)
|