44from app .storage .manager import LocalStorageManager
55from app .models .project import Job
66
7+ import threading
8+
79class MockStorage (LocalStorageManager ):
810 def __init__ (self ):
911 self .jobs = {}
@@ -19,17 +21,23 @@ def test_job_service_success_lifecycle():
1921 storage = MockStorage ()
2022 service = JobService (storage )
2123
22- # Task that returns a result
24+ # Use an event to pause the background task
25+ task_start_event = threading .Event ()
26+
2327 def success_task (job ):
28+ task_start_event .wait () # Wait for signal
2429 return {"final_status" : "success" , "asset" : "video.mp4" }
2530
2631 job = service .start_job ("proj_123" , "render" , success_task )
2732
28- # Check initial state
29- assert job .status == "queued"
33+ # Check initial state (might be queued or already running due to fast orchestration)
34+ assert job .status in [ "queued" , "running" ]
3035 assert job .project_id == "proj_123"
3136
32- # Poll for completion (it runs in a ThreadPoolExecutor)
37+ # Signal task to proceed
38+ task_start_event .set ()
39+
40+ # Poll for completion
3341 max_wait = 5
3442 start_time = time .time ()
3543 while time .time () - start_time < max_wait :
@@ -46,12 +54,19 @@ def test_job_service_failure_lifecycle():
4654 storage = MockStorage ()
4755 service = JobService (storage )
4856
49- # Task that raises an exception
57+ task_start_event = threading .Event ()
58+
5059 def failing_task (job ):
60+ task_start_event .wait ()
5161 raise ValueError ("Simulated provider failure" )
5262
5363 job = service .start_job ("proj_456" , "video" , failing_task )
5464
65+ # Safe to check queued or running
66+ assert job .status in ["queued" , "running" ]
67+
68+ task_start_event .set ()
69+
5570 # Poll for failure
5671 max_wait = 5
5772 start_time = time .time ()
0 commit comments