• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import pytest
16
17try:
18    import grpc  # noqa: F401
19except ImportError:
20    pytest.skip("No GRPC", allow_module_level=True)
21
22from google.api_core import exceptions
23from google.api_core.gapic_v1 import config
24
25
26INTERFACE_CONFIG = {
27    "retry_codes": {
28        "idempotent": ["DEADLINE_EXCEEDED", "UNAVAILABLE"],
29        "other": ["FAILED_PRECONDITION"],
30        "non_idempotent": [],
31    },
32    "retry_params": {
33        "default": {
34            "initial_retry_delay_millis": 1000,
35            "retry_delay_multiplier": 2.5,
36            "max_retry_delay_millis": 120000,
37            "initial_rpc_timeout_millis": 120000,
38            "rpc_timeout_multiplier": 1.0,
39            "max_rpc_timeout_millis": 120000,
40            "total_timeout_millis": 600000,
41        },
42        "other": {
43            "initial_retry_delay_millis": 1000,
44            "retry_delay_multiplier": 1,
45            "max_retry_delay_millis": 1000,
46            "initial_rpc_timeout_millis": 1000,
47            "rpc_timeout_multiplier": 1,
48            "max_rpc_timeout_millis": 1000,
49            "total_timeout_millis": 1000,
50        },
51    },
52    "methods": {
53        "AnnotateVideo": {
54            "timeout_millis": 60000,
55            "retry_codes_name": "idempotent",
56            "retry_params_name": "default",
57        },
58        "Other": {
59            "timeout_millis": 60000,
60            "retry_codes_name": "other",
61            "retry_params_name": "other",
62        },
63        "Plain": {"timeout_millis": 30000},
64    },
65}
66
67
68def test_create_method_configs():
69    method_configs = config.parse_method_configs(INTERFACE_CONFIG)
70
71    retry, timeout = method_configs["AnnotateVideo"]
72    assert retry._predicate(exceptions.DeadlineExceeded(None))
73    assert retry._predicate(exceptions.ServiceUnavailable(None))
74    assert retry._initial == 1.0
75    assert retry._multiplier == 2.5
76    assert retry._maximum == 120.0
77    assert retry._deadline == 600.0
78    assert timeout._initial == 120.0
79    assert timeout._multiplier == 1.0
80    assert timeout._maximum == 120.0
81
82    retry, timeout = method_configs["Other"]
83    assert retry._predicate(exceptions.FailedPrecondition(None))
84    assert retry._initial == 1.0
85    assert retry._multiplier == 1.0
86    assert retry._maximum == 1.0
87    assert retry._deadline == 1.0
88    assert timeout._initial == 1.0
89    assert timeout._multiplier == 1.0
90    assert timeout._maximum == 1.0
91
92    retry, timeout = method_configs["Plain"]
93    assert retry is None
94    assert timeout._timeout == 30.0
95