From 2a32fbd6e42142fc693260c5ede47adc331c6b61 Mon Sep 17 00:00:00 2001 From: Varun Chawla Date: Sat, 7 Feb 2026 23:58:14 -0800 Subject: [PATCH] test: add coverage for background task merging with single BackgroundTask --- tests/test_background_tasks_merge.py | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/test_background_tasks_merge.py b/tests/test_background_tasks_merge.py index 12e71a64c8..d7d91359ec 100644 --- a/tests/test_background_tasks_merge.py +++ b/tests/test_background_tasks_merge.py @@ -6,6 +6,7 @@ Related to issue #11215 from fastapi import BackgroundTasks, FastAPI from fastapi.responses import Response from fastapi.testclient import TestClient +from starlette.background import BackgroundTask app = FastAPI() @@ -110,3 +111,62 @@ def test_only_response_tasks_work(): assert response.status_code == 200 assert "response" in executed_tasks + + +@app.get("/single-background-task-with-injected") +async def endpoint_with_single_background_task_and_injected(tasks: BackgroundTasks): + """Endpoint with a single BackgroundTask in Response and injected BackgroundTasks""" + tasks.add_task(task_from_injected) + return Response( + content="Custom response", + background=BackgroundTask(task_from_response), + ) + + +@app.get("/single-background-task-with-single-injected") +async def endpoint_with_single_background_task_and_single_injected( + tasks: BackgroundTasks, +): + """Endpoint with a single BackgroundTask in Response and single injected task""" + # Add just one task to injected BackgroundTasks to test the single+single case + tasks.add_task(task_from_injected) + return Response( + content="Custom response", + background=BackgroundTask(task_from_response), + ) + + +def test_single_background_task_with_injected_tasks(): + """Test that injected BackgroundTasks are merged with a single BackgroundTask from Response""" + global executed_tasks + executed_tasks = [] + + client = TestClient(app) + response = client.get("/single-background-task-with-injected") + + assert response.status_code == 200 + # Both tasks should be executed + assert "injected" in executed_tasks, "Injected task was not executed" + assert "response" in executed_tasks, "Response task was not executed" + # Injected tasks should run before response tasks + assert executed_tasks.index("injected") < executed_tasks.index( + "response" + ), "Task execution order is wrong" + + +def test_single_background_task_with_single_injected_task(): + """Test that two single BackgroundTasks are merged correctly""" + global executed_tasks + executed_tasks = [] + + client = TestClient(app) + response = client.get("/single-background-task-with-single-injected") + + assert response.status_code == 200 + # Both tasks should be executed + assert "injected" in executed_tasks, "Injected task was not executed" + assert "response" in executed_tasks, "Response task was not executed" + # Injected tasks should run before response tasks + assert executed_tasks.index("injected") < executed_tasks.index( + "response" + ), "Task execution order is wrong"