11"""Tests for the dependencies module, particularly HybridBackgroundTasks."""
22
3+ import asyncio
34from unittest .mock import Mock , patch
45
56import pytest
@@ -24,8 +25,7 @@ def teardown_method(self):
2425 """Restore original use_docket setting."""
2526 settings .use_docket = self .original_use_docket
2627
27- @pytest .mark .asyncio
28- async def test_add_task_with_fastapi_background_tasks (self ):
28+ def test_add_task_with_fastapi_background_tasks (self ):
2929 """Test that tasks are added to FastAPI background tasks when use_docket=False."""
3030 settings .use_docket = False
3131
@@ -35,7 +35,7 @@ def test_func(arg1, arg2=None):
3535 return f"test_func called with { arg1 } , { arg2 } "
3636
3737 # Add task
38- await bg_tasks .add_task (test_func , "hello" , arg2 = "world" )
38+ bg_tasks .add_task (test_func , "hello" , arg2 = "world" )
3939
4040 # Verify task was added to FastAPI background tasks
4141 assert len (bg_tasks .tasks ) == 1
@@ -71,8 +71,14 @@ async def execution():
7171 async def test_func (arg1 , arg2 = None ):
7272 return f"test_func called with { arg1 } , { arg2 } "
7373
74- # Add task
75- await bg_tasks .add_task (test_func , "hello" , arg2 = "world" )
74+ # Add task - this should schedule directly in Docket
75+ bg_tasks .add_task (test_func , "hello" , arg2 = "world" )
76+
77+ # Give the async task a moment to complete
78+ await asyncio .sleep (0.1 )
79+
80+ # When use_docket=True, tasks should NOT be added to FastAPI background tasks
81+ assert len (bg_tasks .tasks ) == 0
7682
7783 # Verify Docket was used
7884 mock_docket_class .assert_called_once_with (
@@ -81,8 +87,7 @@ async def test_func(arg1, arg2=None):
8187 )
8288 mock_docket_instance .add .assert_called_once_with (test_func )
8389
84- @pytest .mark .asyncio
85- async def test_add_task_logs_correctly_for_fastapi (self ):
90+ def test_add_task_logs_correctly_for_fastapi (self ):
8691 """Test that FastAPI background tasks work without errors."""
8792 settings .use_docket = False
8893
@@ -92,7 +97,7 @@ def test_func():
9297 pass
9398
9499 # Should not raise any errors
95- await bg_tasks .add_task (test_func )
100+ bg_tasks .add_task (test_func )
96101
97102 # Verify task was added to FastAPI background tasks
98103 assert len (bg_tasks .tasks ) == 1
@@ -122,7 +127,10 @@ def test_func():
122127 pass
123128
124129 # Should not raise any errors
125- await bg_tasks .add_task (test_func )
130+ bg_tasks .add_task (test_func )
131+
132+ # Give the async task a moment to complete
133+ await asyncio .sleep (0.1 )
126134
127135 # Verify Docket was called
128136 mock_docket_instance .add .assert_called_once_with (test_func )
@@ -178,7 +186,7 @@ def test_func():
178186
179187 # Test with use_docket=False
180188 settings .use_docket = False
181- await bg_tasks .add_task (test_func )
189+ bg_tasks .add_task (test_func )
182190 assert len (bg_tasks .tasks ) == 1
183191
184192 # Clear tasks and test with use_docket=True
@@ -199,14 +207,18 @@ async def execution():
199207
200208 mock_docket_instance .add .return_value = mock_task_callable
201209
202- await bg_tasks .add_task (test_func )
210+ bg_tasks .add_task (test_func )
211+
212+ # Give the async task a moment to complete
213+ await asyncio .sleep (0.1 )
203214
204215 # Should not add to FastAPI tasks when use_docket=True
205216 assert len (bg_tasks .tasks ) == 0
206217 # Should have called Docket
207218 mock_docket_class .assert_called_once ()
208219
209- def test_uses_correct_docket_settings (self ):
220+ @pytest .mark .asyncio
221+ async def test_uses_correct_docket_settings (self ):
210222 """Test that HybridBackgroundTasks uses the correct docket name and URL from settings."""
211223 settings .use_docket = True
212224
@@ -226,12 +238,10 @@ async def execution():
226238
227239 bg_tasks = HybridBackgroundTasks ()
228240
229- async def run_test ():
230- await bg_tasks .add_task (lambda : None )
231-
232- import asyncio
241+ bg_tasks .add_task (lambda : None )
233242
234- asyncio .run (run_test ())
243+ # Give the async task a moment to complete
244+ await asyncio .sleep (0.1 )
235245
236246 # Verify Docket was initialized with correct settings
237247 mock_docket_class .assert_called_once_with (
0 commit comments