From 90004e95d62db974a124d201b9205dea0cf7d6f2 Mon Sep 17 00:00:00 2001 From: Oleksandr Mazur Date: Wed, 11 Dec 2024 16:06:48 +0200 Subject: [PATCH] Tests: core: add timeout values for common clients Force redis, PSQL, kafka clients to have a strict timeout value for any requests. It's done to ensure our tests do not hang for an unknown period of time in case if something goes wrong. Signed-off-by: Oleksandr Mazur --- tests/metrics.py | 8 +++++--- utils/client_simulator/src/simulation_runner.py | 4 +++- utils/kafka_producer/src/producer.py | 6 +++++- utils/redis_client/redis_client.py | 5 ++++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/tests/metrics.py b/tests/metrics.py index 0a277b7..c0eab0f 100644 --- a/tests/metrics.py +++ b/tests/metrics.py @@ -6,12 +6,14 @@ def cgw_metric_get(host: str = "localhost", port: int = 8080) -> str: metrics = "" try: - r = requests.get(f"http://{host}:{port}/metrics") - print("CGW metrics: " + str(r.status_code) + ', txt:' + r.text) + # Try to fetch metrics with 5 seconds timeout value + r = requests.get(f"http://{host}:{port}/metrics", timeout=5) + print("CGW metrics ret code: " + str(r.status_code)) assert r is not None and r.status_code == 200, \ f"CGW metrics is not available" metrics = r.text - except: + except Exception as e: + print("CGW metrics: raised exception when tried to fetch metrics:" + e) raise Exception('CGW metrics fetch failed (Not running?)') return metrics diff --git a/utils/client_simulator/src/simulation_runner.py b/utils/client_simulator/src/simulation_runner.py index dbcac5e..8c06d2e 100644 --- a/utils/client_simulator/src/simulation_runner.py +++ b/utils/client_simulator/src/simulation_runner.py @@ -126,7 +126,9 @@ def handle_reboot(self, socket: client.ClientConnection, msg: dict): def connect(self): if self._socket is None: - self._socket = client.connect(self.server_addr, ssl=self.ssl_context, open_timeout=7200) + # 20 seconds is more then enough to establish conne and exchange + # them handshakes. + self._socket = client.connect(self.server_addr, ssl=self.ssl_context, open_timeout=20, close_timeout=20) return self._socket def disconnect(self): diff --git a/utils/kafka_producer/src/producer.py b/utils/kafka_producer/src/producer.py index 161f3ce..ff9c732 100644 --- a/utils/kafka_producer/src/producer.py +++ b/utils/kafka_producer/src/producer.py @@ -132,7 +132,11 @@ def __exit__(self, exc_type, exc_val, exc_tb): def connect(self) -> kafka.KafkaProducer: if self.is_connected() is False: - self.conn = kafka.KafkaProducer(bootstrap_servers=self.db, client_id="producer") + self.conn = kafka.KafkaProducer( + bootstrap_servers=self.db, + client_id="producer", + max_block_ms=12000, + request_timeout_ms=12000) logger.info("producer: connected to kafka") else: logger.info("producer: already connected to kafka") diff --git a/utils/redis_client/redis_client.py b/utils/redis_client/redis_client.py index f069d4e..7cad8e0 100644 --- a/utils/redis_client/redis_client.py +++ b/utils/redis_client/redis_client.py @@ -12,7 +12,10 @@ def connect(self): """Connect to the Redis database.""" try: # Establish connection to Redis server - self.connection = redis.StrictRedis(host=self.host, port=self.port, db=0, decode_responses=True) + self.connection = redis.StrictRedis( + host=self.host, port=self.port, + db=0, decode_responses=True, socket_timeout=5.0, + socket_connect_timeout=2.0) # Check if the connection is successful self.connection.ping() print(f"Connected to Redis server at {self.host}:{self.port}")