-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_method.py
More file actions
334 lines (259 loc) · 11 KB
/
Copy pathtest_method.py
File metadata and controls
334 lines (259 loc) · 11 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
# 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
from unittest import mock
import pytest
try:
import grpc # noqa: F401
except ImportError:
pytest.skip("No GRPC", allow_module_level=True)
import google.api_core.gapic_v1.client_info
import google.api_core.gapic_v1.method
import google.api_core.page_iterator
from google.api_core import exceptions, retry, timeout
def _utcnow_monotonic():
curr_value = datetime.datetime.min
delta = datetime.timedelta(seconds=0.5)
while True:
yield curr_value
curr_value += delta
def test_wrap_method_basic():
method = mock.Mock(spec=["__call__"], return_value=42)
wrapped_method = google.api_core.gapic_v1.method.wrap_method(method)
result = wrapped_method(1, 2, meep="moop")
assert result == 42
method.assert_called_once_with(1, 2, meep="moop", metadata=mock.ANY)
# Check that the default client info was specified in the metadata.
metadata = method.call_args[1]["metadata"]
assert len(metadata) == 1
client_info = google.api_core.gapic_v1.client_info.DEFAULT_CLIENT_INFO
user_agent_metadata = client_info.to_grpc_metadata()
assert user_agent_metadata in metadata
def test_wrap_method_with_no_client_info():
method = mock.Mock(spec=["__call__"])
wrapped_method = google.api_core.gapic_v1.method.wrap_method(
method, client_info=None
)
wrapped_method(1, 2, meep="moop")
method.assert_called_once_with(1, 2, meep="moop")
def test_wrap_method_with_custom_client_info():
client_info = google.api_core.gapic_v1.client_info.ClientInfo(
python_version=1,
grpc_version=2,
api_core_version=3,
gapic_version=4,
client_library_version=5,
protobuf_runtime_version=6,
)
method = mock.Mock(spec=["__call__"])
wrapped_method = google.api_core.gapic_v1.method.wrap_method(
method, client_info=client_info
)
wrapped_method(1, 2, meep="moop")
method.assert_called_once_with(1, 2, meep="moop", metadata=mock.ANY)
# Check that the custom client info was specified in the metadata.
metadata = method.call_args[1]["metadata"]
assert client_info.to_grpc_metadata() in metadata
def test_invoke_wrapped_method_with_metadata():
method = mock.Mock(spec=["__call__"])
wrapped_method = google.api_core.gapic_v1.method.wrap_method(method)
wrapped_method(mock.sentinel.request, metadata=[("a", "b")])
method.assert_called_once_with(mock.sentinel.request, metadata=mock.ANY)
metadata = method.call_args[1]["metadata"]
# Metadata should have two items: the client info metadata and our custom
# metadata.
assert len(metadata) == 2
assert ("a", "b") in metadata
def test_invoke_wrapped_method_with_metadata_as_none():
method = mock.Mock(spec=["__call__"])
wrapped_method = google.api_core.gapic_v1.method.wrap_method(method)
wrapped_method(mock.sentinel.request, metadata=None)
method.assert_called_once_with(mock.sentinel.request, metadata=mock.ANY)
metadata = method.call_args[1]["metadata"]
# Metadata should have just one items: the client info metadata.
assert len(metadata) == 1
def test_extract_metrics_header_duplicate_tokens():
metadata = [
("x-goog-api-client", "token1 token2"),
("x-goog-api-client", "token2 token3 token1"),
("other-header", "value"),
("x-goog-api-client", "token4 token2"),
]
metric_str, arbitrary_metadata = (
google.api_core.gapic_v1.method._extract_metrics_header(metadata)
)
# Should maintain order of first appearance and eliminate duplicates
assert metric_str == "token1 token2 token3 token4"
assert arbitrary_metadata == [("other-header", "value")]
def test_invoke_wrapped_method_with_duplicate_x_goog_api_client_metadata():
method = mock.Mock(spec=["__call__"])
# Create a custom ClientInfo with defined properties so we know exactly what is returned
client_info = google.api_core.gapic_v1.client_info.ClientInfo(
user_agent="custom-user-agent/1.0",
python_version="3.14.0",
grpc_version="1.76.0",
api_core_version="2.29.0",
)
wrapped_method = google.api_core.gapic_v1.method.wrap_method(
method, client_info=client_info
)
# Invoke the wrapped method with an explicit user-provided custom header that contains duplicates
# both within its own items and overlapping with the default client_info
wrapped_method(
mock.sentinel.request,
metadata=[
("x-goog-api-client", "override-client/2.0"),
(
"x-goog-api-client",
"override-client/2.0 grpc/1.76.0 custom-user-agent/1.0",
),
("other-header", "value"),
],
)
method.assert_called_once_with(mock.sentinel.request, metadata=mock.ANY)
metadata = method.call_args[1]["metadata"]
# There should only be one "x-goog-api-client" header, containing both values joined by space,
# plus the other-header.
assert len(metadata) == 2
metadata_dict = dict(metadata)
assert "other-header" in metadata_dict
assert metadata_dict["other-header"] == "value"
assert "x-goog-api-client" in metadata_dict
# Verify both the user-provided override value and the library system telemetry are merged explicitly
assert (
metadata_dict["x-goog-api-client"]
== "custom-user-agent/1.0 gl-python/3.14.0 grpc/1.76.0 gax/2.29.0 override-client/2.0"
)
@mock.patch("time.sleep")
def test_wrap_method_with_default_retry_and_timeout_and_compression(unused_sleep):
method = mock.Mock(
spec=["__call__"], side_effect=[exceptions.InternalServerError(None), 42]
)
default_retry = retry.Retry()
default_timeout = timeout.ConstantTimeout(60)
default_compression = grpc.Compression.Gzip
wrapped_method = google.api_core.gapic_v1.method.wrap_method(
method, default_retry, default_timeout, default_compression
)
result = wrapped_method()
assert result == 42
assert method.call_count == 2
method.assert_called_with(
timeout=60, compression=default_compression, metadata=mock.ANY
)
@mock.patch("time.sleep")
def test_wrap_method_with_default_retry_and_timeout_using_sentinel(unused_sleep):
method = mock.Mock(
spec=["__call__"], side_effect=[exceptions.InternalServerError(None), 42]
)
default_retry = retry.Retry()
default_timeout = timeout.ConstantTimeout(60)
default_compression = grpc.Compression.Gzip
wrapped_method = google.api_core.gapic_v1.method.wrap_method(
method, default_retry, default_timeout, default_compression
)
result = wrapped_method(
retry=google.api_core.gapic_v1.method.DEFAULT,
timeout=google.api_core.gapic_v1.method.DEFAULT,
compression=google.api_core.gapic_v1.method.DEFAULT,
)
assert result == 42
assert method.call_count == 2
method.assert_called_with(
timeout=60, compression=default_compression, metadata=mock.ANY
)
@mock.patch("time.sleep")
def test_wrap_method_with_overriding_retry_timeout_compression(unused_sleep):
method = mock.Mock(spec=["__call__"], side_effect=[exceptions.NotFound(None), 42])
default_retry = retry.Retry()
default_timeout = timeout.ConstantTimeout(60)
default_compression = grpc.Compression.Gzip
wrapped_method = google.api_core.gapic_v1.method.wrap_method(
method, default_retry, default_timeout, default_compression
)
result = wrapped_method(
retry=retry.Retry(retry.if_exception_type(exceptions.NotFound)),
timeout=timeout.ConstantTimeout(22),
compression=grpc.Compression.Deflate,
)
assert result == 42
assert method.call_count == 2
method.assert_called_with(
timeout=22,
compression=grpc.Compression.Deflate,
metadata=mock.ANY,
)
@pytest.mark.skip(reason="Known flaky due to floating point comparison. #866")
def test_wrap_method_with_overriding_timeout_as_a_number():
method = mock.Mock(spec=["__call__"], return_value=42)
default_retry = retry.Retry()
default_timeout = timeout.ConstantTimeout(60)
wrapped_method = google.api_core.gapic_v1.method.wrap_method(
method, default_retry, default_timeout
)
# Using "result = wrapped_method(timeout=22)" fails since wrapped_method
# does floating point calculations that results in 21.987.. instead of 22
result = wrapped_method(timeout=22)
assert result == 42
actual_timeout = method.call_args[1]["timeout"]
metadata = method.call_args[1]["metadata"]
assert metadata == mock.ANY
assert actual_timeout == pytest.approx(22, abs=0.01)
def test_wrap_method_with_overriding_constant_timeout():
method = mock.Mock(spec=["__call__"], return_value=42)
default_retry = retry.Retry()
default_timeout = timeout.ConstantTimeout(60)
wrapped_method = google.api_core.gapic_v1.method.wrap_method(
method, default_retry, default_timeout
)
result = wrapped_method(timeout=timeout.ConstantTimeout(22))
assert result == 42
actual_timeout = method.call_args[1]["timeout"]
metadata = method.call_args[1]["metadata"]
assert metadata == mock.ANY
assert actual_timeout == 22
def test_wrap_method_with_call():
method = mock.Mock()
mock_call = mock.Mock()
method.with_call.return_value = 42, mock_call
wrapped_method = google.api_core.gapic_v1.method.wrap_method(method, with_call=True)
result = wrapped_method()
assert len(result) == 2
assert result[0] == 42
assert result[1] == mock_call
def test_wrap_method_with_call_not_supported():
"""Raises an error if wrapped callable doesn't have with_call method."""
method = lambda: None # noqa: E731
with pytest.raises(ValueError) as exc_info:
google.api_core.gapic_v1.method.wrap_method(method, with_call=True)
assert "with_call=True is only supported for unary calls" in str(exc_info.value)
@pytest.mark.parametrize(
"headers,expected",
[
((), ""),
(("",), ""),
((None,), ""),
(("", None, ""), ""),
(("token1",), "token1"),
(("token1 token1",), "token1"),
(("token1", "token1"), "token1"),
(("token1 token2 token1",), "token1 token2"),
(("token1", "token2", "token1"), "token1 token2"),
(("token1 token2", "token2 token3"), "token1 token2 token3"),
(("token1", None, "token2", "", "token1"), "token1 token2"),
],
)
def test__deduplicate_metadata_tokens(headers, expected):
dedup = google.api_core.gapic_v1.method._deduplicate_metadata_tokens
assert dedup(*headers) == expected