1414
1515from __future__ import annotations
1616
17+ import asyncio
18+ import threading
1719
1820import pytest
1921
2426async def test_audio_source_aclose_releases_waiter_and_cancels_timer () -> None :
2527 source = rtc .AudioSource (48000 , 1 )
2628 frame = rtc .AudioFrame .create (48000 , 1 , 48000 )
27- await source .capture_frame (frame )
29+ await asyncio . wait_for ( source .capture_frame (frame ), timeout = 5.0 )
2830
2931 assert source ._join_fut is not None
3032 assert source ._join_handle is not None
3133
32- await source .aclose ()
34+ await asyncio . wait_for ( source .aclose (), timeout = 5.0 )
3335
3436 assert source ._ffi_handle .disposed
3537 assert source ._join_fut is None
3638 assert source ._join_handle is None
3739
3840 # After aclose, wait_for_playout should return immediately without hanging
39- await source .wait_for_playout ()
41+ await asyncio . wait_for ( source .wait_for_playout (), timeout = 2.0 )
4042
4143 # Repeated calls are idempotent
42- await source .aclose ()
44+ await asyncio . wait_for ( source .aclose (), timeout = 2.0 )
4345
4446
4547def test_audio_source_sync_close () -> None :
@@ -53,14 +55,25 @@ def test_audio_source_sync_close() -> None:
5355 source .close ()
5456
5557
58+ def test_audio_source_cross_thread_close () -> None :
59+ source = rtc .AudioSource (48000 , 1 )
60+ assert not source ._ffi_handle .disposed
61+
62+ t = threading .Thread (target = source .close )
63+ t .start ()
64+ t .join ()
65+
66+ assert source ._ffi_handle .disposed
67+
68+
5669@pytest .mark .asyncio
5770async def test_audio_source_capture_after_close_is_noop () -> None :
5871 source = rtc .AudioSource (48000 , 1 )
5972 source .close ()
6073
6174 frame = rtc .AudioFrame .create (48000 , 1 , 480 )
6275 # Should safely return without error
63- await source .capture_frame (frame )
76+ await asyncio . wait_for ( source .capture_frame (frame ), timeout = 2.0 )
6477
6578
6679def test_video_source_sync_close () -> None :
@@ -79,7 +92,7 @@ async def test_video_source_aclose_and_capture_after_close() -> None:
7992 source = rtc .VideoSource (640 , 480 )
8093 assert not source ._ffi_handle .disposed
8194
82- await source .aclose ()
95+ await asyncio . wait_for ( source .aclose (), timeout = 5.0 )
8396 assert source ._ffi_handle .disposed
8497
8598 # Capture after close is safely ignored
@@ -92,4 +105,4 @@ async def test_video_source_aclose_and_capture_after_close() -> None:
92105 source .capture_frame (frame )
93106
94107 # Repeated aclose is idempotent
95- await source .aclose ()
108+ await asyncio . wait_for ( source .aclose (), timeout = 2.0 )
0 commit comments