Skip to content

Commit

Permalink
Tests: core: add timeout values for common clients
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Cahb committed Dec 11, 2024
1 parent b2867ac commit 90004e9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
8 changes: 5 additions & 3 deletions tests/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion utils/client_simulator/src/simulation_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 5 additions & 1 deletion utils/kafka_producer/src/producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 4 additions & 1 deletion utils/redis_client/redis_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down

0 comments on commit 90004e9

Please sign in to comment.