|
15 | 15 | from frequenz.client.dispatch.test.client import FakeClient, to_create_params
|
16 | 16 | from frequenz.client.dispatch.test.generator import DispatchGenerator
|
17 | 17 | from frequenz.client.dispatch.types import Dispatch as BaseDispatch
|
18 |
| -from frequenz.client.dispatch.types import Frequency |
| 18 | +from frequenz.client.dispatch.types import DispatchEvent as BaseDispatchEvent |
| 19 | +from frequenz.client.dispatch.types import Event, Frequency, RecurrenceRule |
19 | 20 | from pytest import fixture
|
20 | 21 |
|
21 | 22 | from frequenz.dispatch import (
|
@@ -153,8 +154,9 @@ async def _test_new_dispatch_created(
|
153 | 154 | case Deleted(dispatch) | Updated(dispatch):
|
154 | 155 | assert False, "Expected a created event"
|
155 | 156 | case Created(dispatch):
|
156 |
| - sample = update_dispatch(sample, dispatch) |
157 |
| - assert dispatch == Dispatch(sample) |
| 157 | + sample = Dispatch(update_dispatch(sample, dispatch)) |
| 158 | + sample._set_running_status_notified() # pylint: disable=protected-access |
| 159 | + assert dispatch == sample |
158 | 160 |
|
159 | 161 | return sample
|
160 | 162 |
|
@@ -424,3 +426,44 @@ async def test_dispatch_inf_duration_updated_to_finite_and_continues(
|
424 | 426 | # Expect notification to stop the dispatch because the duration has now passed
|
425 | 427 | stopped_dispatch = await actor_env.running_state_change.receive()
|
426 | 428 | assert stopped_dispatch.running(sample.type) == RunningState.STOPPED
|
| 429 | + |
| 430 | + |
| 431 | +async def test_dispatch_new_but_finished( |
| 432 | + actor_env: ActorTestEnv, |
| 433 | + generator: DispatchGenerator, |
| 434 | + fake_time: time_machine.Coordinates, |
| 435 | +) -> None: |
| 436 | + """Test that a dispatch that is already finished is not started.""" |
| 437 | + # Generate a dispatch that is already finished |
| 438 | + finished_dispatch = generator.generate_dispatch() |
| 439 | + finished_dispatch = replace( |
| 440 | + finished_dispatch, |
| 441 | + active=True, |
| 442 | + duration=timedelta(seconds=5), |
| 443 | + start_time=_now() - timedelta(seconds=50), |
| 444 | + recurrence=None, |
| 445 | + type="I_SHOULD_NEVER_RUN", |
| 446 | + ) |
| 447 | + # Create an old dispatch |
| 448 | + actor_env.client.set_dispatches(actor_env.microgrid_id, [finished_dispatch]) |
| 449 | + await actor_env.actor.stop() |
| 450 | + actor_env.actor.start() |
| 451 | + |
| 452 | + # Create another dispatch the normal way |
| 453 | + new_dispatch = generator.generate_dispatch() |
| 454 | + new_dispatch = replace( |
| 455 | + new_dispatch, |
| 456 | + active=True, |
| 457 | + duration=timedelta(seconds=10), |
| 458 | + start_time=_now() + timedelta(seconds=5), |
| 459 | + recurrence=RecurrenceRule(), |
| 460 | + type="NEW_BETTER_DISPATCH", |
| 461 | + ) |
| 462 | + # Consume one lifecycle_updates event |
| 463 | + await actor_env.updated_dispatches.receive() |
| 464 | + new_dispatch = await _test_new_dispatch_created(actor_env, new_dispatch) |
| 465 | + |
| 466 | + # Advance time to when the new dispatch should still not start |
| 467 | + fake_time.shift(timedelta(seconds=100)) |
| 468 | + |
| 469 | + assert await actor_env.running_state_change.receive() == new_dispatch |
0 commit comments