ClickHouse/tests/integration/test_server_keep_alive/test.py

56 lines
1.7 KiB
Python
Raw Normal View History

2024-03-22 17:24:25 +00:00
import logging
import pytest
2024-08-07 11:37:39 +00:00
import random
2024-03-22 17:24:25 +00:00
import requests
from helpers.cluster import ClickHouseCluster
cluster = ClickHouseCluster(__file__)
node = cluster.add_instance("node", main_configs=["configs/keep_alive_settings.xml"])
@pytest.fixture(scope="module")
def start_cluster():
try:
logging.info("Starting cluster...")
cluster.start()
logging.info("Cluster started")
yield cluster
finally:
cluster.shutdown()
2024-03-22 17:41:53 +00:00
def test_max_keep_alive_requests_on_user_side(start_cluster):
2024-03-22 17:24:25 +00:00
# In this test we have `keep_alive_timeout` set to one hour to never trigger connection reset by timeout, `max_keep_alive_requests` is set to 5.
# We expect server to close connection after each 5 requests. We detect connection reset by change in src port.
# So the first 5 requests should come from the same port, the following 5 requests should come from another port.
2024-08-07 11:37:39 +00:00
log_comments = []
for _ in range(10):
rand_id = random.randint(0, 1000000)
log_comment = f"test_requests_with_keep_alive_{rand_id}"
log_comments.append(log_comment)
log_comments = sorted(log_comments)
2024-03-22 17:24:25 +00:00
session = requests.Session()
for i in range(10):
session.get(
2024-08-07 11:37:39 +00:00
f"http://{node.ip_address}:8123/?query=select%201&log_comment={log_comments[i]}"
2024-03-22 17:24:25 +00:00
)
ports = node.query(
2024-08-07 11:37:39 +00:00
f"""
2024-03-22 17:24:25 +00:00
SYSTEM FLUSH LOGS;
SELECT port
FROM system.query_log
2024-08-07 11:37:39 +00:00
WHERE log_comment IN ({", ".join(f"'{comment}'" for comment in log_comments)}) AND type = 'QueryFinish'
2024-03-22 17:24:25 +00:00
ORDER BY log_comment
"""
).split("\n")[:-1]
expected = 5 * [ports[0]] + [ports[5]] * 5
assert ports == expected