def test_forked_actor_in_actor(self): ''' test_forked_gen_actors.test_actors_actor_in_actor ''' test_name = 'test_forked_gen_actors.test_forked_actor_in_actor' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = ForkedGenActor(name='Parent') logger.debug('%s' % parent) parent.add_child(ForkedGenActor(name='Child-1')) parent.add_child(ForkedGenActor(name='Child-2')) logger.debug('%s, children: %s' % (parent, [str(child) for child in parent.children])) parent.start() logger.debug('%s, actor started' % parent) while parent.processing: logger.debug('%s, actor is processing' % parent) time.sleep(0.1) parent.stop() logger.debug('%s, actor stopped' % parent) result = [] while True: try: result.append(parent.inbox.get()) except EmptyInboxException: break self.assertEqual(len(result), 20) self.assertEqual(parent.processing, False) self.assertEqual(parent.waiting, False)
def test_stop_in_the_middle(self): ''' test_forked_gen_actors.test_stop_in_the_middle ''' test_name = 'test_forked_gen_actors.test_stop_in_the_middle' logger = file_logger(test_name, filename='logs/%s.log' % test_name) actor = LongRunningActor() logger.debug('LongRunningActor-%d' % id(actor)) actor.start() self.assertEqual(actor.processing, True) logger.debug('timeout started') time.sleep(0.5) logger.debug('timeout stopped') logger.debug('actor stop') actor.stop() logger.debug('actor stopped') result = [] while True: try: result.append(actor.inbox.get()) except EmptyInboxException: break self.assertGreater(result, 0) self.assertEqual(actor.processing, False) self.assertEqual(actor.waiting, False)
def test_find_child_by_actor_names(self): ''' test_actors.test_find_child_by_actor_names ''' class TestActor(pyactors.Actor): pass test_name = 'test_actors.test_child_by_actor_names' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = pyactors.Actor() children = [TestActor() for _ in range(10)] for actor in children: parent.add_child(actor) self.assertEqual(len(parent.find(actor_name='TestActor')), 10) for actor in children: parent.remove_child(actor.address) # name is defined parent = pyactors.Actor(name='Parent') children = [TestActor(name='Child-TestActor') for _ in range(10)] for actor in children: parent.add_child(actor) self.assertEqual(len(parent.find(actor_name='Child-TestActor')), 10) for actor in children: parent.remove_child(actor.address)
def test_processing_with_diff_timelife_children(self): ''' test_forked_gen_actors.test_processing_with_diff_timelife_children ''' test_name = 'test_forked_gen_actors.test_processing_with_diff_timelife_children' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = ForkedGenActor() for i in range(5): parent.add_child(TestActor(iters=i)) parent.start() while parent.processing: time.sleep(0.1) parent.stop() result = [] while True: try: result.append(parent.inbox.get()) except EmptyInboxException: break self.assertEqual(result, [0,0,0,0,1,1,1,3,3,6]) self.assertEqual(len(result), 10) self.assertEqual(parent.processing, False) self.assertEqual(parent.waiting, False)
def test_processing_with_diff_timelife_children(self): ''' test_greenlet_actors.test_processing_with_diff_timelife_children ''' test_name = 'test_greenlet_actors.test_processing_with_diff_timelife_children' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = TestActor(name='ParentActor') for i in range(5): parent.add_child(TestActor(name='ChildActor-%i' % i, iters=i)) parent.start() parent.run() parent.stop() result = [] while True: try: result.append(parent.inbox.get()) except EmptyInboxException: break self.assertEqual(result, [0,0,0,0,1,1,1,3,3,6]) self.assertEqual(len(result), 10) self.assertEqual(parent.run_once(), False) self.assertEqual(parent.processing, False) self.assertEqual(parent.waiting, False)
def test_send_msg_between_forked_actors(self): ''' test_forked_gen_actors.test_send_msg_between_forked_actors ''' test_name = 'test_forked_gen_actors.test_send_msg_between_forked_actors' logger = file_logger(test_name, filename='logs/%s.log' % test_name) # TODO '''
def test_loop_not_implemented(self): ''' test_loop_not_implemented ''' test_name = 'test_actors.test_loop_not_implemented' logger = file_logger(test_name, filename='logs/%s.log' % test_name) actor = pyactors.Actor() self.assertRaises(RuntimeError, actor.loop)
def test_create(self): ''' test_actors.test_create ''' test_name = 'test_actors.test_create' logger = file_logger(test_name, filename='logs/%s.log' % test_name) actor = pyactors.Actor() self.assertFalse(actor.processing) self.assertIsNotNone(actor)
def test_set_waiting_flag(self): ''' test_threaded_actors.test_set_waiting_flag ''' test_name = 'test_threaded_actors.test_set_waiting_flag' logger = file_logger(test_name, filename='logs/%s.log' % test_name) actor = ThreadedActor() actor.waiting = True self.assertEqual(actor.waiting, True)
def test_remove_non_existing_actor(self): ''' test_remove_non_existing_actor ''' test_name = 'test_actors.test_remove_non_existing_actor' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = pyactors.Actor() child = pyactors.Actor() self.assertRaises(RuntimeError, parent.remove_child, child.address)
def test_address(self): ''' test_actors.test_address ''' test_name = 'test_actors.test_address' logger = file_logger(test_name, filename='logs/%s.log' % test_name) actor = pyactors.Actor() self.assertNotEqual(actor.address, None) self.assertTrue(type(actor.address) == str) self.assertEqual(len(actor.address), 32)
def test_incorrect_waiting_value_set(self): ''' test_threaded_actors.test_incorrect_waiting_value_set ''' test_name = 'test_threaded_actors.test_incorrect_waiting_value_set' logger = file_logger(test_name, filename='logs/%s.log' % test_name) actor = ThreadedActor() try: actor.waiting = 1 except RuntimeError: pass
def test_find_child_by_actor_class(self): ''' test_actors.test_find_child_by_actor_class ''' test_name = 'test_actors.test_find_child_by_actor_class' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = pyactors.Actor() child = pyactors.Actor() parent.add_child(child) self.assertEqual(len(parent.find(actor_class=pyactors.Actor)), 1) parent.remove_child(child.address)
def test_send_msg_between_actors(self): ''' test_generator_actors.test_send_msg_between_actors ''' test_name = 'test_generator_actors.test_send_msg_between_actors' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = GeneratorActor() parent.add_child(Sender(name='Sender')) parent.add_child(Receiver(name='Receiver')) parent.start() parent.run() self.assertEqual(parent.inbox.get(), 'message from sender')
def test_run(self): ''' test_threaded_actors.test_run ''' test_name = 'test_threaded_actors.test_run' logger = file_logger(test_name, filename='logs/%s.log' % test_name) actor = ThreadedActor() actor.start() while actor.processing: time.sleep(0.1) self.assertEqual(actor.result, 45) self.assertEqual(actor.processing, False) self.assertEqual(actor.waiting, False)
def test_stop_in_the_middle(self): ''' test_threaded_actors.test_stop_in_the_middle ''' test_name = 'test_threaded_actors.test_stop_in_the_middle' logger = file_logger(test_name, filename='logs/%s.log' % test_name) actor = LongRunningActor() actor.start() self.assertEqual(actor.processing, True) time.sleep(0.1) actor.stop() self.assertGreater(actor.result, 0) self.assertEqual(actor.processing, False) self.assertEqual(actor.waiting, False)
def test_send_msg_between_actors(self): ''' test_threaded_actors.test_send_msg_between_actors ''' test_name = 'test_threaded_actors.test_send_msg_between_actors' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = ThreadedActor() parent.add_child(SenderActor(name='Sender')) parent.add_child(ReceiverActor(name='Receiver')) parent.start() while parent.processing: time.sleep(0.1) parent.stop() self.assertEqual(parent.inbox.get(), 'message from sender')
def test_find_child_by_address_list(self): ''' test_actors.test_find_child_by_address_list ''' test_name = 'test_actors.test_find_child_by_address_list' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = pyactors.Actor() children = [pyactors.Actor() for _ in range(10)] addresses = list() for actor in children: addresses.append(actor.address) parent.add_child(actor) self.assertEqual(len(parent.find(address=addresses)), 10) for actor in children: parent.remove_child(actor.address)
def test_add_remove_child(self): ''' test_actors.test_add_remove_child ''' test_name = 'test_actors.test_add_remove_child' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = pyactors.Actor() parent.add_child(pyactors.Actor()) parent.add_child(pyactors.Actor()) parent.add_child(pyactors.Actor()) self.assertEqual(len(parent.children), 3) for actor in parent.children: parent.remove_child(actor.address) self.assertEqual(len(parent.children), 0)
def test_find_childs_of_grandparents(self): ''' test_actors.test_find_childs_of_grandparents ''' test_name = 'test_actors.test_find_childs_of_grandparents' logger = file_logger(test_name, filename='logs/%s.log' % test_name) grandparent = pyactors.Actor(name='grandparent') for _ in range(3): grandparent.add_child(pyactors.Actor(name='parent')) for parent in grandparent.children: for _ in range(2): parent.add_child(pyactors.Actor(name='child')) self.assertEqual(len(grandparent.children[0].children[0].find(actor_name='grandparent')), 1) self.assertEqual(len(grandparent.children[0].children[0].find(actor_name='parent')), 3) self.assertEqual(len(grandparent.children[0].children[0].find(actor_name='child')), 2)
def test_send_msg_between_threaded_actors(self): ''' test_threaded_actors.test_send_msg_between_threaded_actors ''' test_name = 'test_threaded_actors.test_send_msg_between_threaded_actors' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = TestActor() parent.add_child(ThreadedSenderActor(name='Sender')) parent.add_child(ThreadedReceiverActor(name='Receiver')) parent.start() parent.run() parent.stop() self.assertEqual( [actor.message for actor in parent.find(actor_name='Receiver')], ['message from sender'] )
def test_threaded_actor_in_actor(self): ''' test_threaded_actors.test_threaded_actor_in_actor ''' test_name = 'test_threaded_actors.test_threaded_actor_in_actor' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = ThreadedActor() parent.add_child(ThreadedActor()) parent.add_child(ThreadedActor()) parent.start() while parent.processing: time.sleep(0.1) parent.stop() self.assertEqual([child.result for child in parent.children], [45,45]) self.assertEqual(parent.processing, False) self.assertEqual(parent.waiting, False)
def test_send_msg_between_threaded_actors_in_thread(self): ''' test_threaded_actors.test_send_msg_between_threaded_actors_in_thread ''' test_name = 'test_threaded_actors.test_send_msg_between_threaded_actors_in_thread' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = ThreadedActor() parent.add_child(ThreadedSenderActor(name='Sender')) parent.add_child(ThreadedReceiverActor(name='Receiver')) parent.start() while parent.processing: time.sleep(0.1) parent.stop() self.assertEqual( [actor.message for actor in parent.find(actor_name='Receiver')], ['message from sender'] )
def test_find_child_by_actor_name(self): ''' test_actors.test_find_child_by_actor_name ''' test_name = 'test_actors.test_find_child_by_actor_name' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = pyactors.Actor() child = pyactors.Actor() parent.add_child(child) self.assertEqual(len(parent.find(actor_name='Actor')), 1) parent.remove_child(child.address) # name is defined parent = pyactors.Actor(name='Parent') child = pyactors.Actor(name='Child') parent.add_child(child) self.assertEqual(len(parent.find(actor_name='Child')), 1) parent.remove_child(child.address)
def test_run(self): ''' test_forked_green_actors.test_run ''' test_name = 'test_forked_gen_actors.test_run' logger = file_logger(test_name, filename='logs/%s.log' % test_name) actor = TestActor() actor.start() while actor.processing: time.sleep(0.1) actor.stop() result = [] while True: try: result.append(actor.inbox.get()) except EmptyInboxException: break self.assertEqual(len(result), 10) self.assertEqual(actor.processing, False) self.assertEqual(actor.waiting, False)
def test_run(self): ''' test_greenlet_actors.test_run ''' test_name = 'test_greenlet_actors.test_run' logger = file_logger(test_name, filename='logs/%s.log' % test_name) actor = TestActor() actor.start() self.assertEqual(actor.processing, True) actor.run() result = [] while True: try: result.append(actor.inbox.get()) except EmptyInboxException: break self.assertEqual(len(result), 10) self.assertEqual(actor.run_once(), False) self.assertEqual(actor.processing, False) self.assertEqual(actor.waiting, False)
def test_generator_and_greenlet(self): ''' test_actors_zoo.test_generator_and_greenlet ''' test_name = 'test_actors_zoo.test_generator_and_greenlet' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = ActorGe(name='parent') parent.add_child(ActorGe(name='child-actor-ge-1')) parent.add_child(ActorGr(name='child-actor-gr-2')) parent.add_child(ActorGe(name='child-actor-ge-3')) parent.add_child(ActorGe(name='child-actor-ge-4')) parent.add_child(ActorGr(name='child-actor-gr-5')) parent.start() parent.run() parent.stop() result = [] while True: try: result.append(parent.inbox.get()) except EmptyInboxException: break self.assertEqual(len(result), 50)
def test_processing_with_children(self): ''' test_generator_actors.test_processing_with_children ''' test_name = 'test_generator_actors.test_processing_with_children' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = GeneratorActor() for _ in range(5): parent.add_child(TestActor()) parent.start() parent.run() result = [] while True: try: result.append(parent.inbox.get()) except EmptyInboxException: break self.assertEqual(len(result), 50) self.assertEqual(parent.run_once(), False) self.assertEqual(parent.processing, False) self.assertEqual(parent.waiting, False)
def test_processing_with_children(self): ''' test_threaded_actors.test_processing_with_children ''' test_name = 'test_threaded_actors.test_processing_with_children' logger = file_logger(test_name, filename='logs/%s.log' % test_name) parent = ThreadedActor() for _ in range(5): parent.add_child(TestActor()) parent.start() while parent.processing: time.sleep(0.1) result = [] while True: try: result.append(parent.inbox.get()) except EmptyInboxException: break self.assertEqual(len(result), 50) self.assertEqual(parent.processing, False) self.assertEqual(parent.waiting, False)
def test_stop_in_the_middle(self): ''' test_greenlet_actors.test_stop_in_the_middle ''' test_name = 'test_greenlet_actors.test_stop_in_the_middle' logger = file_logger(test_name, filename='logs/%s.log' % test_name) actor = TestActor() actor.start() self.assertEqual(actor.processing, True) for _ in range(5): actor.run_once() actor.stop() result = [] while True: try: result.append(actor.inbox.get()) except EmptyInboxException: break self.assertGreater(len(result), 1) self.assertEqual(actor.run_once(), False) self.assertEqual(actor.processing, False) self.assertEqual(actor.waiting, False)