|
13 | 13 | BlockingConnectionPool, |
14 | 14 | MaintenanceState, |
15 | 15 | ) |
| 16 | +from redis.exceptions import ResponseError |
16 | 17 | from redis.maint_notifications import ( |
| 18 | + EndpointType, |
17 | 19 | MaintNotificationsConfig, |
18 | 20 | NodeMigratingNotification, |
19 | 21 | NodeMigratedNotification, |
@@ -201,6 +203,10 @@ def send(self, data): |
201 | 203 | if b"HELLO" in data: |
202 | 204 | response = b"%7\r\n$6\r\nserver\r\n$5\r\nredis\r\n$7\r\nversion\r\n$5\r\n7.0.0\r\n$5\r\nproto\r\n:3\r\n$2\r\nid\r\n:1\r\n$4\r\nmode\r\n$10\r\nstandalone\r\n$4\r\nrole\r\n$6\r\nmaster\r\n$7\r\nmodules\r\n*0\r\n" |
203 | 205 | self.pending_responses.append(response) |
| 206 | + elif b"MAINT_NOTIFICATIONS" in data and b"internal-ip" in data: |
| 207 | + # Simulate error response - activate it only for internal-ip tests |
| 208 | + response = b"+ERROR\r\n" |
| 209 | + self.pending_responses.append(response) |
204 | 210 | elif b"SET" in data: |
205 | 211 | response = b"+OK\r\n" |
206 | 212 |
|
@@ -337,8 +343,8 @@ def shutdown(self, how): |
337 | 343 | pass |
338 | 344 |
|
339 | 345 |
|
340 | | -class TestMaintenanceNotificationsHandlingSingleProxy: |
341 | | - """Integration tests for maintenance notifications handling with real connection pool.""" |
| 346 | +class TestMaintenanceNotificationsBase: |
| 347 | + """Base class for maintenance notifications handling tests.""" |
342 | 348 |
|
343 | 349 | def setup_method(self): |
344 | 350 | """Set up test fixtures with mocked sockets.""" |
@@ -393,7 +399,7 @@ def _get_client( |
393 | 399 | pool_class: The connection pool class (ConnectionPool or BlockingConnectionPool) |
394 | 400 | max_connections: Maximum number of connections in the pool (default: 10) |
395 | 401 | maint_notifications_config: Optional MaintNotificationsConfig to use. If not provided, |
396 | | - uses self.config from setup_method (default: None) |
| 402 | + uses self.config from setup_method (default: None) |
397 | 403 | setup_pool_handler: Whether to set up pool handler for moving notifications (default: False) |
398 | 404 |
|
399 | 405 | Returns: |
@@ -425,6 +431,71 @@ def _get_client( |
425 | 431 |
|
426 | 432 | return test_redis_client |
427 | 433 |
|
| 434 | + |
| 435 | +class TestMaintenanceNotificationsHandshake(TestMaintenanceNotificationsBase): |
| 436 | + """Integration tests for maintenance notifications handling with real connection pool.""" |
| 437 | + |
| 438 | + def test_handshake_success_when_enabled(self): |
| 439 | + """Test that handshake is performed correctly.""" |
| 440 | + maint_notifications_config = MaintNotificationsConfig( |
| 441 | + enabled=True, endpoint_type=EndpointType.EXTERNAL_IP |
| 442 | + ) |
| 443 | + test_redis_client = self._get_client( |
| 444 | + ConnectionPool, maint_notifications_config=maint_notifications_config |
| 445 | + ) |
| 446 | + |
| 447 | + try: |
| 448 | + # Perform Redis operations that should work with our improved mock responses |
| 449 | + result_set = test_redis_client.set("hello", "world") |
| 450 | + result_get = test_redis_client.get("hello") |
| 451 | + |
| 452 | + # Verify operations completed successfully |
| 453 | + assert result_set is True |
| 454 | + assert result_get == b"world" |
| 455 | + |
| 456 | + finally: |
| 457 | + test_redis_client.close() |
| 458 | + |
| 459 | + def test_handshake_success_when_auto_and_command_not_supported(self): |
| 460 | + """Test that when maintenance notifications are set to 'auto', the client gracefully handles unsupported MAINT_NOTIFICATIONS commands and normal Redis operations succeed.""" |
| 461 | + maint_notifications_config = MaintNotificationsConfig( |
| 462 | + enabled="auto", endpoint_type=EndpointType.INTERNAL_IP |
| 463 | + ) |
| 464 | + test_redis_client = self._get_client( |
| 465 | + ConnectionPool, maint_notifications_config=maint_notifications_config |
| 466 | + ) |
| 467 | + |
| 468 | + try: |
| 469 | + # Perform Redis operations that should work with our improved mock responses |
| 470 | + result_set = test_redis_client.set("hello", "world") |
| 471 | + result_get = test_redis_client.get("hello") |
| 472 | + |
| 473 | + # Verify operations completed successfully |
| 474 | + assert result_set is True |
| 475 | + assert result_get == b"world" |
| 476 | + |
| 477 | + finally: |
| 478 | + test_redis_client.close() |
| 479 | + |
| 480 | + def test_handshake_failure_when_enabled(self): |
| 481 | + """Test that handshake is performed correctly.""" |
| 482 | + maint_notifications_config = MaintNotificationsConfig( |
| 483 | + enabled=True, endpoint_type=EndpointType.INTERNAL_IP |
| 484 | + ) |
| 485 | + test_redis_client = self._get_client( |
| 486 | + ConnectionPool, maint_notifications_config=maint_notifications_config |
| 487 | + ) |
| 488 | + try: |
| 489 | + with pytest.raises(ResponseError): |
| 490 | + test_redis_client.set("hello", "world") |
| 491 | + |
| 492 | + finally: |
| 493 | + test_redis_client.close() |
| 494 | + |
| 495 | + |
| 496 | +class TestMaintenanceNotificationsHandlingSingleProxy(TestMaintenanceNotificationsBase): |
| 497 | + """Integration tests for maintenance notifications handling with real connection pool.""" |
| 498 | + |
428 | 499 | def _validate_connection_handlers(self, conn, pool_handler, config): |
429 | 500 | """Helper method to validate connection handlers are properly set.""" |
430 | 501 | # Test that the node moving handler function is correctly set |
@@ -1891,40 +1962,16 @@ def test_moving_migrating_migrated_moved_state_transitions(self, pool_class): |
1891 | 1962 | pool.disconnect() |
1892 | 1963 |
|
1893 | 1964 |
|
1894 | | -class TestMaintenanceNotificationsHandlingMultipleProxies: |
| 1965 | +class TestMaintenanceNotificationsHandlingMultipleProxies( |
| 1966 | + TestMaintenanceNotificationsBase |
| 1967 | +): |
1895 | 1968 | """Integration tests for maintenance notifications handling with real connection pool.""" |
1896 | 1969 |
|
1897 | 1970 | def setup_method(self): |
1898 | 1971 | """Set up test fixtures with mocked sockets.""" |
1899 | | - self.mock_sockets = [] |
1900 | | - self.original_socket = socket.socket |
| 1972 | + super().setup_method() |
1901 | 1973 | self.orig_host = "test.address.com" |
1902 | 1974 |
|
1903 | | - # Mock socket creation to return our mock sockets |
1904 | | - def mock_socket_factory(*args, **kwargs): |
1905 | | - mock_sock = MockSocket() |
1906 | | - self.mock_sockets.append(mock_sock) |
1907 | | - return mock_sock |
1908 | | - |
1909 | | - self.socket_patcher = patch("socket.socket", side_effect=mock_socket_factory) |
1910 | | - self.socket_patcher.start() |
1911 | | - |
1912 | | - # Mock select.select to simulate data availability for reading |
1913 | | - def mock_select(rlist, wlist, xlist, timeout=0): |
1914 | | - # Check if any of the sockets in rlist have data available |
1915 | | - ready_sockets = [] |
1916 | | - for sock in rlist: |
1917 | | - if hasattr(sock, "connected") and sock.connected and not sock.closed: |
1918 | | - # Only return socket as ready if it actually has data to read |
1919 | | - if hasattr(sock, "pending_responses") and sock.pending_responses: |
1920 | | - ready_sockets.append(sock) |
1921 | | - # Don't return socket as ready just because it received commands |
1922 | | - # Only when there are actual responses available |
1923 | | - return (ready_sockets, [], []) |
1924 | | - |
1925 | | - self.select_patcher = patch("select.select", side_effect=mock_select) |
1926 | | - self.select_patcher.start() |
1927 | | - |
1928 | 1975 | ips = ["1.2.3.4", "5.6.7.8", "9.10.11.12"] |
1929 | 1976 | ips = ips * 3 |
1930 | 1977 |
|
@@ -1952,15 +1999,9 @@ def mock_socket_getaddrinfo(host, port, family=0, type=0, proto=0, flags=0): |
1952 | 1999 | ) |
1953 | 2000 | self.getaddrinfo_patcher.start() |
1954 | 2001 |
|
1955 | | - # Create maintenance notifications config |
1956 | | - self.config = MaintNotificationsConfig( |
1957 | | - enabled=True, proactive_reconnect=True, relaxed_timeout=30 |
1958 | | - ) |
1959 | | - |
1960 | 2002 | def teardown_method(self): |
1961 | 2003 | """Clean up test fixtures.""" |
1962 | | - self.socket_patcher.stop() |
1963 | | - self.select_patcher.stop() |
| 2004 | + super().teardown_method() |
1964 | 2005 | self.getaddrinfo_patcher.stop() |
1965 | 2006 |
|
1966 | 2007 | @pytest.mark.parametrize("pool_class", [ConnectionPool, BlockingConnectionPool]) |
|
0 commit comments