|
| 1 | +import json |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from durabletask.aio.internal.shared import get_grpc_aio_channel |
| 7 | + |
| 8 | +HOST_ADDRESS = 'localhost:50051' |
| 9 | + |
| 10 | + |
| 11 | +def _find_option(options, key): |
| 12 | + for k, v in options: |
| 13 | + if k == key: |
| 14 | + return v |
| 15 | + raise AssertionError(f'Option with key {key} not found in options: {options}') |
| 16 | + |
| 17 | + |
| 18 | +def test_aio_channel_passes_base_options_and_max_lengths(): |
| 19 | + base_options = [ |
| 20 | + ('grpc.max_send_message_length', 4321), |
| 21 | + ('grpc.max_receive_message_length', 8765), |
| 22 | + ('grpc.primary_user_agent', 'durabletask-aio-tests'), |
| 23 | + ] |
| 24 | + with patch('durabletask.aio.internal.shared.grpc_aio.insecure_channel') as mock_channel: |
| 25 | + get_grpc_aio_channel(HOST_ADDRESS, False, options=base_options) |
| 26 | + # Ensure called with options kwarg |
| 27 | + assert mock_channel.call_count == 1 |
| 28 | + args, kwargs = mock_channel.call_args |
| 29 | + assert args[0] == HOST_ADDRESS |
| 30 | + assert 'options' in kwargs |
| 31 | + opts = kwargs['options'] |
| 32 | + # Check our base options made it through |
| 33 | + assert ('grpc.max_send_message_length', 4321) in opts |
| 34 | + assert ('grpc.max_receive_message_length', 8765) in opts |
| 35 | + assert ('grpc.primary_user_agent', 'durabletask-aio-tests') in opts |
| 36 | + |
| 37 | + |
| 38 | +def test_aio_channel_merges_env_keepalive_and_retry(monkeypatch: pytest.MonkeyPatch): |
| 39 | + # retry grpc option |
| 40 | + # service_config ref => https://github.com/grpc/grpc-proto/blob/master/grpc/service_config/service_config.proto#L44 |
| 41 | + max_attempts = 4 |
| 42 | + initial_backoff_ms = 250 |
| 43 | + max_backoff_ms = 2000 |
| 44 | + backoff_multiplier = 1.5 |
| 45 | + codes = ['RESOURCE_EXHAUSTED'] |
| 46 | + service_config = { |
| 47 | + 'methodConfig': [ |
| 48 | + { |
| 49 | + 'name': [{'service': ''}], # match all services/methods |
| 50 | + 'retryPolicy': { |
| 51 | + 'maxAttempts': max_attempts, |
| 52 | + 'initialBackoff': f'{initial_backoff_ms / 1000.0}s', |
| 53 | + 'maxBackoff': f'{max_backoff_ms / 1000.0}s', |
| 54 | + 'backoffMultiplier': backoff_multiplier, |
| 55 | + 'retryableStatusCodes': codes, |
| 56 | + }, |
| 57 | + } |
| 58 | + ] |
| 59 | + } |
| 60 | + |
| 61 | + base_options = [('grpc.service_config', json.dumps(service_config))] |
| 62 | + |
| 63 | + with patch('durabletask.aio.internal.shared.grpc_aio.insecure_channel') as mock_channel: |
| 64 | + get_grpc_aio_channel(HOST_ADDRESS, False, options=base_options) |
| 65 | + |
| 66 | + args, kwargs = mock_channel.call_args |
| 67 | + assert args[0] == HOST_ADDRESS |
| 68 | + assert 'options' in kwargs |
| 69 | + opts = kwargs['options'] |
| 70 | + |
| 71 | + # Retry service config present and parses correctly |
| 72 | + svc_cfg_str = _find_option(opts, 'grpc.service_config') |
| 73 | + svc_cfg = json.loads(svc_cfg_str) |
| 74 | + assert 'methodConfig' in svc_cfg and isinstance(svc_cfg['methodConfig'], list) |
| 75 | + retry_policy = svc_cfg['methodConfig'][0]['retryPolicy'] |
| 76 | + assert retry_policy['maxAttempts'] == 4 |
| 77 | + assert retry_policy['initialBackoff'] == f'{250 / 1000.0}s' |
| 78 | + assert retry_policy['maxBackoff'] == f'{2000 / 1000.0}s' |
| 79 | + assert retry_policy['backoffMultiplier'] == 1.5 |
| 80 | + # Codes are upper-cased list |
| 81 | + assert 'RESOURCE_EXHAUSTED' in retry_policy['retryableStatusCodes'] |
| 82 | + |
| 83 | + |
| 84 | +def test_aio_secure_channel_receives_options_when_secure_true(): |
| 85 | + base_options = [('grpc.max_receive_message_length', 999999)] |
| 86 | + with ( |
| 87 | + patch('durabletask.aio.internal.shared.grpc_aio.secure_channel') as mock_channel, |
| 88 | + patch('grpc.ssl_channel_credentials') as mock_credentials, |
| 89 | + ): |
| 90 | + get_grpc_aio_channel(HOST_ADDRESS, True, options=base_options) |
| 91 | + args, kwargs = mock_channel.call_args |
| 92 | + assert args[0] == HOST_ADDRESS |
| 93 | + assert args[1] == mock_credentials.return_value |
| 94 | + assert ('grpc.max_receive_message_length', 999999) in kwargs.get('options', []) |
0 commit comments