コード例 #1
0
 def test_oid_consistency(self):
     """Tests that oid is consistent in single process"""
     m1 = pidbox.Mailbox('mailbox1')
     m2 = pidbox.Mailbox('mailbox2')
     assert m1.oid == m1.oid
     assert m2.oid == m2.oid
     assert m1.oid != m2.oid
コード例 #2
0
    def test_reply__collect(self):
        mailbox = pidbox.Mailbox('test_reply__collect')(self.connection)
        exchange = mailbox.reply_exchange.name
        channel = self.connection.channel()
        mailbox.reply_queue(channel).declare()

        ticket = uuid()
        mailbox._publish_reply({'foo': 'bar'}, exchange, mailbox.oid, ticket)
        _callback_called = [False]

        def callback(body):
            _callback_called[0] = True

        reply = mailbox._collect(ticket,
                                 limit=1,
                                 callback=callback,
                                 channel=channel)
        self.assertEqual(reply, [{'foo': 'bar'}])
        self.assertTrue(_callback_called[0])

        ticket = uuid()
        mailbox._publish_reply({'biz': 'boz'}, exchange, mailbox.oid, ticket)
        reply = mailbox._collect(ticket, limit=1, channel=channel)
        self.assertEqual(reply, [{'biz': 'boz'}])

        de = mailbox.connection.drain_events = Mock()
        de.side_effect = socket.timeout
        mailbox._collect(ticket, limit=1, channel=channel)
コード例 #3
0
    def test_reply__collect(self):
        mailbox = pidbox.Mailbox("test_reply__collect")(self.connection)
        exchange = mailbox.reply_exchange.name

        ticket = uuid()
        mailbox.get_reply_queue(ticket)(self.connection.channel()).declare()
        mailbox._publish_reply({"foo": "bar"}, exchange, ticket)
        _callback_called = [False]

        def callback(body):
            _callback_called[0] = True

        channel = self.connection.channel()
        reply = mailbox._collect(ticket,
                                 limit=1,
                                 callback=callback,
                                 channel=channel)
        self.assertEqual(reply, [{"foo": "bar"}])
        self.assertTrue(_callback_called[0])

        ticket = uuid()
        mailbox.get_reply_queue(ticket)(self.connection.channel()).declare()
        mailbox._publish_reply({"biz": "boz"}, exchange, ticket)
        reply = mailbox._collect(ticket, limit=1, channel=channel)
        self.assertEqual(reply, [{"biz": "boz"}])

        de = mailbox.connection.drain_events = Mock()
        de.side_effect = socket.timeout
        mailbox._collect(ticket, limit=1, channel=channel)
コード例 #4
0
 def test_publish_reply_ignores_InconsistencyError(self):
     mailbox = pidbox.Mailbox('test_reply__collect')(self.connection)
     with patch('kombu.pidbox.Producer') as Producer:
         producer = Producer.return_value = Mock(name='producer')
         producer.publish.side_effect = InconsistencyError()
         mailbox._publish_reply(
             {'foo': 'bar'}, mailbox.reply_exchange, mailbox.oid, 'foo',
         )
         producer.publish.assert_called()
コード例 #5
0
    def test_reply__collect(self):
        mailbox = pidbox.Mailbox('test_reply__collect')(self.connection)
        exchange = mailbox.reply_exchange.name
        channel = self.connection.channel()
        mailbox.reply_queue(channel).declare()

        ticket = uuid()
        mailbox._publish_reply({'foo': 'bar'}, exchange, mailbox.oid, ticket)
        _callback_called = [False]

        def callback(body):
            _callback_called[0] = True

        reply = mailbox._collect(ticket,
                                 limit=1,
                                 callback=callback,
                                 channel=channel)
        self.assertEqual(reply, [{'foo': 'bar'}])
        self.assertTrue(_callback_called[0])

        ticket = uuid()
        mailbox._publish_reply({'biz': 'boz'}, exchange, mailbox.oid, ticket)
        reply = mailbox._collect(ticket, limit=1, channel=channel)
        self.assertEqual(reply, [{'biz': 'boz'}])

        mailbox._publish_reply({'foo': 'BAM'},
                               exchange,
                               mailbox.oid,
                               'doom',
                               serializer='pickle')
        with self.assertRaises(ContentDisallowed):
            reply = mailbox._collect('doom', limit=1, channel=channel)
        mailbox._publish_reply(
            {'foo': 'BAMBAM'},
            exchange,
            mailbox.oid,
            'doom',
            serializer='pickle',
        )
        reply = mailbox._collect('doom',
                                 limit=1,
                                 channel=channel,
                                 accept=['pickle'])
        self.assertEqual(reply[0]['foo'], 'BAMBAM')

        de = mailbox.connection.drain_events = Mock()
        de.side_effect = socket.timeout
        mailbox._collect(ticket, limit=1, channel=channel)
