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
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)
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)
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()
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)
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"
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"
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)
# 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()
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'})
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