forked from frequenz-floss/frequenz-dispatch-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_managing_actor.py
More file actions
492 lines (401 loc) · 15.6 KB
/
test_managing_actor.py
File metadata and controls
492 lines (401 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# LICENSE: ALL RIGHTS RESERVED
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
"""Test the dispatch runner."""
import asyncio
import heapq
from dataclasses import dataclass, replace
from datetime import datetime, timedelta, timezone
from typing import AsyncIterator, Callable, Iterator, cast
from unittest.mock import patch
import async_solipsism
import pytest
import time_machine
from frequenz.channels import Broadcast, Receiver, Sender
from frequenz.client.dispatch import recurrence
from frequenz.client.dispatch.recurrence import Frequency, RecurrenceRule
from frequenz.client.dispatch.test.client import FakeClient
from frequenz.client.dispatch.test.generator import DispatchGenerator
from frequenz.sdk.actor import Actor
from pytest import fixture
from frequenz.dispatch import (
ActorDispatcher,
Dispatch,
Dispatcher,
DispatchInfo,
MergeByType,
MergeByTypeTarget,
MergeStrategy,
)
from frequenz.dispatch._bg_service import DispatchScheduler
@fixture
def generator() -> DispatchGenerator:
"""Return a dispatch generator."""
return DispatchGenerator()
@fixture
def event_loop_policy() -> async_solipsism.EventLoopPolicy:
"""Set the event loop policy to use async_solipsism."""
policy = async_solipsism.EventLoopPolicy()
asyncio.set_event_loop_policy(policy)
return policy
@fixture
def fake_time() -> Iterator[time_machine.Coordinates]:
"""Replace real time with a time machine that doesn't automatically tick."""
# destination can be a datetime or a timestamp (int), so are moving to the
# epoch (in UTC!)
with time_machine.travel(destination=0, tick=False) as traveller:
yield traveller
def _now() -> datetime:
"""Return the current time in UTC."""
return datetime.now(tz=timezone.utc)
class MockActor(Actor):
"""Mock actor for testing."""
def __init__(
self, initial_dispatch: DispatchInfo, receiver: Receiver[DispatchInfo]
) -> None:
"""Initialize the actor."""
super().__init__(name="MockActor")
self.initial_dispatch = initial_dispatch
self.receiver = receiver
async def _run(self) -> None:
while True:
await asyncio.sleep(1)
@classmethod
async def create(
cls, initial_dispatch: DispatchInfo, receiver: Receiver[DispatchInfo]
) -> "MockActor":
"""Create a new actor."""
actor = cls(initial_dispatch, receiver)
return actor
@classmethod
async def create_fail(
cls, __: DispatchInfo, _: Receiver[DispatchInfo]
) -> "MockActor":
"""Create a new actor."""
raise ValueError("Failed to create actor")
@dataclass
class _TestEnv:
"""Test environment."""
actors_service: ActorDispatcher
running_status_sender: Sender[Dispatch]
generator: DispatchGenerator = DispatchGenerator()
def actor(self, identity: int) -> MockActor:
"""Return the actor."""
# pylint: disable=protected-access
assert identity in self.actors_service._actors
return cast(MockActor, self.actors_service._actors[identity].actor)
# pylint: enable=protected-access
def is_running(self, identity: int) -> bool:
"""Return whether the actor is running."""
# pylint: disable-next=protected-access
if identity not in self.actors_service._actors:
return False
return self.actor(identity).is_running
@fixture
async def test_env() -> AsyncIterator[_TestEnv]:
"""Create a test environment."""
channel = Broadcast[Dispatch](name="dispatch ready test channel")
actors_service = ActorDispatcher(
actor_factory=MockActor.create,
running_status_receiver=channel.new_receiver(),
dispatch_identity=lambda dispatch: dispatch.id,
)
actors_service.start()
await asyncio.sleep(1)
yield _TestEnv(
actors_service=actors_service,
running_status_sender=channel.new_sender(),
)
await actors_service.stop()
async def test_simple_start_stop(
test_env: _TestEnv,
fake_time: time_machine.Coordinates,
) -> None:
"""Test behavior when receiving start/stop messages."""
now = _now()
duration = timedelta(minutes=10)
dispatch = test_env.generator.generate_dispatch()
dispatch = replace(
dispatch,
id=1,
active=True,
dry_run=False,
duration=duration,
start_time=now,
payload={"test": True},
type="UNIT_TEST",
recurrence=replace(
dispatch.recurrence,
frequency=Frequency.UNSPECIFIED,
),
)
# Send status update to start actor, expect no DispatchInfo for the start
await test_env.running_status_sender.send(Dispatch(dispatch))
fake_time.shift(timedelta(seconds=1))
await asyncio.sleep(1)
await asyncio.sleep(1)
event = test_env.actor(1).initial_dispatch
assert event.options == {"test": True}
assert event.components == dispatch.target
assert event.dry_run is False
assert test_env.actor(1).is_running is True
fake_time.shift(duration)
await test_env.running_status_sender.send(Dispatch(dispatch))
# Give await actor.stop a chance to run
await asyncio.sleep(1)
# pylint: disable=protected-access
assert 1 not in test_env.actors_service._actors
# pylint: enable=protected-access
async def test_start_failed(
test_env: _TestEnv, fake_time: time_machine.Coordinates
) -> None:
"""Test auto-retry after 60 seconds."""
# pylint: disable=protected-access
test_env.actors_service._actor_factory = MockActor.create_fail
now = _now()
duration = timedelta(minutes=10)
dispatch = test_env.generator.generate_dispatch()
dispatch = replace(
dispatch,
id=1,
active=True,
dry_run=False,
duration=duration,
start_time=now,
payload={"test": True},
type="UNIT_TEST",
recurrence=replace(
dispatch.recurrence,
frequency=Frequency.UNSPECIFIED,
),
)
# Send status update to start actor, expect no DispatchInfo for the start
await test_env.running_status_sender.send(Dispatch(dispatch))
fake_time.shift(timedelta(seconds=1))
# Replace failing mock actor factory with a working one
test_env.actors_service._actor_factory = MockActor.create
# Give retry task time to start
await asyncio.sleep(1)
fake_time.shift(timedelta(seconds=65))
await asyncio.sleep(65)
assert test_env.actor(1).is_running is True
def test_heapq_dispatch_compare(test_env: _TestEnv) -> None:
"""Test that the heapq compare function works."""
dispatch1 = test_env.generator.generate_dispatch()
dispatch2 = test_env.generator.generate_dispatch()
# Simulate two dispatches with the same 'until' time
now = datetime.now(timezone.utc)
until_time = now + timedelta(minutes=5)
# Create the heap
scheduled_events: list[DispatchScheduler.QueueItem] = []
# Push two events with the same 'until' time onto the heap
heapq.heappush(
scheduled_events,
DispatchScheduler.QueueItem(until_time, Dispatch(dispatch1), True),
)
heapq.heappush(
scheduled_events,
DispatchScheduler.QueueItem(until_time, Dispatch(dispatch2), True),
)
def test_heapq_dispatch_start_stop_compare(test_env: _TestEnv) -> None:
"""Test that the heapq compare function works."""
dispatch1 = test_env.generator.generate_dispatch()
dispatch2 = test_env.generator.generate_dispatch()
# Simulate two dispatches with the same 'until' time
now = datetime.now(timezone.utc)
until_time = now + timedelta(minutes=5)
# Create the heap
scheduled_events: list[DispatchScheduler.QueueItem] = []
# Push two events with the same 'until' time onto the heap
heapq.heappush(
scheduled_events,
DispatchScheduler.QueueItem(until_time, Dispatch(dispatch1), stop_event=False),
)
heapq.heappush(
scheduled_events,
DispatchScheduler.QueueItem(until_time, Dispatch(dispatch2), stop_event=True),
)
assert scheduled_events[0].dispatch_id == dispatch1.id
assert scheduled_events[1].dispatch_id == dispatch2.id
async def test_dry_run(test_env: _TestEnv, fake_time: time_machine.Coordinates) -> None:
"""Test the dry run mode."""
dispatch = test_env.generator.generate_dispatch()
dispatch = replace(
dispatch,
id=1,
dry_run=True,
active=True,
start_time=_now(),
duration=timedelta(minutes=10),
type="UNIT_TEST",
recurrence=replace(
dispatch.recurrence,
frequency=Frequency.UNSPECIFIED,
),
)
await test_env.running_status_sender.send(Dispatch(dispatch))
fake_time.shift(timedelta(seconds=1))
await asyncio.sleep(1)
event = test_env.actor(1).initial_dispatch
assert event.dry_run is dispatch.dry_run
assert event.components == dispatch.target
assert event.options == dispatch.payload
assert test_env.actor(1).is_running is True
assert dispatch.duration is not None
fake_time.shift(dispatch.duration)
await test_env.running_status_sender.send(Dispatch(dispatch))
# Give await actor.stop a chance to run
await asyncio.sleep(1)
@pytest.mark.parametrize("strategy", [MergeByTypeTarget(), MergeByType(), None])
async def test_manage_abstraction(
fake_time: time_machine.Coordinates,
generator: DispatchGenerator,
strategy: MergeStrategy | None,
) -> None:
"""Test Dispatcher.start_managing sets up correctly."""
identity: Callable[[Dispatch], int] = (
strategy.identity if strategy else lambda dispatch: dispatch.id
)
class MyFakeClient(FakeClient):
"""Fake client for testing."""
def __init__(self, *, server_url: str, key: str):
assert server_url
assert key
super().__init__()
mid = 1
# Patch `Client` class in Dispatcher with MyFakeClient
with patch("frequenz.dispatch._dispatcher.Client", MyFakeClient):
dispatcher = Dispatcher(
microgrid_id=mid, server_url="grpc://test-url", key="test-key"
)
dispatcher.start()
channel = Broadcast[Dispatch](name="dispatch ready test channel")
sender = channel.new_sender()
async def new_mock_receiver(
_: Dispatcher,
dispatch_type: str,
*,
merge_strategy: MergeStrategy | None,
) -> Receiver[Dispatch]:
assert dispatch_type == "MANAGE_TEST"
assert merge_strategy is strategy
return channel.new_receiver()
with patch(
"frequenz.dispatch._dispatcher.Dispatcher.new_running_state_event_receiver",
new_mock_receiver,
):
await dispatcher.start_managing(
dispatch_type="MANAGE_TEST",
actor_factory=MockActor.create,
merge_strategy=strategy,
)
# pylint: disable=protected-access
assert "MANAGE_TEST" in dispatcher._actor_dispatchers
actor_manager = dispatcher._actor_dispatchers["MANAGE_TEST"]
# pylint: disable=comparison-with-callable
assert actor_manager._actor_factory == MockActor.create
# pylint: enable=comparison-with-callable
dispatch = Dispatch(
replace(
generator.generate_dispatch(),
start_time=_now(),
duration=timedelta(minutes=10),
recurrence=recurrence.RecurrenceRule(),
active=True,
type="MANAGE_TEST",
)
)
fake_time.move_to(dispatch.start_time + timedelta(seconds=1))
assert dispatch.started
# Send a dispatch to start an actor instance
await sender.send(dispatch)
# Give the actor a chance to start
await asyncio.sleep(1)
# Check if actor instance is created
assert identity(dispatch) in actor_manager._actors
async def test_actor_dispatcher_update_isolation(
test_env: _TestEnv,
fake_time: time_machine.Coordinates,
) -> None:
"""Test that updates for one dispatch don't affect other actors of the same type."""
dispatch_type = "ISOLATION_TEST"
start_time = _now()
duration = timedelta(minutes=5)
# Create first dispatch
dispatch1_spec = replace(
test_env.generator.generate_dispatch(),
id=101, # Unique ID
type=dispatch_type,
active=True,
dry_run=False,
start_time=start_time + timedelta(seconds=1), # Stagger start slightly
duration=duration,
payload={"instance": 1},
recurrence=RecurrenceRule(),
)
dispatch1 = Dispatch(dispatch1_spec)
# Create second dispatch of the same type, different ID
dispatch2_spec = replace(
test_env.generator.generate_dispatch(),
id=102, # Unique ID
type=dispatch_type, # Same type
active=True,
dry_run=False,
start_time=start_time + timedelta(seconds=2), # Stagger start slightly
duration=duration,
payload={"instance": 2},
recurrence=RecurrenceRule(),
)
dispatch2 = Dispatch(dispatch2_spec)
# Send dispatch 1 to start actor 1
# print(f"Sending dispatch 1: {dispatch1}")
await test_env.running_status_sender.send(dispatch1)
fake_time.shift(timedelta(seconds=1.1)) # Move time past dispatch1 start
await asyncio.sleep(0.1) # Allow actor to start
assert test_env.is_running(101), "Actor 1 should be running"
actor1 = test_env.actor(101)
assert actor1 is not None
# pylint: disable-next=protected-access
assert actor1.initial_dispatch._src.id == 101
assert actor1.initial_dispatch.options == {"instance": 1}
assert not test_env.is_running(102), "Actor 2 should not be running yet"
# Send dispatch 2 to start actor 2
# print(f"Sending dispatch 2: {dispatch2}")
await test_env.running_status_sender.send(dispatch2)
fake_time.shift(timedelta(seconds=1)) # Move time past dispatch2 start
await asyncio.sleep(0.1) # Allow actor to start
assert test_env.actor(101).is_running, "Actor 1 should still be running"
assert test_env.actor(102).is_running, "Actor 2 should now be running"
actor2 = test_env.actor(102)
assert actor2 is not None
# pylint: disable-next=protected-access
assert actor2.initial_dispatch._src.id == 102
assert actor2.initial_dispatch.options == {"instance": 2}
# Now, send an update to stop dispatch 1
dispatch1_stop = Dispatch(
replace(dispatch1_spec, duration=timedelta(seconds=1), active=False)
)
# print(f"Sending stop for dispatch 1: {dispatch1_stop}")
await test_env.running_status_sender.send(dispatch1_stop)
await asyncio.sleep(0.1) # Allow ActorDispatcher to process the stop
# THE CORE ASSERTION: Actor 1 should stop, Actor 2 should remain running
# pylint: disable=protected-access
assert (
101 not in test_env.actors_service._actors
), "Actor 1 should have been removed"
# pylint: enable=protected-access
assert (
test_env.actor(102).is_running is True
), "Actor 2 should be running after Actor 1 stopped"
# Double check actor1 object state if needed (though removal is stronger check)
# assert not actor1.is_running
# Cleanup: Stop Actor 2
dispatch2_stop = Dispatch(replace(dispatch2_spec, active=False))
# print(f"Sending stop for dispatch 2: {dispatch2_stop}")
await test_env.running_status_sender.send(dispatch2_stop)
await asyncio.sleep(0.1) # Allow ActorDispatcher to process the stop
# pylint: disable=protected-access
assert (
102 not in test_env.actors_service._actors
), "Actor 2 should have been removed"
# pylint: enable=protected-access
assert not test_env.is_running(102), "Actor 2 should be stopped"