@@ -78,15 +78,23 @@ def mock_pretrained_config():
7878 yield
7979
8080
81- class AwaitableMock (AsyncMock ):
81+ class AwaitableTask (AsyncMock ):
82+ """Mock that can be awaited and acts like an asyncio.Task"""
83+
8284 def __await__ (self ):
83- return iter ([])
85+ # Return a completed future-like object
86+ async def _await ():
87+ return None
88+
89+ return _await ().__await__ ()
8490
8591
8692@pytest .fixture (autouse = True )
8793def mock_create_task ():
88- with patch ("asyncio.create_task" , return_value = AwaitableMock (spec = Task )):
89- yield
94+ with patch ("asyncio.create_task" ) as mock_task :
95+ # Return an AwaitableTask that can be awaited
96+ mock_task .return_value = AwaitableTask (spec = Task )
97+ yield mock_task
9098
9199
92100@pytest .fixture (autouse = True )
@@ -124,17 +132,23 @@ async def test_hf_initialization(patch_central_database, mock_download_specific_
124132
125133
126134@pytest .mark .skipif (not is_torch_installed (), reason = "torch is not installed" )
127- def test_is_model_id_valid_true ():
135+ @pytest .mark .asyncio
136+ async def test_is_model_id_valid_true ():
128137 # Simulate valid model ID
129138 hf_chat = HuggingFaceChatTarget (model_id = "test_model" , use_cuda = False )
139+ # Await the background task to prevent warnings
140+ await hf_chat .load_model_and_tokenizer_task
130141 assert hf_chat .is_model_id_valid ()
131142
132143
133144@pytest .mark .skipif (not is_torch_installed (), reason = "torch is not installed" )
134- def test_is_model_id_valid_false ():
145+ @pytest .mark .asyncio
146+ async def test_is_model_id_valid_false ():
135147 # Simulate invalid model ID by causing an exception
136148 with patch ("transformers.PretrainedConfig.from_pretrained" , side_effect = Exception ("Invalid model" )):
137149 hf_chat = HuggingFaceChatTarget (model_id = "test_model" , use_cuda = False )
150+ # Await the background task to prevent warnings
151+ await hf_chat .load_model_and_tokenizer_task
138152 assert not hf_chat .is_model_id_valid ()
139153
140154
@@ -191,8 +205,11 @@ async def test_missing_chat_template_error():
191205
192206
193207@pytest .mark .skipif (not is_torch_installed (), reason = "torch is not installed" )
194- def test_invalid_prompt_request_validation ():
208+ @pytest .mark .asyncio
209+ async def test_invalid_prompt_request_validation ():
195210 hf_chat = HuggingFaceChatTarget (model_id = "test_model" , use_cuda = False )
211+ # Await the background task to prevent warnings
212+ await hf_chat .load_model_and_tokenizer_task
196213
197214 # Create an invalid prompt request with multiple message pieces
198215 message_piece1 = MessagePiece (
@@ -301,18 +318,24 @@ async def test_optional_kwargs_args_passed_when_loading_model(mock_transformers)
301318
302319
303320@pytest .mark .skipif (not is_torch_installed (), reason = "torch is not installed" )
304- def test_is_json_response_supported ():
321+ @pytest .mark .asyncio
322+ async def test_is_json_response_supported ():
305323 hf_chat = HuggingFaceChatTarget (model_id = "dummy" , use_cuda = False , trust_remote_code = True )
324+ # Await the background task to prevent warnings
325+ await hf_chat .load_model_and_tokenizer_task
306326 assert hf_chat .is_json_response_supported () is False
307327
308328
309329@pytest .mark .skipif (not is_torch_installed (), reason = "torch is not installed" )
310- def test_hugging_face_chat_sets_endpoint_and_rate_limit ():
330+ @pytest .mark .asyncio
331+ async def test_hugging_face_chat_sets_endpoint_and_rate_limit ():
311332 target = HuggingFaceChatTarget (
312333 model_id = "test_model" ,
313334 use_cuda = False ,
314335 max_requests_per_minute = 30 ,
315336 )
337+ # Await the background task to prevent warnings
338+ await target .load_model_and_tokenizer_task
316339 identifier = target .get_identifier ()
317340 # HuggingFaceChatTarget doesn't set an endpoint (it's local), so it shouldn't be in identifier
318341 assert "endpoint" not in identifier
0 commit comments