|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import pytest |
| 3 | +from unittest.mock import MagicMock, patch, AsyncMock |
| 4 | +from ydb import driver, connection |
| 5 | +from ydb.aio import pool, _utilities |
| 6 | + |
| 7 | + |
| 8 | +class MockEndpointInfo: |
| 9 | + def __init__(self, address, port, location): |
| 10 | + self.address = address |
| 11 | + self.port = port |
| 12 | + self.endpoint = f"{address}:{port}" |
| 13 | + self.location = location |
| 14 | + self.ssl = False |
| 15 | + self.node_id = 1 |
| 16 | + |
| 17 | + def endpoints_with_options(self): |
| 18 | + yield (self.endpoint, connection.EndpointOptions(ssl_target_name_override=None, node_id=self.node_id)) |
| 19 | + |
| 20 | + |
| 21 | +class MockDiscoveryResult: |
| 22 | + def __init__(self, self_location, endpoints): |
| 23 | + self.self_location = self_location |
| 24 | + self.endpoints = endpoints |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.asyncio |
| 28 | +async def test_detect_local_dc_overrides_server_location(): |
| 29 | + """Test that detected location overrides server's self_location for preferred endpoints.""" |
| 30 | + # Server reports dc1, but we detect dc2 as nearest |
| 31 | + endpoints = [ |
| 32 | + MockEndpointInfo("dc1-host", 2135, "dc1"), |
| 33 | + MockEndpointInfo("dc2-host", 2135, "dc2"), |
| 34 | + ] |
| 35 | + mock_result = MockDiscoveryResult(self_location="dc1", endpoints=endpoints) |
| 36 | + |
| 37 | + mock_resolver = MagicMock() |
| 38 | + mock_resolver.resolve = AsyncMock(return_value=mock_result) |
| 39 | + |
| 40 | + preferred = [] |
| 41 | + |
| 42 | + def mock_init(self, endpoint, driver_config, endpoint_options=None): |
| 43 | + self.endpoint = endpoint |
| 44 | + self.node_id = 1 |
| 45 | + |
| 46 | + with patch.object(_utilities, "detect_local_dc", AsyncMock(return_value="dc2")): |
| 47 | + with patch("ydb.aio.connection.Connection.__init__", mock_init): |
| 48 | + with patch("ydb.aio.connection.Connection.connection_ready", AsyncMock()): |
| 49 | + with patch("ydb.aio.connection.Connection.close", AsyncMock()): |
| 50 | + with patch("ydb.aio.connection.Connection.add_cleanup_callback", lambda *a: None): |
| 51 | + config = driver.DriverConfig( |
| 52 | + endpoint="grpc://test:2135", database="/local", detect_local_dc=True, use_all_nodes=False |
| 53 | + ) |
| 54 | + discovery = pool.Discovery( |
| 55 | + store=pool.ConnectionsCache(config.use_all_nodes), driver_config=config |
| 56 | + ) |
| 57 | + discovery._resolver = mock_resolver |
| 58 | + |
| 59 | + original_add = discovery._cache.add |
| 60 | + discovery._cache.add = lambda conn, pref=False: ( |
| 61 | + preferred.append(conn.endpoint) if pref else None, |
| 62 | + original_add(conn, pref), |
| 63 | + )[1] |
| 64 | + |
| 65 | + await discovery.execute_discovery() |
| 66 | + |
| 67 | + assert any("dc2" in ep for ep in preferred), "dc2 should be preferred (detected)" |
| 68 | + assert not any("dc1" in ep for ep in preferred), "dc1 should not be preferred" |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.asyncio |
| 72 | +async def test_detect_local_dc_failure_fallback(): |
| 73 | + """Test that detection failure falls back to server's self_location.""" |
| 74 | + endpoints = [ |
| 75 | + MockEndpointInfo("dc1-host", 2135, "dc1"), |
| 76 | + MockEndpointInfo("dc2-host", 2135, "dc2"), |
| 77 | + ] |
| 78 | + mock_result = MockDiscoveryResult(self_location="dc1", endpoints=endpoints) |
| 79 | + |
| 80 | + mock_resolver = MagicMock() |
| 81 | + mock_resolver.resolve = AsyncMock(return_value=mock_result) |
| 82 | + |
| 83 | + preferred = [] |
| 84 | + |
| 85 | + def mock_init(self, endpoint, driver_config, endpoint_options=None): |
| 86 | + self.endpoint = endpoint |
| 87 | + self.node_id = 1 |
| 88 | + |
| 89 | + with patch.object(_utilities, "detect_local_dc", AsyncMock(return_value=None)): |
| 90 | + with patch("ydb.aio.connection.Connection.__init__", mock_init): |
| 91 | + with patch("ydb.aio.connection.Connection.connection_ready", AsyncMock()): |
| 92 | + with patch("ydb.aio.connection.Connection.close", AsyncMock()): |
| 93 | + with patch("ydb.aio.connection.Connection.add_cleanup_callback", lambda *a: None): |
| 94 | + config = driver.DriverConfig( |
| 95 | + endpoint="grpc://test:2135", database="/local", detect_local_dc=True, use_all_nodes=False |
| 96 | + ) |
| 97 | + discovery = pool.Discovery( |
| 98 | + store=pool.ConnectionsCache(config.use_all_nodes), driver_config=config |
| 99 | + ) |
| 100 | + discovery._resolver = mock_resolver |
| 101 | + |
| 102 | + original_add = discovery._cache.add |
| 103 | + discovery._cache.add = lambda conn, pref=False: ( |
| 104 | + preferred.append(conn.endpoint) if pref else None, |
| 105 | + original_add(conn, pref), |
| 106 | + )[1] |
| 107 | + |
| 108 | + await discovery.execute_discovery() |
| 109 | + |
| 110 | + assert any("dc1" in ep for ep in preferred), "dc1 should be preferred (server fallback)" |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.asyncio |
| 114 | +async def test_detect_local_dc_skipped_when_use_all_nodes_true(): |
| 115 | + """Test that detect_local_dc is NOT called when use_all_nodes=True.""" |
| 116 | + endpoints = [ |
| 117 | + MockEndpointInfo("dc1-host", 2135, "dc1"), |
| 118 | + MockEndpointInfo("dc2-host", 2135, "dc2"), |
| 119 | + ] |
| 120 | + mock_result = MockDiscoveryResult(self_location="dc1", endpoints=endpoints) |
| 121 | + |
| 122 | + mock_resolver = MagicMock() |
| 123 | + mock_resolver.resolve = AsyncMock(return_value=mock_result) |
| 124 | + |
| 125 | + def mock_init(self, endpoint, driver_config, endpoint_options=None): |
| 126 | + self.endpoint = endpoint |
| 127 | + self.node_id = 1 |
| 128 | + |
| 129 | + with patch.object(_utilities, "detect_local_dc", AsyncMock(return_value="dc2")) as detect_mock: |
| 130 | + with patch("ydb.aio.connection.Connection.__init__", mock_init): |
| 131 | + with patch("ydb.aio.connection.Connection.connection_ready", AsyncMock()): |
| 132 | + with patch("ydb.aio.connection.Connection.close", AsyncMock()): |
| 133 | + with patch("ydb.aio.connection.Connection.add_cleanup_callback", lambda *a: None): |
| 134 | + config = driver.DriverConfig( |
| 135 | + endpoint="grpc://test:2135", database="/local", detect_local_dc=True, use_all_nodes=True |
| 136 | + ) |
| 137 | + discovery = pool.Discovery( |
| 138 | + store=pool.ConnectionsCache(config.use_all_nodes), driver_config=config |
| 139 | + ) |
| 140 | + discovery._resolver = mock_resolver |
| 141 | + await discovery.execute_discovery() |
| 142 | + |
| 143 | + assert detect_mock.call_count == 0, "detect_local_dc should NOT be called when use_all_nodes=True" |
0 commit comments