Exemplo n.º 1
0
def test_exception():
    publisher = Mock()
    man = EventManager('dummy', publisher)
    evt, eid = man.create()

    ret = man.getreturner('dummy', eid)
    ret.send(exc=KeyError())
    assert publisher.send.called
    args, kwargs = publisher.send.call_args
    rid = args[2]['envelopeto']
    man.return_(rid[1], args[2]['data'])
    assert evt.ready()
    evt.wait()
Exemplo n.º 2
0
def test():
    publisher = Mock()
    man = EventManager('dummy', publisher)
    evt, eid = man.create()
    assert not evt.ready()

    ret = man.getreturner('dummy', eid)
    ret.send('foo')
    assert publisher.send.called
    args, kwargs = publisher.send.call_args
    rid = args[2]['envelopeto']
    assert rid[1] == eid, (rid, eid)
    man.return_(rid[1], args[2]['data'])
    assert evt.ready()
    assert evt.wait() == 'foo'
Exemplo n.º 3
0
 def __init__(self, config, httpsockfd, id=None):
     self.config = config
     _sock = socket.fromfd(httpsockfd, socket.AF_INET, socket.SOCK_STREAM)
     self.httpsock = greenio.GreenSocket(_sock)
     self.id = id or uuid.uuid4().hex
     self.broker = self.create_broker()
     self.publisher = Publisher(
         connection=self.broker,
         exchange='messaging',
         exchange_type='topic'
     )
     self.mqueue = coros.queue()
     self.eventmanager = EventManager(self.id,
         Publisher(
             connection=self.broker,
             exchange='return',
             exchange_type='topic',
             durable=False,
             auto_delete=True,
     ))
     self.procs = proc.RunningProcSet()
     self.components = {}
     self.subscriptions = defaultdict(list)
     self._setup_logging()
Exemplo n.º 4
0
class ChildServer(object):
    def __init__(self, config, httpsockfd, id=None):
        self.config = config
        _sock = socket.fromfd(httpsockfd, socket.AF_INET, socket.SOCK_STREAM)
        self.httpsock = greenio.GreenSocket(_sock)
        self.id = id or uuid.uuid4().hex
        self.broker = self.create_broker()
        self.publisher = Publisher(
            connection=self.broker,
            exchange='messaging',
            exchange_type='topic'
        )
        self.mqueue = coros.queue()
        self.eventmanager = EventManager(self.id,
            Publisher(
                connection=self.broker,
                exchange='return',
                exchange_type='topic',
                durable=False,
                auto_delete=True,
        ))
        self.procs = proc.RunningProcSet()
        self.components = {}
        self.subscriptions = defaultdict(list)
        self._setup_logging()

    def create_broker(self):
        brconf = self.config.broker
        return BrokerConnection(
            hostname=brconf.host,
            port=brconf.getint('port'),
            userid=brconf.get('username', None),
            password=brconf.get('password', None),
            virtual_host=brconf.get('vhost', None),
            ssl=brconf.getboolean('ssl', False),
            backend_cls=brconf.get('backend_class', None)
        )

    def _wait(self, consumer, callback):
        with consumer:
            consumer.register_callback(callback)
            for msg in consumer.iterconsume():
                pass

    def _serve_http(self):
        app = create_application(self)
        self.wsgiapp = app
        wsgi.server(self.httpsock, app)

    def _incoming_message(self, payload, msg):
        print 'got message', payload
        subscriber = payload['envelopeto']
        returnid = payload['returnid']
        event = self.eventmanager.getreturner(returnid)
        if subscriber in self.subscriptions:
            for sub in self.subscriptions[subscriber]:
                sub.send((event, payload['data']))
        msg.ack()

    def _incoming_return(self, payload, msg):
        print 'got return', payload
        returnid = payload['envelopeto']
        self.eventmanager.return_(returnid, payload['data'])
        msg.ack()

    def _process_outqueue(self):
        while True:
            to, event, message = self.mqueue.get()
            try:
                self._do_send(to, event, message)
            except Exception,e:
                event.send_exception(e)