Commits

Ronny Pfannschmidt  committed 71e1566

resuffle

  • Participants
  • Parent commits 36e9c59

Comments (0)

Files changed (3)

File action/actor.py

 from collections import deque
+
+
 class Actor(object):
-
     def handle(self, message):
         raise NotImplementedError
 
 
 class Null(Actor):
     def handle(self, message):
-        return
+        return []
 
 
 class QueueActor(Actor):
                 ]
 
         assert 0
-
-
-
-
-

File action/messages.py

     def reply(self, data):
         return Message(target=self.source, source=self.target, data=data)
 
+
 class MessageQueue(object):
 
     def __init__(self):
     def put(self, message):
         self.messages.append(message)
 
-    def run_once(self):
-
+    def dispatch_one(self):
         message = self.messages.popleft()
         new_messages = message.target.handle(message)
-        if new_messages is not None:
-            self.messages.extend(new_messages)
+        self.messages.extend(new_messages)
 
-    def run_all(self):
+    def run(self):
         while self.messages:
-            self.run_once()
+            self.dispatch_one()
 

File testing/test_messagequeue.py

 from action.messages import MessageQueue, Message
 from action.actor import Actor, Null, QueueActor
 
+
 class RememberingActor(Actor):
     def __init__(self):
         self.store = []
 
     def handle(self, message):
         self.store.append(message)
-
+        return []
 
 
 def test_create():
         data='test')
     queue.put(message)
     assert not actor.store
-    queue.run_once()
+    queue.run()
     message = Message(
         target=actor, source=nul,
         data='test')
     queue.put(message)
     assert not actor.store
-    queue.run_once()
+    queue.run()
     assert actor.store == [message]
 
 
 def test_handle_result():
-    
+
     class ResultActor(Actor):
         def handle(self, message):
             return [message._replace(data=message.data+1)]
-    
+
     queue = MessageQueue()
 
     r = ResultActor()
-    queue.put(Message(source=None,target=r,data=1))
-    queue.run_once()
+    queue.put(Message(source=None, target=r, data=1))
+    queue.dispatch_one()
     assert len(queue.messages) == 1
-    assert queue.messages[0].data ==2
+    assert queue.messages[0].data == 2
 
 
 def test_queue():
     queue.put(mm(data=('put', 'some')))
     queue.put(mm(data=('get', None)))
     queue.put(mm(data=('get', None)))
-    queue.run_all()
-
+    queue.run()
 
     data = [x.data for x in actor.store]
     assert data == [
         ('item', 'some'),
         ('empty', None),
     ]
-
-
-