|
15 | 15 | import time_machine
|
16 | 16 | from frequenz.channels import Broadcast, Receiver, Sender
|
17 | 17 | from frequenz.client.dispatch import recurrence
|
18 |
| -from frequenz.client.dispatch.recurrence import Frequency |
| 18 | +from frequenz.client.dispatch.recurrence import Frequency, RecurrenceRule |
19 | 19 | from frequenz.client.dispatch.test.client import FakeClient
|
20 | 20 | from frequenz.client.dispatch.test.generator import DispatchGenerator
|
21 | 21 | from frequenz.sdk.actor import Actor
|
@@ -104,9 +104,17 @@ def actor(self, identity: int) -> MockActor:
|
104 | 104 | """Return the actor."""
|
105 | 105 | # pylint: disable=protected-access
|
106 | 106 | assert identity in self.actors_service._actors
|
107 |
| - return cast(MockActor, self.actors_service._actors[identity]) |
| 107 | + return cast(MockActor, self.actors_service._actors[identity].actor) |
108 | 108 | # pylint: enable=protected-access
|
109 | 109 |
|
| 110 | + def is_running(self, identity: int) -> bool: |
| 111 | + """Return whether the actor is running.""" |
| 112 | + # pylint: disable-next=protected-access |
| 113 | + if identity not in self.actors_service._actors: |
| 114 | + return False |
| 115 | + |
| 116 | + return self.actor(identity).is_running |
| 117 | + |
110 | 118 |
|
111 | 119 | @fixture
|
112 | 120 | async def test_env() -> AsyncIterator[_TestEnv]:
|
@@ -383,3 +391,102 @@ async def new_mock_receiver(
|
383 | 391 |
|
384 | 392 | # Check if actor instance is created
|
385 | 393 | assert identity(dispatch) in actor_manager._actors
|
| 394 | + |
| 395 | + |
| 396 | +async def test_actor_dispatcher_update_isolation( |
| 397 | + test_env: _TestEnv, |
| 398 | + fake_time: time_machine.Coordinates, |
| 399 | +) -> None: |
| 400 | + """Test that updates for one dispatch don't affect other actors of the same type.""" |
| 401 | + dispatch_type = "ISOLATION_TEST" |
| 402 | + start_time = _now() |
| 403 | + duration = timedelta(minutes=5) |
| 404 | + |
| 405 | + # Create first dispatch |
| 406 | + dispatch1_spec = replace( |
| 407 | + test_env.generator.generate_dispatch(), |
| 408 | + id=101, # Unique ID |
| 409 | + type=dispatch_type, |
| 410 | + active=True, |
| 411 | + dry_run=False, |
| 412 | + start_time=start_time + timedelta(seconds=1), # Stagger start slightly |
| 413 | + duration=duration, |
| 414 | + payload={"instance": 1}, |
| 415 | + recurrence=RecurrenceRule(), |
| 416 | + ) |
| 417 | + dispatch1 = Dispatch(dispatch1_spec) |
| 418 | + |
| 419 | + # Create second dispatch of the same type, different ID |
| 420 | + dispatch2_spec = replace( |
| 421 | + test_env.generator.generate_dispatch(), |
| 422 | + id=102, # Unique ID |
| 423 | + type=dispatch_type, # Same type |
| 424 | + active=True, |
| 425 | + dry_run=False, |
| 426 | + start_time=start_time + timedelta(seconds=2), # Stagger start slightly |
| 427 | + duration=duration, |
| 428 | + payload={"instance": 2}, |
| 429 | + recurrence=RecurrenceRule(), |
| 430 | + ) |
| 431 | + dispatch2 = Dispatch(dispatch2_spec) |
| 432 | + |
| 433 | + # Send dispatch 1 to start actor 1 |
| 434 | + # print(f"Sending dispatch 1: {dispatch1}") |
| 435 | + await test_env.running_status_sender.send(dispatch1) |
| 436 | + fake_time.shift(timedelta(seconds=1.1)) # Move time past dispatch1 start |
| 437 | + await asyncio.sleep(0.1) # Allow actor to start |
| 438 | + |
| 439 | + assert test_env.is_running(101), "Actor 1 should be running" |
| 440 | + actor1 = test_env.actor(101) |
| 441 | + assert actor1 is not None |
| 442 | + # pylint: disable-next=protected-access |
| 443 | + assert actor1.initial_dispatch._src.id == 101 |
| 444 | + assert actor1.initial_dispatch.options == {"instance": 1} |
| 445 | + assert not test_env.is_running(102), "Actor 2 should not be running yet" |
| 446 | + |
| 447 | + # Send dispatch 2 to start actor 2 |
| 448 | + # print(f"Sending dispatch 2: {dispatch2}") |
| 449 | + await test_env.running_status_sender.send(dispatch2) |
| 450 | + fake_time.shift(timedelta(seconds=1)) # Move time past dispatch2 start |
| 451 | + await asyncio.sleep(0.1) # Allow actor to start |
| 452 | + |
| 453 | + assert test_env.actor(101).is_running, "Actor 1 should still be running" |
| 454 | + assert test_env.actor(102).is_running, "Actor 2 should now be running" |
| 455 | + actor2 = test_env.actor(102) |
| 456 | + assert actor2 is not None |
| 457 | + # pylint: disable-next=protected-access |
| 458 | + assert actor2.initial_dispatch._src.id == 102 |
| 459 | + assert actor2.initial_dispatch.options == {"instance": 2} |
| 460 | + |
| 461 | + # Now, send an update to stop dispatch 1 |
| 462 | + dispatch1_stop = Dispatch( |
| 463 | + replace(dispatch1_spec, duration=timedelta(seconds=1), active=False) |
| 464 | + ) |
| 465 | + # print(f"Sending stop for dispatch 1: {dispatch1_stop}") |
| 466 | + await test_env.running_status_sender.send(dispatch1_stop) |
| 467 | + await asyncio.sleep(0.1) # Allow ActorDispatcher to process the stop |
| 468 | + |
| 469 | + # THE CORE ASSERTION: Actor 1 should stop, Actor 2 should remain running |
| 470 | + # pylint: disable=protected-access |
| 471 | + assert ( |
| 472 | + 101 not in test_env.actors_service._actors |
| 473 | + ), "Actor 1 should have been removed" |
| 474 | + # pylint: enable=protected-access |
| 475 | + assert ( |
| 476 | + test_env.actor(102).is_running is True |
| 477 | + ), "Actor 2 should be running after Actor 1 stopped" |
| 478 | + # Double check actor1 object state if needed (though removal is stronger check) |
| 479 | + # assert not actor1.is_running |
| 480 | + |
| 481 | + # Cleanup: Stop Actor 2 |
| 482 | + dispatch2_stop = Dispatch(replace(dispatch2_spec, active=False)) |
| 483 | + # print(f"Sending stop for dispatch 2: {dispatch2_stop}") |
| 484 | + await test_env.running_status_sender.send(dispatch2_stop) |
| 485 | + await asyncio.sleep(0.1) # Allow ActorDispatcher to process the stop |
| 486 | + |
| 487 | + # pylint: disable=protected-access |
| 488 | + assert ( |
| 489 | + 102 not in test_env.actors_service._actors |
| 490 | + ), "Actor 2 should have been removed" |
| 491 | + # pylint: enable=protected-access |
| 492 | + assert not test_env.is_running(102), "Actor 2 should be stopped" |
0 commit comments