<commit_before>'''Tests actor and actor proxies.'''
from multiprocessing.queues import Queue

import pulsar
from pulsar import send, get_actor, CommandNotFound, async_while, TcpServer
from pulsar.utils.pep import pickle, default_timer
from pulsar.apps.test import (unittest, ActorTestMixin, run_on_arbiter,
                              dont_run_with_thread)

from examples.echo.manage import Echo, EchoServerProtocol


def check_actor(actor, name):
    # put something on a queue, just for coverage.
    actor.put(None)
    assert(actor.name==name)
    
class create_echo_server(object):
    '''partial is not picklable in python 2.6'''
    def __init__(self, address):
        self.address = address
        
    def __call__(self, actor):
        '''Starts an echo server on a newly spawn actor'''
        address = self.address
        server = TcpServer(actor.event_loop, address[0], address[1],
                           EchoServerProtocol)
        yield server.start_serving()
        actor.servers['echo'] = server
        actor.extra['echo-address'] = server.address
    

class TestProxy(unittest.TestCase):
    
    def test_get_proxy(self):
        self.assertRaises(ValueError, pulsar.get_proxy, 'shcbjsbcjcdcd')
        self.assertEqual(pulsar.get_proxy('shcbjsbcjcdcd', safe=True), None)

    def test_bad_concurrency(self):
        actor = pulsar.get_actor()
        # bla concurrency does not exists
        self.assertRaises(ValueError, pulsar.concurrency, 'bla', pulsar.Actor,
                          actor, pulsar.Config())

    def test_dummy_proxy(self):
        p = pulsar.concurrency('thread', pulsar.Actor, pulsar.get_actor(),
                               pulsar.Config())
        self.assertEqual(p.mailbox, None)
        self.assertEqual(p.spawning_start, None)
        self.assertEqual(p.stopping_start, None)
        self.assertEqual(p.callback, None)
        self.assertEqual(str(p), 'actor(%s)' % p.aid)

    def testActorCoverage(self):
        '''test case for coverage'''
        actor = pulsar.get_actor()
        d = send('sjdcbhjscbhjdbjsj', 'bla')
        self.assertIsInstance(d.result.error, CommandNotFound)
        self.assertRaises(pickle.PicklingError, pickle.dumps, actor)
        

class TestActorThread(ActorTestMixin, unittest.TestCase):
    concurrency = 'thread'
    
    def test_spawn_actor(self):
        '''Test spawning from actor domain.'''
        proxy = yield self.spawn(name='pippo')
        yield self.assertEqual(proxy.name, 'pippo')
        # The current actor is linked with the actor just spawned

    def test_spawn_and_interact(self):
        proxy = yield self.spawn(name='pluto')
        self.assertEqual(proxy.name, 'pluto')
        yield self.async.assertEqual(send(proxy, 'ping'), 'pong')
        yield self.async.assertEqual(send(proxy, 'echo', 'Hello!'), 'Hello!')
        #yield send(proxy, 'run', check_actor, 'pluto')
        
    def __test_info(self):
        #TODO: this fails in Travis when using python 2.6
        proxy = yield self.spawn(name='pippo')
        self.assertEqual(proxy.name, 'pippo')
        info = yield send(proxy, 'info')
        self.assertTrue('actor' in info)
        ainfo = info['actor']
        self.assertEqual(ainfo['is_process'], self.concurrency=='process')

    @run_on_arbiter
    def testSimpleSpawn(self):
        '''Test start and stop for a standard actor on the arbiter domain.'''
        proxy = yield self.spawn(name='simple-actor-on-%s' % self.concurrency)<commit_msg>test travis 2<commit_after>'''Tests actor and actor proxies.'''
from multiprocessing.queues import Queue

import pulsar
from pulsar import send, get_actor, CommandNotFound, async_while, TcpServer
from pulsar.utils.pep import pickle, default_timer
from pulsar.apps.test import (unittest, ActorTestMixin, run_on_arbiter,
                              dont_run_with_thread)

from examples.echo.manage import Echo, EchoServerProtocol


def check_actor(actor, name):
    # put something on a queue, just for coverage.
    actor.put(None)
    assert(actor.name==name)
    
class create_echo_server(object):
    '''partial is not picklable in python 2.6'''
    def __init__(self, address):
        self.address = address
        
    def __call__(self, actor):
        '''Starts an echo server on a newly spawn actor'''
        address = self.address
        server = TcpServer(actor.event_loop, address[0], address[1],
                           EchoServerProtocol)
        yield server.start_serving()
        actor.servers['echo'] = server
        actor.extra['echo-address'] = server.address
    

class TestProxy(unittest.TestCase):
    
    def test_get_proxy(self):
        self.assertRaises(ValueError, pulsar.get_proxy, 'shcbjsbcjcdcd')
        self.assertEqual(pulsar.get_proxy('shcbjsbcjcdcd', safe=True), None)

    def test_bad_concurrency(self):
        actor = pulsar.get_actor()
        # bla concurrency does not exists
        self.assertRaises(ValueError, pulsar.concurrency, 'bla', pulsar.Actor,
                          actor, pulsar.Config())

    def test_dummy_proxy(self):
        p = pulsar.concurrency('thread', pulsar.Actor, pulsar.get_actor(),
                               pulsar.Config())
        self.assertEqual(p.mailbox, None)
        self.assertEqual(p.spawning_start, None)
        self.assertEqual(p.stopping_start, None)
        self.assertEqual(p.callback, None)
        self.assertEqual(str(p), 'actor(%s)' % p.aid)

    def testActorCoverage(self):
        '''test case for coverage'''
        actor = pulsar.get_actor()
        d = send('sjdcbhjscbhjdbjsj', 'bla')
        self.assertIsInstance(d.result.error, CommandNotFound)
        self.assertRaises(pickle.PicklingError, pickle.dumps, actor)
        

class TestActorThread(ActorTestMixin, unittest.TestCase):
    concurrency = 'thread'
    
    def test_spawn_actor(self):
        '''Test spawning from actor domain.'''
        proxy = yield self.spawn(name='pippo')
        yield self.assertEqual(proxy.name, 'pippo')
        # The current actor is linked with the actor just spawned

    def test_spawn_and_interact(self):
        proxy = yield self.spawn(name='pluto')
        self.assertEqual(proxy.name, 'pluto')
        yield self.async.assertEqual(send(proxy, 'ping'), 'pong')
        yield self.async.assertEqual(send(proxy, 'echo', 'Hello!'), 'Hello!')
        #yield send(proxy, 'run', check_actor, 'pluto')
        
    def test_info(self):
        proxy = yield self.spawn(name='pippo')
        self.assertEqual(proxy.name, 'pippo')
        info = yield send(proxy, 'info')
        self.assertTrue('actor' in info)
        ainfo = info['actor']
        self.assertEqual(ainfo['is_process'], self.concurrency=='process')

    @run_on_arbiter
    def testSimpleSpawn(self):
        '''Test start and stop for a standard actor on the arbiter domain.'''
        proxy = yield self.spawn(name='simple-actor-on-%s' % self.concurrency)
{'fraction_non_alphanumeric': 0.11388568129330254, 'fraction_numerical': 0.0015877598152424942, 'mean_word_length': 2.8731134712129682, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7280125', 'n_tokens_mistral': 2110, 'n_tokens_neox': 2065, 'n_words': 476}