File size: 3,425 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2017 Google 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.

from unittest import mock
import pytest  # type: ignore

from google.resumable_media import common


class TestInvalidResponse(object):
    def test_constructor(self):
        response = mock.sentinel.response
        error = common.InvalidResponse(response, 1, "a", [b"m"], True)

        assert error.response is response
        assert error.args == (1, "a", [b"m"], True)


class TestRetryStrategy(object):
    def test_constructor_defaults(self):
        retry_strategy = common.RetryStrategy()
        assert retry_strategy.max_sleep == common.MAX_SLEEP
        assert retry_strategy.max_cumulative_retry == common.MAX_CUMULATIVE_RETRY
        assert retry_strategy.max_retries is None

    def test_constructor_failure(self):
        with pytest.raises(ValueError) as exc_info:
            common.RetryStrategy(max_cumulative_retry=600.0, max_retries=12)

        exc_info.match(common._SLEEP_RETRY_ERROR_MSG)

    def test_constructor_custom_delay_and_multiplier(self):
        retry_strategy = common.RetryStrategy(initial_delay=3.0, multiplier=4)
        assert retry_strategy.max_sleep == common.MAX_SLEEP
        assert retry_strategy.max_cumulative_retry == common.MAX_CUMULATIVE_RETRY
        assert retry_strategy.max_retries is None
        assert retry_strategy.initial_delay == 3.0
        assert retry_strategy.multiplier == 4

    def test_constructor_explicit_bound_cumulative(self):
        max_sleep = 10.0
        max_cumulative_retry = 100.0
        retry_strategy = common.RetryStrategy(
            max_sleep=max_sleep, max_cumulative_retry=max_cumulative_retry
        )

        assert retry_strategy.max_sleep == max_sleep
        assert retry_strategy.max_cumulative_retry == max_cumulative_retry
        assert retry_strategy.max_retries is None

    def test_constructor_explicit_bound_retries(self):
        max_sleep = 13.75
        max_retries = 14
        retry_strategy = common.RetryStrategy(
            max_sleep=max_sleep, max_retries=max_retries
        )

        assert retry_strategy.max_sleep == max_sleep
        assert retry_strategy.max_cumulative_retry is None
        assert retry_strategy.max_retries == max_retries

    def test_retry_allowed_bound_cumulative(self):
        retry_strategy = common.RetryStrategy(max_cumulative_retry=100.0)
        assert retry_strategy.retry_allowed(50.0, 10)
        assert retry_strategy.retry_allowed(99.0, 7)
        assert retry_strategy.retry_allowed(100.0, 4)
        assert not retry_strategy.retry_allowed(101.0, 11)
        assert not retry_strategy.retry_allowed(200.0, 6)

    def test_retry_allowed_bound_retries(self):
        retry_strategy = common.RetryStrategy(max_retries=6)
        assert retry_strategy.retry_allowed(1000.0, 5)
        assert retry_strategy.retry_allowed(99.0, 6)
        assert not retry_strategy.retry_allowed(625.5, 7)