Skip to content

Commit b489883

Browse files
committed
FIX: Fix live client test flakiness
1 parent fbc568f commit b489883

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

tests/test_live_client.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,9 @@ async def test_live_start(
358358
assert message.start_session
359359

360360

361-
def test_live_start_twice(
361+
async def test_live_start_twice(
362362
live_client: client.Live,
363+
mock_live_server: MockLiveServerInterface,
363364
) -> None:
364365
"""
365366
Test that calling start() twice raises a ValueError.
@@ -373,6 +374,10 @@ def test_live_start_twice(
373374
# Act
374375
live_client.start()
375376

377+
_ = await mock_live_server.wait_for_message_of_type(
378+
message_type=gateway.SessionStart,
379+
)
380+
376381
# Assert
377382
with pytest.raises(ValueError):
378383
live_client.start()
@@ -389,8 +394,9 @@ def test_live_start_before_subscribe(
389394
live_client.start()
390395

391396

392-
def test_live_iteration_after_start(
397+
async def test_live_iteration_after_start(
393398
live_client: client.Live,
399+
mock_live_server: MockLiveServerInterface,
394400
) -> None:
395401
"""
396402
Test that iterating the Live client after it is started raises a
@@ -405,13 +411,18 @@ def test_live_iteration_after_start(
405411
# Act
406412
live_client.start()
407413

414+
_ = await mock_live_server.wait_for_message_of_type(
415+
message_type=gateway.SessionStart,
416+
)
417+
408418
# Assert
409419
with pytest.raises(ValueError):
410420
iter(live_client)
411421

412422

413-
def test_live_async_iteration_after_start(
423+
async def test_live_async_iteration_after_start(
414424
live_client: client.Live,
425+
mock_live_server: MockLiveServerInterface,
415426
) -> None:
416427
"""
417428
Test that async-iterating the Live client after it is started raises a
@@ -426,6 +437,10 @@ def test_live_async_iteration_after_start(
426437
# Act
427438
live_client.start()
428439

440+
_ = await mock_live_server.wait_for_message_of_type(
441+
message_type=gateway.SessionStart,
442+
)
443+
429444
# Assert
430445
with pytest.raises(ValueError):
431446
live_client.__aiter__()
@@ -759,9 +774,10 @@ def test_live_block_for_close(
759774
assert not live_client.is_connected()
760775

761776

762-
def test_live_block_for_close_timeout(
777+
async def test_live_block_for_close_timeout(
763778
live_client: client.Live,
764779
monkeypatch: pytest.MonkeyPatch,
780+
mock_live_server: MockLiveServerInterface,
765781
) -> None:
766782
"""
767783
Test that block_for_close terminates the session when the timeout is
@@ -778,15 +794,20 @@ def test_live_block_for_close_timeout(
778794
)
779795

780796
# Act, Assert
797+
_ = await mock_live_server.wait_for_message_of_type(
798+
message_type=gateway.SubscriptionRequest,
799+
)
800+
781801
live_client.block_for_close(timeout=0)
782802
live_client.terminate.assert_called_once() # type: ignore
783803

784804

785805
@pytest.mark.usefixtures("mock_live_server")
786-
def test_live_block_for_close_timeout_stream(
806+
async def test_live_block_for_close_timeout_stream(
787807
live_client: client.Live,
788808
monkeypatch: pytest.MonkeyPatch,
789809
tmp_path: pathlib.Path,
810+
mock_live_server: MockLiveServerInterface,
790811
) -> None:
791812
"""
792813
Test that block_for_close flushes user streams on timeout.
@@ -805,6 +826,10 @@ def test_live_block_for_close_timeout_stream(
805826
live_client.add_stream(stream)
806827

807828
# Act, Assert
829+
_ = await mock_live_server.wait_for_message_of_type(
830+
message_type=gateway.SubscriptionRequest,
831+
)
832+
808833
live_client.block_for_close(timeout=0)
809834
stream.flush.assert_called() # type: ignore [attr-defined]
810835

@@ -837,6 +862,7 @@ async def test_live_wait_for_close(
837862
async def test_live_wait_for_close_timeout(
838863
live_client: client.Live,
839864
monkeypatch: pytest.MonkeyPatch,
865+
mock_live_server: MockLiveServerInterface,
840866
) -> None:
841867
"""
842868
Test that wait_for_close terminates the session when the timeout is
@@ -853,6 +879,11 @@ async def test_live_wait_for_close_timeout(
853879
symbols="ALL_SYMBOLS",
854880
start=None,
855881
)
882+
883+
_ = await mock_live_server.wait_for_message_of_type(
884+
message_type=gateway.SubscriptionRequest,
885+
)
886+
856887
await live_client.wait_for_close(timeout=0)
857888

858889
# Assert
@@ -864,6 +895,7 @@ async def test_live_wait_for_close_timeout_stream(
864895
live_client: client.Live,
865896
monkeypatch: pytest.MonkeyPatch,
866897
tmp_path: pathlib.Path,
898+
mock_live_server: MockLiveServerInterface,
867899
) -> None:
868900
"""
869901
Test that wait_for_close flushes user streams on timeout.
@@ -883,6 +915,10 @@ async def test_live_wait_for_close_timeout_stream(
883915
live_client.add_stream(stream)
884916

885917
# Act
918+
_ = await mock_live_server.wait_for_message_of_type(
919+
message_type=gateway.SubscriptionRequest,
920+
)
921+
886922
await live_client.wait_for_close(timeout=0)
887923

888924
# Assert

0 commit comments

Comments
 (0)