Skip to content

Commit 630e02f

Browse files
authored
fix: reduce sleep time in timeout tests and add elapsed time checks (#111)
- Changed sleep duration from 1000s (16 minutes) to 10s in both timeout tests - Added elapsed time assertions to verify tests complete within 2 seconds - Prevents CI jobs from hanging due to background threads sleeping for too long - Ensures tests verify that the calling thread returns immediately after timeout
1 parent 1f60746 commit 630e02f

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

tests/unit/task_processor/test_unit_task_processor_processor.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,23 @@ def test_run_task_kills_task_after_timeout(
115115
task = Task.create(
116116
sleep_task.task_identifier,
117117
scheduled_for=timezone.now(),
118-
args=(1000,),
118+
args=(10,), # Sleep for 10 seconds
119119
timeout=timedelta(microseconds=1),
120120
)
121121
task.save(using=current_database)
122122

123123
# When
124+
start_time = time.time()
124125
task_runs = run_tasks(current_database)
126+
elapsed_time = time.time() - start_time
127+
128+
# Then - the function should return quickly (within ~2 seconds)
129+
# Not block for 10 seconds waiting for the worker thread
130+
assert elapsed_time < 2.0, (
131+
f"run_tasks blocked for {elapsed_time:.2f} seconds, "
132+
"indicating it's waiting for the worker thread to finish"
133+
)
125134

126-
# Then
127135
assert (
128136
len(task_runs)
129137
== TaskRun.objects.using(current_database).filter(task=task).count()
@@ -160,17 +168,26 @@ def test_run_recurring_task_kills_task_after_timeout(
160168
run_every=timedelta(seconds=1), timeout=timedelta(microseconds=1)
161169
)
162170
def _dummy_recurring_task() -> None:
163-
time.sleep(1000)
171+
time.sleep(10) # Sleep for 10 seconds
164172

165173
initialise()
166174

167175
task = RecurringTask.objects.using(current_database).get(
168176
task_identifier="test_unit_task_processor_processor._dummy_recurring_task",
169177
)
178+
170179
# When
180+
start_time = time.time()
171181
task_runs = run_recurring_tasks(current_database)
182+
elapsed_time = time.time() - start_time
183+
184+
# Then - the function should return quickly (within ~2 seconds)
185+
# Not block for 10 seconds waiting for the worker thread
186+
assert elapsed_time < 2.0, (
187+
f"run_recurring_tasks blocked for {elapsed_time:.2f} seconds, "
188+
"indicating it's waiting for the worker thread to finish"
189+
)
172190

173-
# Then
174191
assert (
175192
len(task_runs)
176193
== RecurringTaskRun.objects.using(current_database).filter(task=task).count()

0 commit comments

Comments
 (0)