1
1
"""Tests for the dependencies module, particularly HybridBackgroundTasks."""
2
2
3
+ import asyncio
3
4
from unittest .mock import Mock , patch
4
5
5
6
import pytest
@@ -24,8 +25,7 @@ def teardown_method(self):
24
25
"""Restore original use_docket setting."""
25
26
settings .use_docket = self .original_use_docket
26
27
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 ):
29
29
"""Test that tasks are added to FastAPI background tasks when use_docket=False."""
30
30
settings .use_docket = False
31
31
@@ -35,7 +35,7 @@ def test_func(arg1, arg2=None):
35
35
return f"test_func called with { arg1 } , { arg2 } "
36
36
37
37
# Add task
38
- await bg_tasks .add_task (test_func , "hello" , arg2 = "world" )
38
+ bg_tasks .add_task (test_func , "hello" , arg2 = "world" )
39
39
40
40
# Verify task was added to FastAPI background tasks
41
41
assert len (bg_tasks .tasks ) == 1
@@ -71,8 +71,14 @@ async def execution():
71
71
async def test_func (arg1 , arg2 = None ):
72
72
return f"test_func called with { arg1 } , { arg2 } "
73
73
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
76
82
77
83
# Verify Docket was used
78
84
mock_docket_class .assert_called_once_with (
@@ -81,8 +87,7 @@ async def test_func(arg1, arg2=None):
81
87
)
82
88
mock_docket_instance .add .assert_called_once_with (test_func )
83
89
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 ):
86
91
"""Test that FastAPI background tasks work without errors."""
87
92
settings .use_docket = False
88
93
@@ -92,7 +97,7 @@ def test_func():
92
97
pass
93
98
94
99
# Should not raise any errors
95
- await bg_tasks .add_task (test_func )
100
+ bg_tasks .add_task (test_func )
96
101
97
102
# Verify task was added to FastAPI background tasks
98
103
assert len (bg_tasks .tasks ) == 1
@@ -122,7 +127,10 @@ def test_func():
122
127
pass
123
128
124
129
# 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 )
126
134
127
135
# Verify Docket was called
128
136
mock_docket_instance .add .assert_called_once_with (test_func )
@@ -178,7 +186,7 @@ def test_func():
178
186
179
187
# Test with use_docket=False
180
188
settings .use_docket = False
181
- await bg_tasks .add_task (test_func )
189
+ bg_tasks .add_task (test_func )
182
190
assert len (bg_tasks .tasks ) == 1
183
191
184
192
# Clear tasks and test with use_docket=True
@@ -199,14 +207,18 @@ async def execution():
199
207
200
208
mock_docket_instance .add .return_value = mock_task_callable
201
209
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 )
203
214
204
215
# Should not add to FastAPI tasks when use_docket=True
205
216
assert len (bg_tasks .tasks ) == 0
206
217
# Should have called Docket
207
218
mock_docket_class .assert_called_once ()
208
219
209
- def test_uses_correct_docket_settings (self ):
220
+ @pytest .mark .asyncio
221
+ async def test_uses_correct_docket_settings (self ):
210
222
"""Test that HybridBackgroundTasks uses the correct docket name and URL from settings."""
211
223
settings .use_docket = True
212
224
@@ -226,12 +238,10 @@ async def execution():
226
238
227
239
bg_tasks = HybridBackgroundTasks ()
228
240
229
- async def run_test ():
230
- await bg_tasks .add_task (lambda : None )
231
-
232
- import asyncio
241
+ bg_tasks .add_task (lambda : None )
233
242
234
- asyncio .run (run_test ())
243
+ # Give the async task a moment to complete
244
+ await asyncio .sleep (0.1 )
235
245
236
246
# Verify Docket was initialized with correct settings
237
247
mock_docket_class .assert_called_once_with (
0 commit comments