Skip to content

Commit f260f70

Browse files
Merge pull request ClickHouse#78987 from dmitry-sles-novikov/fix_test_nats_no_connection_at_startup_1
Fixed flaky test test_nats_no_connection_at_startup_1
2 parents 69c9ecf + 1a8b053 commit f260f70

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

tests/integration/helpers/cluster.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,14 @@ def rabbitmq_debuginfo(rabbitmq_id, cookie):
348348

349349
async def check_nats_is_available(nats_port, ssl_ctx=None):
350350
nc = await nats_connect_ssl(
351-
nats_port, user="click", password="house", ssl_ctx=ssl_ctx
351+
nats_port, user="click", password="house", ssl_ctx=ssl_ctx, max_reconnect_attempts=1
352352
)
353353
available = nc.is_connected
354354
await nc.close()
355355
return available
356356

357357

358-
async def nats_connect_ssl(nats_port, user, password, ssl_ctx=None):
358+
async def nats_connect_ssl(nats_port, user, password, ssl_ctx=None, **connect_options):
359359
if not ssl_ctx:
360360
ssl_ctx = ssl.create_default_context()
361361
ssl_ctx.check_hostname = False
@@ -365,6 +365,7 @@ async def nats_connect_ssl(nats_port, user, password, ssl_ctx=None):
365365
user=user,
366366
password=password,
367367
tls=ssl_ctx,
368+
**connect_options
368369
)
369370
return nc
370371

tests/integration/test_storage_nats/test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import logging
44
import math
5+
import nats
56
import os.path as p
67
import random
78
import subprocess
@@ -46,6 +47,24 @@ def wait_nats_to_start(nats_port, ssl_ctx=None, timeout=180):
4647
except Exception as ex:
4748
logging.debug("Can't connect to NATS " + str(ex))
4849
time.sleep(0.5)
50+
51+
assert False, "NATS is unavailable"
52+
53+
# function to check if nats is paused, because in some cases we successfully connected to it after calling pause_container
54+
def wait_nats_paused(nats_port, ssl_ctx=None, timeout=180):
55+
start = time.time()
56+
while time.time() - start < timeout:
57+
try:
58+
asyncio.run(check_nats_is_available(nats_port, ssl_ctx=ssl_ctx))
59+
time.sleep(0.5)
60+
except nats.errors.NoServersError:
61+
logging.debug("NATS is paused")
62+
return
63+
except Exception as ex:
64+
logging.warning("Detect NATS status failed with error \"" + str(ex) + "\" - continue waiting for proper status...")
65+
time.sleep(0.5)
66+
67+
assert False, "NATS is not paused"
4968

5069
def nats_check_query_result(query, time_limit_sec = 60):
5170
query_result = ""
@@ -1326,6 +1345,7 @@ def test_nats_restore_failed_connection_without_losses_on_write(nats_cluster):
13261345

13271346
def test_nats_no_connection_at_startup_1(nats_cluster):
13281347
with nats_cluster.pause_container("nats1"):
1348+
wait_nats_paused(nats_cluster.nats_port, nats_cluster.nats_ssl_context)
13291349
instance.query_and_get_error(
13301350
"""
13311351
CREATE TABLE test.cs (key UInt64, value UInt64)
@@ -1369,6 +1389,7 @@ def test_nats_no_connection_at_startup_2(nats_cluster):
13691389
"""
13701390
)
13711391
with nats_cluster.pause_container("nats1"):
1392+
wait_nats_paused(nats_cluster.nats_port, nats_cluster.nats_ssl_context)
13721393
instance.query("ATTACH TABLE test.cs")
13731394

13741395
wait_for_table_is_ready(instance, "test.cs")

0 commit comments

Comments
 (0)