Skip to content

Commit 71eb800

Browse files
committed
Add test for server-initiated PING handling
Signed-off-by: Casper Beyer <casper@synadia.com>
1 parent 75ce2f9 commit 71eb800

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# NATS server config for testing server-initiated PINGs
2+
# Set very short ping interval so server pings client quickly
3+
ping_interval: "100ms"
4+
ping_max: 3

nats-client/tests/test_client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,3 +709,28 @@ async def test_inbox_prefix_cannot_end_with_dot(server):
709709
"""Test that inbox prefix ending with '.' is rejected."""
710710
with pytest.raises(ValueError, match="inbox_prefix cannot end with '.'"):
711711
await connect(server.client_url, inbox_prefix="test.", timeout=1.0)
712+
713+
714+
@pytest.mark.asyncio
715+
async def test_server_initiated_ping_pong():
716+
"""Test that client properly handles PING from server and responds with PONG."""
717+
import os
718+
719+
# Start server with very short ping interval
720+
config_path = os.path.join(os.path.dirname(__file__), "configs", "server_ping.conf")
721+
server = await run(config_path=config_path, port=0, timeout=5.0)
722+
723+
try:
724+
client = await connect(server.client_url, timeout=1.0, allow_reconnect=False)
725+
726+
try:
727+
# Wait long enough for server to send at least one PING
728+
# Server is configured to ping every 100ms
729+
await asyncio.sleep(0.3)
730+
731+
# If ping/pong handling didn't work, client would be disconnected
732+
assert client.status == ClientStatus.CONNECTED, "Client should still be connected after server PINGs"
733+
finally:
734+
await client.close()
735+
finally:
736+
await server.shutdown()

0 commit comments

Comments
 (0)