|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import os |
| 4 | +from typing import Optional |
| 5 | +from urllib.parse import urlparse |
| 6 | +import pytest |
| 7 | + |
| 8 | +from redis.backoff import ExponentialWithJitterBackoff, NoBackoff |
| 9 | +from redis.client import Redis |
| 10 | +from redis.maintenance_events import EndpointType, MaintenanceEventsConfig |
| 11 | +from redis.retry import Retry |
| 12 | +from tests.test_scenario.fault_injector_client import FaultInjectorClient |
| 13 | + |
| 14 | +RELAX_TIMEOUT = 30 |
| 15 | +CLIENT_TIMEOUT = 5 |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture() |
| 19 | +def endpoint_name(request): |
| 20 | + return request.config.getoption("--endpoint-name") or os.getenv( |
| 21 | + "REDIS_ENDPOINT_NAME", "m-standard" |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture() |
| 26 | +def endpoints_config(endpoint_name: str): |
| 27 | + endpoints_config = os.getenv("REDIS_ENDPOINTS_CONFIG_PATH", None) |
| 28 | + |
| 29 | + if not (endpoints_config and os.path.exists(endpoints_config)): |
| 30 | + raise FileNotFoundError(f"Endpoints config file not found: {endpoints_config}") |
| 31 | + |
| 32 | + try: |
| 33 | + with open(endpoints_config, "r") as f: |
| 34 | + data = json.load(f) |
| 35 | + db = data[endpoint_name] |
| 36 | + return db |
| 37 | + except Exception as e: |
| 38 | + raise ValueError( |
| 39 | + f"Failed to load endpoints config file: {endpoints_config}" |
| 40 | + ) from e |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture() |
| 44 | +def fault_injector_client(): |
| 45 | + url = os.getenv("FAULT_INJECTION_API_URL", "http://127.0.0.1:20324") |
| 46 | + return FaultInjectorClient(url) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture() |
| 50 | +def client_maint_events(endpoints_config): |
| 51 | + return _get_client_maint_events(endpoints_config) |
| 52 | + |
| 53 | + |
| 54 | +def _get_client_maint_events( |
| 55 | + endpoints_config, |
| 56 | + enable_maintenance_events: bool = True, |
| 57 | + endpoint_type: Optional[EndpointType] = None, |
| 58 | + enable_relax_timeout: bool = True, |
| 59 | + enable_proactive_reconnect: bool = True, |
| 60 | + disable_retries: bool = False, |
| 61 | + socket_timeout: Optional[float] = None, |
| 62 | +): |
| 63 | + """Create Redis client with maintenance events enabled.""" |
| 64 | + |
| 65 | + # Get credentials from the configuration |
| 66 | + username = endpoints_config.get("username") |
| 67 | + password = endpoints_config.get("password") |
| 68 | + |
| 69 | + # Parse host and port from endpoints URL |
| 70 | + endpoints = endpoints_config.get("endpoints", []) |
| 71 | + if not endpoints: |
| 72 | + raise ValueError("No endpoints found in configuration") |
| 73 | + |
| 74 | + parsed = urlparse(endpoints[0]) |
| 75 | + host = parsed.hostname |
| 76 | + port = parsed.port |
| 77 | + |
| 78 | + if not host: |
| 79 | + raise ValueError(f"Could not parse host from endpoint URL: {endpoints[0]}") |
| 80 | + |
| 81 | + logging.info(f"Connecting to Redis Enterprise: {host}:{port} with user: {username}") |
| 82 | + |
| 83 | + # Configure maintenance events |
| 84 | + maintenance_config = MaintenanceEventsConfig( |
| 85 | + enabled=enable_maintenance_events, |
| 86 | + proactive_reconnect=enable_proactive_reconnect, |
| 87 | + relax_timeout=RELAX_TIMEOUT if enable_relax_timeout else -1, |
| 88 | + endpoint_type=endpoint_type, |
| 89 | + ) |
| 90 | + |
| 91 | + # Create Redis client with maintenance events config |
| 92 | + # This will automatically create the MaintenanceEventPoolHandler |
| 93 | + if disable_retries: |
| 94 | + retry = Retry(NoBackoff(), 0) |
| 95 | + else: |
| 96 | + retry = Retry(backoff=ExponentialWithJitterBackoff(base=1, cap=10), retries=3) |
| 97 | + |
| 98 | + client = Redis( |
| 99 | + host=host, |
| 100 | + port=port, |
| 101 | + socket_timeout=CLIENT_TIMEOUT if socket_timeout is None else socket_timeout, |
| 102 | + username=username, |
| 103 | + password=password, |
| 104 | + protocol=3, # RESP3 required for push notifications |
| 105 | + maintenance_events_config=maintenance_config, |
| 106 | + retry=retry, |
| 107 | + ) |
| 108 | + logging.info("Redis client created with maintenance events enabled") |
| 109 | + logging.info(f"Client uses Protocol: {client.connection_pool.get_protocol()}") |
| 110 | + maintenance_handler_exists = client.maintenance_events_pool_handler is not None |
| 111 | + logging.info(f"Maintenance events pool handler: {maintenance_handler_exists}") |
| 112 | + |
| 113 | + return client |
0 commit comments