@@ -328,3 +328,54 @@ async def test_does_not_restart_on_base_exception(
328328 ),
329329 (* RUN_INFO , "All 1 actor(s) finished." ),
330330 ]
331+
332+
333+ async def test_does_not_restart_if_cancelled (
334+ actor_auto_restart_once : None , # pylint: disable=unused-argument
335+ caplog : pytest .LogCaptureFixture ,
336+ ) -> None :
337+ """Create a faulty actor and expect it not to restart when cancelled."""
338+ caplog .set_level ("DEBUG" , logger = "frequenz.sdk.actor._actor" )
339+ caplog .set_level ("DEBUG" , logger = "frequenz.sdk.actor._run_utils" )
340+
341+ input_chan_1 : Broadcast [bool ] = Broadcast ("TestChannel1" )
342+ input_chan_2 : Broadcast [bool ] = Broadcast ("TestChannel2" )
343+
344+ echo_chan : Broadcast [bool ] = Broadcast ("echo output" )
345+ echo_rx = echo_chan .new_receiver ()
346+
347+ actor = EchoActor (
348+ "EchoActor" ,
349+ input_chan_1 .new_receiver (),
350+ input_chan_2 .new_receiver (),
351+ echo_chan .new_sender (),
352+ )
353+
354+ async def cancel_actor () -> None :
355+ """Cancel the actor after a short delay."""
356+ await input_chan_1 .new_sender ().send (True )
357+ msg = await echo_rx .receive ()
358+ assert msg is True
359+ assert actor .is_running is True
360+
361+ await input_chan_2 .new_sender ().send (False )
362+ msg = await echo_rx .receive ()
363+ assert msg is False
364+
365+ actor .cancel ()
366+
367+ async with asyncio .timeout (1.0 ):
368+ async with asyncio .TaskGroup () as group :
369+ group .create_task (cancel_actor (), name = "cancel" )
370+ await run (actor )
371+
372+ assert actor .is_running is False
373+ assert BaseTestActor .restart_count == 0
374+ assert caplog .record_tuples == [
375+ (* RUN_INFO , "Starting 1 actor(s)..." ),
376+ (* ACTOR_INFO , "Actor EchoActor[EchoActor]: Starting..." ),
377+ (* RUN_INFO , "Actor EchoActor[EchoActor]: Started normally." ),
378+ (* ACTOR_INFO , "Actor EchoActor[EchoActor]: Cancelled." ),
379+ (* RUN_ERROR , "Actor EchoActor[EchoActor]: Raised an exception while running." ),
380+ (* RUN_INFO , "All 1 actor(s) finished." ),
381+ ]
0 commit comments