async def receive_log(): try: async with trio_amqp.connect() as protocol: channel = await protocol.channel() exchange_name = 'topic_logs' await channel.exchange(exchange_name, 'topic') result = await channel.queue(queue_name='', durable=False, auto_delete=True) queue_name = result['queue'] binding_keys = sys.argv[1:] if not binding_keys: print("Usage: %s [binding_key]..." % (sys.argv[0], )) sys.exit(1) for binding_key in binding_keys: await channel.queue_bind(exchange_name='topic_logs', queue_name=queue_name, routing_key=binding_key) print(' [*] Waiting for logs. To exit press CTRL+C') await channel.basic_consume(callback, queue_name=queue_name) await trio.sleep_forever() except trio_amqp.AmqpClosedConnection: print("closed connections") return
async def test_wrong_callback_argument(self): def badcallback(): pass self.reset_vhost() amqp = connect(virtualhost=self.vhost, ) with pytest.raises(TypeError): async with amqp: chan = await self.create_channel(amqp) await chan.queue_declare("q", exclusive=True, no_wait=False) await chan.exchange_declare("e", "fanout") await chan.queue_bind("q", "e", routing_key='') # get a different channel channel = await self.create_channel(amqp=amqp) # publish await channel.publish( "coucou", "e", routing_key='', ) # assert there is a message to consume await self.check_messages("q", 1) # start consume await channel.basic_consume(badcallback, queue_name="q") await trio.sleep(1)
async def test_socket_nodelay(self): self.reset_vhost() proto = connect(virtualhost=self.vhost) async with proto: sock = proto._stream.socket opt_val = sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY) assert opt_val == 1, opt_val
async def receive_log(): try: async with trio_amqp.connect() as protocol: channel = await protocol.channel() exchange_name = 'direct_logs' await channel.exchange(exchange_name, 'direct') result = await channel.queue(queue_name='', durable=False, auto_delete=True) queue_name = result['queue'] severities = sys.argv[1:] if not severities: print("Usage: %s [info] [warning] [error]" % (sys.argv[0], )) sys.exit(1) for severity in severities: await channel.queue_bind( exchange_name='direct_logs', queue_name=queue_name, routing_key=severity, ) print(' [*] Waiting for logs. To exit press CTRL+C') with trio.fail_after(10): await channel.basic_consume(callback, queue_name=queue_name) except trio_amqp.AmqpClosedConnection: print("closed connections") return
async def receive_log(): try: async with trio_amqp.connect() as protocol: channel = await protocol.channel() exchange_name = 'logs' await channel.exchange(exchange_name=exchange_name, type_name='fanout') # let RabbitMQ generate a random queue name result = await channel.queue(queue_name='', exclusive=True) queue_name = result['queue'] await channel.queue_bind(exchange_name=exchange_name, queue_name=queue_name, routing_key='') print(' [*] Waiting for logs. To exit press CTRL+C') await channel.basic_consume(callback, queue_name=queue_name, no_ack=True) await trio.sleep_forever() except trio_amqp.AmqpClosedConnection: print("closed connections") return
async def test_connect_tuning(self): # frame_max should be higher than 131072 self.reset_vhost() frame_max = 131072 channel_max = 10 heartbeat = 100 proto = connect( virtualhost=self.vhost, channel_max=channel_max, frame_max=frame_max, heartbeat=heartbeat, ) async with proto: assert proto.state == OPEN, proto.state assert proto.server_properties is not None assert proto.connection_tunning == { 'frame_max': frame_max, 'channel_max': channel_max, 'heartbeat': heartbeat } assert proto.server_channel_max == channel_max assert proto.server_frame_max == frame_max assert proto.server_heartbeat == heartbeat assert proto.state == CLOSED, proto.state
async def worker(): async with trio_amqp.connect('localhost', 5672) as protocol: channel = await protocol.channel() await channel.queue(queue_name='task_queue', durable=True) await channel.basic_qos(prefetch_count=1, prefetch_size=0, connection_global=False) await channel.basic_consume(callback, queue_name='task_queue') await trio.sleep_forever()
async def send(): async with trio_amqp.connect() as protocol: channel = await protocol.channel() await channel.queue_declare(queue_name='hello') await channel.basic_publish( payload='Hello World!', exchange_name='', routing_key='hello' ) print(" [x] Sent 'Hello World!'")
async def rpc_server(): async with trio_amqp.connect() as protocol: channel = await protocol.channel() await channel.queue_declare(queue_name='rpc_queue') await channel.basic_qos(prefetch_count=1, prefetch_size=0, connection_global=False) await channel.basic_consume(on_request, queue_name='rpc_queue') print(" [x] Awaiting RPC requests") await trio.sleep_forever()
async def receive(): try: async with trio_amqp.connect() as protocol: channel = await protocol.channel() await channel.queue_declare(queue_name='hello') await channel.basic_consume(callback, queue_name='hello') await trio.sleep_forever() except trio_amqp.AmqpClosedConnection: print("closed connections") return
async def exchange_routing(): try: async with trio_amqp.connect() as protocol: channel = await protocol.channel() exchange_name = 'logs' message = ' '.join(sys.argv[1:]) or "info: Hello World!" await channel.exchange_declare(exchange_name=exchange_name, type_name='fanout') await channel.basic_publish(message, exchange_name=exchange_name, routing_key='') print(" [x] Sent %r" % (message, )) except trio_amqp.AmqpClosedConnection: print("closed connections") return
async def exchange_routing(): try: async with trio_amqp.connect() as protocol: channel = await protocol.channel() exchange_name = 'direct_logs' severity = sys.argv[1] if len(sys.argv) > 1 else 'info' message = ' '.join(sys.argv[2:]) or 'Hello World!' await channel.exchange(exchange_name, 'direct') await channel.publish(message, exchange_name=exchange_name, routing_key=severity) print(" [x] Sent %r" % (message, )) except trio_amqp.AmqpClosedConnection: print("closed connections") return
async def call(self, n): async with trio_amqp.connect() as protocol: self.protocol = protocol await self.connect() self.response = None self.corr_id = str(uuid.uuid4()) await self.channel.basic_publish( payload=str(n), exchange_name='', routing_key='rpc_queue', properties={ 'reply_to': self.callback_queue, 'correlation_id': self.corr_id, }, ) await self.waiter.wait() return int(self.response)
async def exchange_routing_topic(): try: async with trio_amqp.connect() as protocol: channel = await protocol.channel() exchange_name = 'topic_logs' message = ' '.join(sys.argv[2:]) or 'Hello World!' routing_key = sys.argv[1] if len( sys.argv) > 1 else 'anonymous.info' await channel.exchange(exchange_name, 'topic') await channel.publish(message, exchange_name=exchange_name, routing_key=routing_key) print(" [x] Sent %r" % message) except trio_amqp.AmqpClosedConnection: print("closed connections") return
async def test_heartbeat(self): amqp = connect( virtualhost=self.vhost, ) self.reset_vhost() with pytest.raises(exceptions.HeartbeatTimeoutError): async with amqp: async def mock_send(): self.send_called += 1 self.send_called = 0 amqp.send_heartbeat = mock_send # reset both timers to 1) make them 'see' the new heartbeat value # 2) so that the mock is actually called back from the main loop amqp.server_heartbeat = 1 await amqp._heartbeat_timer_send_reset() await amqp._heartbeat_timer_recv_reset() await trio.sleep(1.1) assert self.send_called == 1 await trio.sleep(1.1) # not reached assert False,"not reached"