@@ -454,3 +454,45 @@ async def test_dispatch_new_but_finished(
454454 fake_time .shift (timedelta (seconds = 100 ))
455455
456456 assert await actor_env .running_state_change .receive () == new_dispatch
457+
458+
459+ async def test_notification_on_actor_start (
460+ actor_env : ActorTestEnv ,
461+ generator : DispatchGenerator ,
462+ fake_time : time_machine .Coordinates ,
463+ ) -> None :
464+ """Test that the actor sends notifications for all running dispatches on start."""
465+ # Generate a dispatch that is already running
466+ running_dispatch = generator .generate_dispatch ()
467+ running_dispatch = replace (
468+ running_dispatch ,
469+ active = True ,
470+ duration = timedelta (seconds = 10 ),
471+ start_time = _now () - timedelta (seconds = 5 ),
472+ recurrence = RecurrenceRule (),
473+ type = "I_SHOULD_RUN" ,
474+ )
475+ # Generate a dispatch that is not running
476+ stopped_dispatch = generator .generate_dispatch ()
477+ stopped_dispatch = replace (
478+ stopped_dispatch ,
479+ active = False ,
480+ duration = timedelta (seconds = 5 ),
481+ start_time = _now () - timedelta (seconds = 5 ),
482+ recurrence = RecurrenceRule (),
483+ type = "I_SHOULD_NOT_RUN" ,
484+ )
485+ await actor_env .actor .stop ()
486+
487+ # Create the dispatches
488+ actor_env .client .set_dispatches (
489+ actor_env .microgrid_id , [running_dispatch , stopped_dispatch ]
490+ )
491+ actor_env .actor .start ()
492+
493+ fake_time .shift (timedelta (seconds = 1 ))
494+ await asyncio .sleep (1 )
495+
496+ # Expect notification of the running dispatch being ready to run
497+ ready_dispatch = await actor_env .running_state_change .receive ()
498+ assert ready_dispatch .running (running_dispatch .type ) == RunningState .RUNNING
0 commit comments