@@ -433,7 +433,7 @@ async def test_dispatch_new_but_finished(
433433 generator : DispatchGenerator ,
434434 fake_time : time_machine .Coordinates ,
435435) -> None :
436- """Test that a dispatch that is already finished is not started."""
436+ """Test that a finished dispatch is not started at startup ."""
437437 # Generate a dispatch that is already finished
438438 finished_dispatch = generator .generate_dispatch ()
439439 finished_dispatch = replace (
@@ -444,10 +444,25 @@ async def test_dispatch_new_but_finished(
444444 recurrence = RecurrenceRule (),
445445 type = "TEST_TYPE" ,
446446 )
447- # Create an old dispatch
447+ # Inject an old dispatch
448448 test_env .client .set_dispatches (test_env .microgrid_id , [finished_dispatch ])
449449 await test_env .service .stop ()
450450 test_env .service .start ()
451+ test_env = replace (
452+ test_env ,
453+ lifecycle_events = test_env .service .new_lifecycle_events_receiver ("TEST_TYPE" ),
454+ running_state_change = (
455+ await test_env .service .new_running_state_event_receiver (
456+ "TEST_TYPE" , unify_running_intervals = None
457+ )
458+ ),
459+ )
460+
461+ fake_time .shift (timedelta (seconds = 1 ))
462+ # Process the lifecycle event caused by the old dispatch at startup
463+ await test_env .lifecycle_events .receive ()
464+
465+ await asyncio .sleep (1 )
451466
452467 # Create another dispatch the normal way
453468 new_dispatch = generator .generate_dispatch ()
@@ -459,8 +474,7 @@ async def test_dispatch_new_but_finished(
459474 recurrence = RecurrenceRule (),
460475 type = "TEST_TYPE" ,
461476 )
462- # Consume one lifecycle_updates event
463- await test_env .lifecycle_events .receive ()
477+
464478 new_dispatch = await _test_new_dispatch_created (test_env , new_dispatch )
465479
466480 # Advance time to when the new dispatch should still not start
0 commit comments