|
33 | 33 |
|
34 | 34 | __CROSS_SYNC_OUTPUT__ = "tests.unit.data._sync_autogen.test_metrics_interceptor" |
35 | 35 |
|
36 | | - |
37 | | - |
| 36 | +@CrossSync.convert(replace_symbols={"__aiter__": "__iter__"}) |
| 37 | +def _make_mock_stream_call(values, exc=None): |
| 38 | + """ |
| 39 | + Create a mock call object that can be used for streaming calls |
| 40 | + """ |
| 41 | + call = CrossSync.Mock() |
| 42 | + async def gen(): |
| 43 | + for val in values: |
| 44 | + yield val |
| 45 | + if exc: |
| 46 | + raise exc |
| 47 | + call.__aiter__ = mock.Mock(return_value=gen()) |
| 48 | + return call |
38 | 49 |
|
39 | 50 |
|
40 | 51 | @CrossSync.convert_class(sync_name="TestMetricsInterceptor") |
@@ -251,15 +262,8 @@ async def test_unary_stream_interceptor_success(self): |
251 | 262 | op.first_response_latency = None |
252 | 263 | instance.operation_map[op.uuid] = op |
253 | 264 |
|
254 | | - continuation = CrossSync.Mock() |
| 265 | + continuation = CrossSync.Mock(return_value=_make_mock_stream_call([1, 2])) |
255 | 266 | call = continuation.return_value |
256 | | - if CrossSync.is_async: |
257 | | - async def gen(): |
258 | | - yield 1 |
259 | | - yield 2 |
260 | | - call.__aiter__ = mock.Mock(return_value=gen()) |
261 | | - else: |
262 | | - call.__iter__ = mock.Mock(return_value=iter([1, 2])) |
263 | 267 | call.trailing_metadata = CrossSync.Mock(return_value=[("a", "b")]) |
264 | 268 | call.initial_metadata = CrossSync.Mock(return_value=[("c", "d")]) |
265 | 269 | details = mock.Mock() |
@@ -288,19 +292,8 @@ async def test_unary_stream_interceptor_failure_mid_stream(self): |
288 | 292 | op.first_response_latency = None |
289 | 293 | instance.operation_map[op.uuid] = op |
290 | 294 | exc = ValueError("test") |
291 | | - |
292 | | - continuation = CrossSync.Mock() |
| 295 | + continuation = CrossSync.Mock(return_value=_make_mock_stream_call([1], exc=exc)) |
293 | 296 | call = continuation.return_value |
294 | | - if CrossSync.is_async: |
295 | | - async def mock_generator(): |
296 | | - yield 1 |
297 | | - raise exc |
298 | | - call.__aiter__ = mock.Mock(return_value=mock_generator()) |
299 | | - else: |
300 | | - def mock_generator(): |
301 | | - yield 1 |
302 | | - raise exc |
303 | | - call.__iter__ = mock.Mock(return_value=mock_generator()) |
304 | 297 | call.trailing_metadata = CrossSync.Mock(return_value=[("a", "b")]) |
305 | 298 | call.initial_metadata = CrossSync.Mock(return_value=[("c", "d")]) |
306 | 299 | details = mock.Mock() |
@@ -445,15 +438,8 @@ async def test_unary_stream_interceptor_start_operation(self, initial_state): |
445 | 438 | op.state = initial_state |
446 | 439 | instance.operation_map[op.uuid] = op |
447 | 440 |
|
448 | | - continuation = CrossSync.Mock() |
| 441 | + continuation = CrossSync.Mock(return_value=_make_mock_stream_call([1, 2])) |
449 | 442 | call = continuation.return_value |
450 | | - if CrossSync.is_async: |
451 | | - async def gen(): |
452 | | - yield 1 |
453 | | - yield 2 |
454 | | - call.__aiter__ = mock.Mock(return_value=gen()) |
455 | | - else: |
456 | | - call.__iter__ = mock.Mock(return_value=iter([1, 2])) |
457 | 443 | call.trailing_metadata = CrossSync.Mock(return_value=[]) |
458 | 444 | call.initial_metadata = CrossSync.Mock(return_value=[]) |
459 | 445 | details = mock.Mock() |
|
0 commit comments