Skip to content

Commit 50388c0

Browse files
committed
refactor(actor): Cleanup actor base messages code
1 parent 3ae78f7 commit 50388c0

File tree

10 files changed

+48
-90
lines changed

10 files changed

+48
-90
lines changed

src/powerapi/actor/actor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,13 @@ def soft_kill(self):
286286
<powerapi.message.message.PoisonPillMessage>`
287287
288288
"""
289-
self.send_control(PoisonPillMessage(soft=True, sender_name='system'))
289+
self.send_control(PoisonPillMessage())
290290
self.socket_interface.close()
291291

292292
def hard_kill(self):
293293
"""Kill this actor by sending a hard :class:`PoisonPillMessage
294294
<powerapi.message.message.PoisonPillMessage>`
295295
296296
"""
297-
self.send_control(PoisonPillMessage(soft=False, sender_name='system'))
297+
self.send_control(PoisonPillMessage(soft=False))
298298
self.socket_interface.close()

src/powerapi/actor/supervisor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def launch_actor(self, actor, start_message=True):
107107
actor.connect_data()
108108

109109
if start_message:
110-
actor.send_control(StartMessage(SUPERVISOR_NAME))
110+
actor.send_control(StartMessage())
111111
msg = actor.receive_control(2000)
112112
if isinstance(msg, ErrorMessage):
113113
raise ActorInitError(msg.error_message)

src/powerapi/handler/start_handler.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ def handle(self, msg):
4444
:param powerapi.StartMessage msg: Message that initialize the actor
4545
"""
4646
if self.state.initialized:
47-
self.state.actor.send_control(
48-
ErrorMessage(self.state.actor.name, 'Actor already initialized'))
47+
self.state.actor.send_control(ErrorMessage('Actor already initialized'))
4948
return
5049

5150
if not isinstance(msg, StartMessage):
@@ -55,7 +54,7 @@ def handle(self, msg):
5554

5655
if self.state.alive:
5756
self.state.initialized = True
58-
self.state.actor.send_control(OKMessage(self.state.actor.name))
57+
self.state.actor.send_control(OKMessage())
5958

6059
def initialization(self):
6160
"""

src/powerapi/message.py

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -30,82 +30,41 @@
3030

3131
class Message:
3232
"""
33-
Abstract Message class
33+
Abstract actor Message class.
3434
"""
3535

36-
def __init__(self, sender_name: str):
37-
self.sender_name = sender_name
38-
39-
def __str__(self):
40-
raise NotImplementedError()
41-
4236

4337
class OKMessage(Message):
4438
"""
45-
Message sends to acknowledge last received message
39+
Message sent by an actor after a successful startup.
4640
"""
4741

48-
def __init__(self, sender_name: str):
49-
Message.__init__(self, sender_name)
50-
51-
def __str__(self):
52-
return "OKMessage"
53-
5442

5543
class ErrorMessage(Message):
5644
"""
57-
Message used to indicate that an error as occuried
45+
Message sent by an actor when an error occurs during startup.
5846
"""
5947

60-
def __init__(self, sender_name: str, error_message: str):
48+
def __init__(self, error_message: str):
6149
"""
62-
:param str error_code: message associated to the error
50+
:param error_message: Message associated to the encountered error.
6351
"""
64-
Message.__init__(self, sender_name)
6552
self.error_message = error_message
6653

67-
def __str__(self):
68-
return "ErrorMessage : " + self.error_message
69-
7054

7155
class StartMessage(Message):
7256
"""
73-
Message that asks the actor to launch its initialisation process
57+
Message sent to an actor to initiate its startup.
7458
"""
7559

76-
def __init__(self, sender_name: str):
77-
Message.__init__(self, sender_name)
78-
79-
def __str__(self):
80-
return "StartMessage"
81-
82-
83-
class EndMessage(Message):
84-
"""
85-
Message sent by actor to its parent when it terminates itself
86-
"""
87-
88-
def __init__(self, sender_name: str):
89-
Message.__init__(self, sender_name)
90-
91-
def __str__(self):
92-
return "EndMessage"
93-
9460

9561
class PoisonPillMessage(Message):
9662
"""
97-
Message which allow to kill an actor
63+
Message sent to an actor to initiate its shutdown.
9864
"""
9965

100-
def __init__(self, soft: bool = True, sender_name: str = ''):
101-
Message.__init__(self, sender_name=sender_name)
66+
def __init__(self, soft: bool = True):
67+
"""
68+
:param bool soft: Indicate whether the actor should process all its messages before shutting down.
69+
"""
10270
self.is_soft = soft
103-
self.is_hard = not soft
104-
105-
def __str__(self):
106-
return "PoisonPillMessage"
107-
108-
def __eq__(self, other):
109-
if isinstance(other, PoisonPillMessage):
110-
return other.is_soft == self.is_soft and other.is_hard == self.is_hard
111-
return False

src/powerapi/puller/handlers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def run(self):
7676
dispatcher.send_data(raw_report)
7777

7878
except FilterUselessError:
79-
self.handler.handle_internal_msg(PoisonPillMessage(False, self.name))
79+
self.handler.handle_internal_msg(PoisonPillMessage(False))
8080
return
8181

8282
except BadInputData as exn:
@@ -86,7 +86,7 @@ def run(self):
8686
except StopIteration:
8787
time.sleep(self.state.timeout_puller / 1000)
8888
if not self.state.stream_mode:
89-
self.handler.handle_internal_msg(PoisonPillMessage(False, self.name))
89+
self.handler.handle_internal_msg(PoisonPillMessage(False))
9090
return
9191

9292

@@ -141,11 +141,11 @@ def handle(self, msg: Message):
141141
try:
142142
StartHandler.handle(self, msg)
143143
except PullerInitializationException as e:
144-
self.state.actor.send_control(ErrorMessage(self.state.actor.name, e.msg))
144+
self.state.actor.send_control(ErrorMessage(e.msg))
145145

146146
self.pull_db()
147147

148-
self.handle_internal_msg(PoisonPillMessage(False, self.state.actor.name))
148+
self.handle_internal_msg(PoisonPillMessage(False))
149149

150150
def pull_db(self):
151151
"""
@@ -166,4 +166,4 @@ def _connect_database(self):
166166
self.state.database_it = self.state.database.iter(self.state.stream_mode)
167167
except DBError as error:
168168
self.state.alive = False
169-
self.state.actor.send_control(ErrorMessage(self.state.actor.name, error.msg))
169+
self.state.actor.send_control(ErrorMessage(error.msg))

src/powerapi/pusher/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def initialization(self):
4747
try:
4848
self.state.database.connect()
4949
except DBError as error:
50-
self.state.actor.send_control(ErrorMessage(self.state.actor.name, error.msg))
50+
self.state.actor.send_control(ErrorMessage(error.msg))
5151
self.state.alive = False
5252

5353

tests/unit/actor/abstract_test_actor.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def pusher(database):
152152
actor.connect_control()
153153

154154
yield actor
155-
actor.send_control(PoisonPillMessage(sender_name='system-test'))
155+
actor.send_control(PoisonPillMessage())
156156
if actor.is_alive():
157157
actor.terminate()
158158
actor.socket_interface.close()
@@ -168,7 +168,7 @@ def start_actor(actor: Actor):
168168
actor.start()
169169
actor.connect_control()
170170
actor.connect_data()
171-
actor.send_control(StartMessage('system'))
171+
actor.send_control(StartMessage())
172172
_ = actor.receive_control(2000)
173173

174174

@@ -263,7 +263,7 @@ def init_actor(actor):
263263
@staticmethod
264264
@pytest.fixture
265265
def started_actor(init_actor):
266-
init_actor.send_control(StartMessage('test_case'))
266+
init_actor.send_control(StartMessage())
267267
_ = init_actor.receive_control(2000)
268268
yield init_actor
269269

@@ -330,7 +330,7 @@ def actor_with_crash_handler(actor):
330330
@staticmethod
331331
@pytest.fixture
332332
def started_actor_with_crash_handler(actor_with_crash_handler):
333-
actor_with_crash_handler.send_control(StartMessage(SENDER_NAME))
333+
actor_with_crash_handler.send_control(StartMessage())
334334
_ = actor_with_crash_handler.receive_control(2000)
335335
return actor_with_crash_handler
336336

@@ -342,22 +342,22 @@ def test_new_actor_is_alive(init_actor):
342342
@staticmethod
343343
@pytest.mark.usefixtures("shutdown_system")
344344
def test_send_PoisonPillMessage_set_actor_alive_to_False(init_actor):
345-
init_actor.send_control(PoisonPillMessage(sender_name='system-abstract-test'))
345+
init_actor.send_control(PoisonPillMessage())
346346
time.sleep(0.1)
347347
assert not init_actor.is_alive()
348348

349349
@staticmethod
350350
@pytest.mark.usefixtures("shutdown_system")
351351
def test_send_StartMessage_answer_OkMessage(init_actor):
352-
init_actor.send_control(StartMessage(SENDER_NAME))
352+
init_actor.send_control(StartMessage())
353353
msg = init_actor.receive_control(2000)
354354
print('message start', str(msg))
355355
assert isinstance(msg, OKMessage)
356356

357357
@staticmethod
358358
@pytest.mark.usefixtures("shutdown_system")
359359
def test_send_StartMessage_to_already_started_actor_answer_ErrorMessage(started_actor):
360-
started_actor.send_control(StartMessage(SENDER_NAME))
360+
started_actor.send_control(StartMessage())
361361
msg = started_actor.receive_control(2000)
362362
assert isinstance(msg, ErrorMessage)
363363
assert msg.error_message == 'Actor already initialized'
@@ -366,13 +366,13 @@ def test_send_StartMessage_to_already_started_actor_answer_ErrorMessage(started_
366366
@pytest.mark.usefixtures("shutdown_system")
367367
def test_send_message_on_data_canal_to_non_initialized_actor_raise_NotConnectedException(actor):
368368
with pytest.raises(NotConnectedException):
369-
actor.send_data(StartMessage(SENDER_NAME))
369+
actor.send_data(StartMessage())
370370

371371
@staticmethod
372372
@pytest.mark.usefixtures("shutdown_system")
373373
def test_send_message_on_control_canal_to_non_initialized_actor_raise_NotConnectedException(actor):
374374
with pytest.raises(NotConnectedException):
375-
actor.send_control(StartMessage(SENDER_NAME))
375+
actor.send_control(StartMessage())
376376

377377
@staticmethod
378378
@pytest.mark.usefixtures("shutdown_system")
@@ -401,7 +401,7 @@ def test_if_actor_behaviour_raise_fatal_exception_the_actor_must_be_killed(actor
401401
@staticmethod
402402
@pytest.mark.usefixtures("shutdown_system")
403403
def test_starting_actor_with_a_no_StartMessage_does_no_change_initialized(init_actor):
404-
init_actor.send_control(ErrorMessage('system', 'error test'))
404+
init_actor.send_control(ErrorMessage('error test'))
405405
assert not init_actor.state.initialized
406406

407407

@@ -451,7 +451,7 @@ def init_actor_with_db(actor_with_db):
451451
@staticmethod
452452
@pytest.fixture
453453
def started_actor_with_db(init_actor_with_db, fake_db):
454-
init_actor_with_db.send_control(StartMessage('test_case'))
454+
init_actor_with_db.send_control(StartMessage())
455455
_ = init_actor_with_db.receive_control(2000)
456456
yield init_actor_with_db
457457

@@ -460,7 +460,7 @@ def started_actor_with_db(init_actor_with_db, fake_db):
460460
@staticmethod
461461
@pytest.mark.usefixtures("shutdown_system")
462462
def started_actor_with_crash_handler(actor_with_db_and_crash_handler):
463-
actor_with_db_and_crash_handler.send_control(StartMessage(SENDER_NAME))
463+
actor_with_db_and_crash_handler.send_control(StartMessage())
464464
_ = actor_with_db_and_crash_handler.receive_control(2000)
465465
return actor_with_db_and_crash_handler
466466

@@ -472,22 +472,22 @@ def test_new_actor_is_alive(init_actor_with_db):
472472
@staticmethod
473473
@pytest.mark.usefixtures("shutdown_system")
474474
def test_send_PoisonPillMessage_set_actor_alive_to_False(init_actor_with_db):
475-
init_actor_with_db.send_control(PoisonPillMessage(sender_name='system-abstract-test'))
475+
init_actor_with_db.send_control(PoisonPillMessage())
476476
time.sleep(0.1)
477477
assert not init_actor_with_db.is_alive()
478478

479479
@staticmethod
480480
@pytest.mark.usefixtures("shutdown_system")
481481
def test_send_StartMessage_answer_OkMessage(init_actor_with_db):
482-
init_actor_with_db.send_control(StartMessage(SENDER_NAME))
482+
init_actor_with_db.send_control(StartMessage())
483483
msg = init_actor_with_db.receive_control(2000)
484484
print('message start', str(msg))
485485
assert isinstance(msg, OKMessage)
486486

487487
@staticmethod
488488
@pytest.mark.usefixtures("shutdown_system")
489489
def test_send_StartMessage_to_already_started_actor_answer_ErrorMessage(started_actor_with_db):
490-
started_actor_with_db.send_control(StartMessage(SENDER_NAME))
490+
started_actor_with_db.send_control(StartMessage())
491491
msg = started_actor_with_db.receive_control(2000)
492492
assert isinstance(msg, ErrorMessage)
493493
assert msg.error_message == 'Actor already initialized'
@@ -496,13 +496,13 @@ def test_send_StartMessage_to_already_started_actor_answer_ErrorMessage(started_
496496
@pytest.mark.usefixtures("shutdown_system")
497497
def test_send_message_on_data_canal_to_non_initialized_actor_raise_NotConnectedException(actor_with_db):
498498
with pytest.raises(NotConnectedException):
499-
actor_with_db.send_data(StartMessage(SENDER_NAME))
499+
actor_with_db.send_data(StartMessage())
500500

501501
@staticmethod
502502
@pytest.mark.usefixtures("shutdown_system")
503503
def test_send_message_on_control_canal_to_non_initialized_actor_raise_NotConnectedException(actor_with_db):
504504
with pytest.raises(NotConnectedException):
505-
actor_with_db.send_control(StartMessage(SENDER_NAME))
505+
actor_with_db.send_control(StartMessage())
506506

507507
@staticmethod
508508
@pytest.mark.usefixtures("shutdown_system")
@@ -531,5 +531,5 @@ def test_if_actor_behaviour_raise_fatal_exception_the_actor_must_be_killed(init_
531531
@staticmethod
532532
@pytest.mark.usefixtures("shutdown_system")
533533
def test_starting_actor_with_a_no_StartMessage_does_no_change_initialized(init_actor_with_db):
534-
init_actor_with_db.send_control(ErrorMessage('system', 'error test'))
534+
init_actor_with_db.send_control(ErrorMessage('error test'))
535535
assert not init_actor_with_db.state.initialized

tests/unit/actor/test_supervisor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def send_control(self, msg):
6868
self.send_msg.append(msg)
6969

7070
def receive_control(self, timeout=None):
71-
return OKMessage("FakeActor")
71+
return OKMessage()
7272

7373

7474
class FakeActorConnectError(FakeActor):
@@ -86,7 +86,7 @@ class FakeActorInitError(FakeActor):
8686
"""
8787

8888
def receive_control(self, timeout=None):
89-
return ErrorMessage('error', 'FakeActorConnectError')
89+
return ErrorMessage('FakeActorConnectError')
9090

9191

9292
############

tests/unit/dispatcher/test_dispatcher_actor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def started_actor(init_actor):
319319
"""
320320
Fixture for starting a DispatcherActor.
321321
"""
322-
init_actor.send_control(StartMessage('test_case'))
322+
init_actor.send_control(StartMessage())
323323
_ = init_actor.receive_control(2000)
324324
yield init_actor
325325

@@ -481,7 +481,7 @@ def test_send_PoisonPillMessage_make_dispatcher_forward_it_to_formula(dispatcher
481481
"""
482482
Check that a PoissonPillMessage stops the DispatcherActor as well as their formulas
483483
"""
484-
dispatcher_with_two_formula.send_control(PoisonPillMessage(True, 'system-test-dispatcher'))
484+
dispatcher_with_two_formula.send_control(PoisonPillMessage())
485485

486486
sleep(2)
487487

0 commit comments

Comments
 (0)