コード例 #6
0
    def test__publish_uses_default_channel(self):
        class QueueCalled(Exception):
            pass

        def queue__call__side(channel, *args, **kwargs):
            raise QueueCalled(channel)

        ticket = uuid()
        with patch.object(pidbox.Queue, '__call__') as queue__call__:
            mailbox = pidbox.Mailbox('test_reply__collect')(self.connection)
            queue__call__.side_effect = queue__call__side
            try:
                mailbox._publish(ticket, {}, reply_ticket=ticket)
            except QueueCalled as c:
                assert c.args[0] is not None
            except Exception:
                raise
            else:
                assert False, "Queue not called"
コード例 #7
0
    def test_reply__collect_uses_default_channel(self):
        class ConsumerCalled(Exception):
            pass

        def fake_Consumer(channel, *args, **kwargs):
            raise ConsumerCalled(channel)

        ticket = uuid()
        with patch('kombu.pidbox.Consumer') as Consumer:
            mailbox = pidbox.Mailbox('test_reply__collect')(self.connection)
            assert mailbox.connection.default_channel is not None
            Consumer.side_effect = fake_Consumer
            try:
                mailbox._collect(ticket, limit=1)
            except ConsumerCalled as c:
                assert c.args[0] is not None
            except Exception:
                raise
            else:
                assert False, "Consumer not called"
コード例 #8
0
 def test_broadcast_matcher_pattern_string_type(self):
     mailbox = pidbox.Mailbox("test_matcher_str")(self.connection)
     with pytest.raises(ValueError):
         mailbox._broadcast("ping", pattern=1, matcher=2)
コード例 #9
0
ファイル: node.py プロジェクト: nangedetianxia/blog_codes
# All rights reserved.
"""
    Comment for this files.
    Author: [email protected]
    Created at: 2017-11-14 09:34
    Update: -
"""
import sys

import kombu
from kombu import pidbox

hostname = "localhost"

connection = kombu.Connection("redis://192.168.25.2:6379")
mailbox = pidbox.Mailbox("test", type="direct")
node = mailbox.Node(hostname, state={"a": "b"})
node.channel = connection.channel()


def callback(body, message):
    print(body)
    print(message)


def main(arguments):
    consumer = node.listen(callback=callback)
    try:
        while True:
            print('Consumer Waiting')
            connection.drain_events()
コード例 #10
0
def main(arguments):
    connection = kombu.Connection("redis://192.168.25.2:6379")
    mailbox = pidbox.Mailbox("test", type="direct")
    bound = mailbox(connection)

    bound._broadcast("print_msg", {'msg': 'Message for you'})
コード例 #11
0
        assert self.bound.call(['some_node'], 'mymethod') == 'COLLECTED'
        consumer = self.node.Consumer()
        assert is_call(self.get_next(consumer))

    def test_multi_call(self):
        assert self.bound.multi_call('mymethod') == 'COLLECTED'
        consumer = self.node.Consumer()
        assert is_call(self.get_next(consumer))

    def get_next(self, consumer):
        m = consumer.queues[0].get()
        if m:
            return m.payload


GLOBAL_PIDBOX = pidbox.Mailbox('global_unittest_mailbox')


def getoid():
    return GLOBAL_PIDBOX.oid


class test_PidboxOid:
    """Unittests checking oid consistency of Pidbox"""

    def test_oid_consistency(self):
        """Tests that oid is consistent in single process"""
        m1 = pidbox.Mailbox('mailbox1')
        m2 = pidbox.Mailbox('mailbox2')
        assert m1.oid == m1.oid
        assert m2.oid == m2.oid