@@ -426,7 +426,7 @@ async def test_dispatch_new_but_finished(
426426 generator : DispatchGenerator ,
427427 fake_time : time_machine .Coordinates ,
428428) -> None :
429- """Test that a dispatch that is already finished is not started."""
429+ """Test that a finished dispatch is not started at startup ."""
430430 # Generate a dispatch that is already finished
431431 finished_dispatch = generator .generate_dispatch ()
432432 finished_dispatch = replace (
@@ -437,10 +437,25 @@ async def test_dispatch_new_but_finished(
437437 recurrence = RecurrenceRule (),
438438 type = "TEST_TYPE" ,
439439 )
440- # Create an old dispatch
440+ # Inject an old dispatch
441441 test_env .client .set_dispatches (test_env .microgrid_id , [finished_dispatch ])
442442 await test_env .service .stop ()
443443 test_env .service .start ()
444+ test_env = replace (
445+ test_env ,
446+ lifecycle_events = test_env .service .new_lifecycle_events_receiver ("TEST_TYPE" ),
447+ running_state_change = (
448+ await test_env .service .new_running_state_event_receiver (
449+ "TEST_TYPE" , unify_running_intervals = False
450+ )
451+ ),
452+ )
453+
454+ fake_time .shift (timedelta (seconds = 1 ))
455+ # Process the lifecycle event caused by the old dispatch at startup
456+ await test_env .lifecycle_events .receive ()
457+
458+ await asyncio .sleep (1 )
444459
445460 # Create another dispatch the normal way
446461 new_dispatch = generator .generate_dispatch ()
@@ -452,8 +467,7 @@ async def test_dispatch_new_but_finished(
452467 recurrence = RecurrenceRule (),
453468 type = "TEST_TYPE" ,
454469 )
455- # Consume one lifecycle_updates event
456- await test_env .lifecycle_events .receive ()
470+
457471 new_dispatch = await _test_new_dispatch_created (test_env , new_dispatch )
458472
459473 # Advance time to when the new dispatch should still not start
0 commit comments