55import asyncio
66
77import unittest
8- from unittest .mock import patch , Mock , AsyncMock
8+ from unittest .mock import patch , Mock , MagicMock
99import pytest
1010
11- import aiohttp
11+ import requests
1212import runpod
1313from runpod .serverless .modules import rp_fastapi
1414
@@ -54,43 +54,31 @@ def test_start_serverless_with_realtime(self):
5454 os .environ .pop ("RUNPOD_REALTIME_PORT" )
5555 os .environ .pop ("RUNPOD_ENDPOINT_ID" )
5656
57- @pytest .mark .asyncio
5857 def test_webhook_sender_success (self ):
5958 """Test the webhook sender when the request is successful."""
60- loop = asyncio .get_event_loop ()
61-
62- module_location = "runpod.serverless.modules.rp_fastapi.aiohttp.ClientSession"
59+ module_location = "runpod.serverless.modules.rp_fastapi.requests.Session.post"
6360
64- with patch (f" { module_location } .post" , new_callable = AsyncMock ) as mock_post :
61+ with patch (module_location , new_callable = MagicMock ) as mock_post :
6562 # Simulate a successful response
66- mock_post .return_value .__aenter__ . return_value . status = 200
63+ mock_post .return_value .status_code = 200
6764
68- # Directly await the function
69- success = asyncio .run (rp_fastapi ._send_webhook_async (
70- "test_webhook" , {"test" : "output" }))
65+ # Call the function
66+ success = rp_fastapi ._send_webhook ("test_webhook" , {"test" : "output" })
7167 assert success is True
7268
73- loop .close ()
74-
75- @pytest .mark .asyncio
7669 def test_webhook_sender_failure (self ):
7770 """Test the webhook sender when the request fails."""
78- loop = asyncio .get_event_loop ()
79-
80- module_location = "runpod.serverless.modules.rp_fastapi.aiohttp.ClientSession"
71+ module_location = "runpod.serverless.modules.rp_fastapi.requests.Session.post"
8172
82- with patch (f" { module_location } .post" , new_callable = AsyncMock ) as mock_post :
83- # Configure the mock to raise an exception to simulate a 500 error
84- mock_post .return_value .__aenter__ . return_value . raise_for_status .side_effect = aiohttp . ClientResponseError ( # pylint: disable=line-too-long
85- request_info = None , history = None , status = 500 )
73+ with patch (module_location , new_callable = MagicMock ) as mock_post :
74+ # Configure the mock to simulate a failure (e.g., a 500 status code)
75+ mock_post .return_value .raise_for_status .side_effect = requests . HTTPError ()
76+ mock_post . return_value . status_code = 500
8677
87- # Directly await the function
88- success = asyncio .run (rp_fastapi ._send_webhook_async (
89- "test_webhook" , {"test" : "output" }))
78+ # Call the function
79+ success = rp_fastapi ._send_webhook ("test_webhook" , {"test" : "output" })
9080 assert success is False
9181
92- loop .close ()
93-
9482 @pytest .mark .asyncio
9583 def test_run (self ):
9684 '''
@@ -195,9 +183,9 @@ def generator_handler(job):
195183 assert "error" in error_runsync_return
196184
197185 # Test webhook caller sent
198- with patch (f"{ module_location } ._send_webhook_async" , return_value = True ) as mock_send_webhook : # pylint: disable=line-too-long
186+ with patch (f"{ module_location } .threading" ) as mock_thread :
199187 asyncio .run (worker_api ._sim_runsync (input_object_with_webhook ))
200- assert mock_send_webhook .called
188+ assert mock_thread . Thread .called
201189
202190 loop .close ()
203191
@@ -212,7 +200,8 @@ def test_stream(self):
212200 with patch (f"{ module_location } .FastAPI" , Mock ()), \
213201 patch (f"{ module_location } .APIRouter" , return_value = Mock ()), \
214202 patch (f"{ module_location } .uvicorn" , Mock ()), \
215- patch (f"{ module_location } .uuid.uuid4" , return_value = "123" ):
203+ patch (f"{ module_location } .uuid.uuid4" , return_value = "123" ), \
204+ patch (f"{ module_location } .threading" ):
216205
217206 default_input_object = rp_fastapi .DefaultRequest (
218207 input = {"test_input" : "test_input" }
@@ -243,10 +232,10 @@ def test_stream(self):
243232 }
244233
245234 # Test webhook caller sent
246- with patch (f"{ module_location } ._send_webhook_async " , return_value = True ) as mock_send_webhook : # pylint: disable=line-too-long
235+ with patch (f"{ module_location } .threading " , return_value = True ) as mock_threading :
247236 asyncio .run (worker_api ._sim_run (input_object_with_webhook ))
248237 asyncio .run (worker_api ._sim_stream ("test-123" ))
249- assert mock_send_webhook .called
238+ assert mock_threading . Thread .called
250239
251240 # Test with generator handler
252241 def generator_handler (job ):
@@ -275,7 +264,8 @@ def test_status(self):
275264 with patch (f"{ module_location } .FastAPI" , Mock ()), \
276265 patch (f"{ module_location } .APIRouter" , return_value = Mock ()), \
277266 patch (f"{ module_location } .uvicorn" , Mock ()), \
278- patch (f"{ module_location } .uuid.uuid4" , return_value = "123" ):
267+ patch (f"{ module_location } .uuid.uuid4" , return_value = "123" ), \
268+ patch (f"{ module_location } .threading" ):
279269
280270 worker_api = rp_fastapi .WorkerAPI ({"handler" : self .handler })
281271
@@ -306,10 +296,10 @@ def test_status(self):
306296 }
307297
308298 # Test webhook caller sent
309- with patch (f"{ module_location } ._send_webhook_async " , return_value = True ) as mock_send_webhook : # pylint: disable=line-too-long
299+ with patch (f"{ module_location } .threading " , return_value = True ) as mock_threading :
310300 asyncio .run (worker_api ._sim_run (input_object_with_webhook ))
311301 asyncio .run (worker_api ._sim_status ("test-123" ))
312- assert mock_send_webhook .called
302+ assert mock_threading . Thread .called
313303
314304 # Test with generator handler
315305 def generator_handler (job ):
0 commit comments