def test_message_with_propos(self): p = {'q': False} def ok(e): self.assertIsInstance(e, ReceivedMessage) self.assertEquals(e.body, b'hello5') # bcoz u can compare memoryviews to their providers :D self.assertEquals(e.properties.content_type, b'text/plain') self.assertEquals(e.properties.content_encoding, b'utf8') p['q'] = True con, fut = self.c.consume(Queue(u'hello5', exclusive=True), on_message=ok, no_ack=True) fut.result() self.c.publish(Message(b'hello5', properties={ 'content_type': b'text/plain', 'content_encoding': b'utf8' }), routing_key=u'hello5', tx=True).result() time.sleep(2) self.assertTrue(p['q'])
def test_very_long_messages(self): con, fut = self.c.consume(Queue(u'hello', exclusive=True)) fut.result() data = six.binary_type(os.urandom(20 * 1024 * 1024 + 1423)) self.c.publish(Message(data), routing_key=b'hello', confirm=True).result()
def test_drain_1(self): con, fut = self.c.consume( Queue(u'helloA', exclusive=True, auto_delete=True)) fut.result() self.c.publish(Message(b'ioi'), routing_key=u'helloA') self.assertIsInstance(self.c.drain(2), MessageReceived) self.assertIsInstance(self.c.drain(1), NothingMuch)
def test_send_recv_nonzerolen_listofmemoryview(self): """single and multi frame in LIST_OF_MEMORYVIEW mode""" from coolamqp.attaches import BodyReceiveMode con, fut = self.c.consume( Queue(u'hello8', exclusive=True), no_ack=True, body_receive_mode=BodyReceiveMode.LIST_OF_MEMORYVIEW) fut.result() data = b'hello8' self.c.publish(Message(data), routing_key=u'hello8', confirm=True) m = self.c.drain(1) self.assertIsInstance(m, MessageReceived) self.assertIsInstance(m.body[0], memoryview) self.assertEquals(m.body[0], data) data = six.binary_type(os.urandom(512 * 1024)) self.c.publish(Message(data), routing_key=u'hello8', confirm=True) m = self.c.drain(5) self.assertIsInstance(m, MessageReceived) self.assertTrue(all([isinstance(x, memoryview) for x in m.body])) self.assertEquals(b''.join(x.tobytes() for x in m.body), data)
def test_send_recv_nonzerolen_memoryview(self): """single and multi frame in MEMORYVIEW mode""" from coolamqp.attaches import BodyReceiveMode con, fut = self.c.consume(Queue(u'hello7', exclusive=True), no_ack=True, body_receive_mode=BodyReceiveMode.MEMORYVIEW) fut.result() data = b'hello7' self.c.publish(Message(data), routing_key=u'hello7', confirm=True) m = self.c.drain(2) self.assertIsInstance(m, MessageReceived) self.assertIsInstance(m.body, memoryview) self.assertEquals(m.body, data) data = six.binary_type(os.urandom(512 * 1024)) self.c.publish(Message(data), routing_key=u'hello7', confirm=True) m = self.c.drain(9) self.assertIsInstance(m, MessageReceived) self.assertIsInstance(m.body, memoryview) print(len(m.body)) self.assertEquals(m.body.tobytes(), data)
def test_send_recv_zerolen(self): p = {'q': False} def ok(e): self.assertIsInstance(e, ReceivedMessage) p['q'] = True con, fut = self.c.consume(Queue(u'hello3', exclusive=True), on_message=ok, no_ack=True) fut.result() self.c.publish(Message(b''), routing_key=u'hello3', tx=True).result() time.sleep(1) self.assertTrue(p['q'])
def test_fanout(self): x = Exchange(u'jola', type='direct', auto_delete=True) c1, f1 = self.c.consume(Queue('one', exchange=x, exclusive=True), no_ack=True) c2, f2 = self.c.consume(Queue('two', exchange=x, exclusive=True), no_ack=True) f1.result() f2.result() self.c.publish(Message(b'hello'), exchange=x, tx=True).result() self.assertIsInstance(self.c.drain(2), MessageReceived) self.assertIsInstance(self.c.drain(2), MessageReceived) self.assertIsInstance(self.c.drain(2), NothingMuch)
def test_queue_bind(self): queue = Queue('my-queue') exchange = Exchange('my-exchange', type='topic') self.c.declare(queue).result() self.c.declare(exchange).result() self.c.bind(queue, exchange, 'test').result() q = six.moves.queue.Queue() cons, fut = self.c.consume(queue, on_message=lambda msg: q.put(msg), no_ack=True) fut.result() self.c.publish(Message(b'test'), exchange=exchange, routing_key='test', confirm=True).result() msg_v = q.get(block=True, timeout=5) self.assertEqual(msg_v.body, b'test') cons.cancel()
def test_send_recv_nonzerolen(self): """with callback function""" p = {'q': False} def ok(e): self.assertIsInstance(e, ReceivedMessage) self.assertEquals(e.body, b'hello6') p['q'] = True con, fut = self.c.consume(Queue(u'hello6', exclusive=True), on_message=ok, no_ack=True) fut.result() self.c.publish(Message(b'hello6'), routing_key=u'hello6', tx=True).result() time.sleep(1) self.assertTrue(p['q'])
def test_nacking_and_acking(self): p = {'q': False, 'count': 0} def ok(msg): if not p['count']: msg.nack() msg.nack() else: msg.ack() msg.ack() self.assertIsInstance(msg, ReceivedMessage) p['q'] = True p['count'] += 1 con, fut = self.c.consume(Queue(u'hello3', exclusive=True), on_message=ok, no_ack=False) fut.result() self.c.publish(Message(b''), routing_key=u'hello3', tx=True).result() time.sleep(1) self.assertTrue(p['q']) self.assertEquals(p['count'], 2)
def loop(self): evt = self.amqp.drain(timeout=1.0) if isinstance(evt, ReceivedMessage): routing_key = evt.routing_key.tobytes().decode('utf8').replace( '-repl', '') self.amqp.publish(Message(evt.body), routing_key=routing_key)
def _send(self): self.cad_thread.amqp.publish(Message(uuid.uuid4().hex.encode('utf8')), routing_key=self.name + '-repl')