def test_pikathread_bad_conn_params(connection_params): # Ensure that a bad connection parameter fails immediately bad_params = [copy.copy(connection_params[0])] bad_params[0].port = 1 thread = _PikaThread(bad_params) thread.start() thread.join(timeout=5) assert not thread.is_alive() with pytest.raises(pika.exceptions.AMQPConnectionError): thread.raise_if_exception() # Start a working connection params = [copy.copy(connection_params[0])] thread = _PikaThread(params) thread.start() thread.wait_for_connection() # Forcibly cause a failure of reconnection by editing the inner connection dict # - otherwise, we can't guarantee that it will do them in the right order thread._connection_parameters[0].port = 1 # Make sure it will fail on the next attempt thread._reconnection_attempt_limit = 1 # Force a reconnection thread._debug_close_connection() # Wait for it to do this reconnection wait of 1s thread.join(timeout=5, stop=False) assert not thread.is_alive()
def test_pikathread_subscribe_queue(connection_params, test_channel): queue = test_channel.temporary_queue_declare() thread = _PikaThread(connection_params) try: thread.start() thread.wait_for_connection() messages = Queue() def _get_message(*args): print(f"Got message: {pprint.pformat(args)}") messages.put(args[3]) thread.subscribe_queue(queue, _get_message, reconnectable=True, subscription_id=1) test_channel.basic_publish("", queue, "This is a message") assert messages.get(timeout=2) == b"This is a message" print("Terminating connection") thread._connected.clear() thread._debug_close_connection() thread.wait_for_connection() print("Reconnected") test_channel.basic_publish("", queue, "This is another message") assert messages.get(timeout=2) == b"This is another message" finally: thread.join(stop=True)
def test_pikathread_broadcast_subscribe(connection_params, test_channel): thread = _PikaThread(connection_params) thread.start() thread.wait_for_connection() exchange = test_channel.temporary_exchange_declare(exchange_type="fanout", auto_delete=True) # Basic check that a message has come through got_message = threading.Event() def _callback(channel, basic_deliver, properties, body): print( f"Got message:\n Channel: {channel.channel_number}\n basic_deliver: {basic_deliver}\n properties: {properties}\n\n {body.decode()}" ) got_message.set() # Make a subscription and wait for it to be valid thread.subscribe_broadcast(exchange, _callback, subscription_id=1, reconnectable=True).result() test_channel.basic_publish(exchange, routing_key="", body="A Message") got_message.wait(5) assert got_message.is_set() thread.join(re_raise=True, stop=True)
def test_pikathread(connection_params): thread = _PikaThread(connection_params) thread.start() print("Waiting for pika connection") thread.wait_for_connection() print("stopping connection") thread.join(re_raise=True, stop=True)
def test_pikathread_unsubscribe(test_channel, connection_params): queue = test_channel.temporary_queue_declare() thread = _PikaThread(connection_params) try: thread.start() thread.wait_for_connection() messages = Queue() def _get_message(*args): print(f"Got message: {pprint.pformat(args)}") messages.put(args[3]) thread.subscribe_queue(queue, _get_message, reconnectable=True, subscription_id=1) test_channel.basic_publish("", queue, "This is a message") assert messages.get(timeout=1) == b"This is a message" # Issue an unsubscribe then wait for confirmation thread.unsubscribe(1).result() # Send a message again test_channel.basic_publish("", queue, "This is a message again") # Wait a short time to make sure it does not get delivered with pytest.raises(Empty): messages.get(timeout=1) finally: thread.join(stop=True)
def test_pikathread_send(connection_params, test_channel): queue = test_channel.temporary_queue_declare(exclusive=True) thread = _PikaThread(connection_params) try: thread.start() thread.wait_for_connection() thread.send("", queue, "Test Message").result() assert test_channel.basic_get(queue, auto_ack=True)[2] == b"Test Message" # Make sure we don't have the missing queue declared with pytest.raises(pika.exceptions.ChannelClosedByBroker): check_channel = test_channel.connection.channel() check_channel.queue_declare("unroutable-missing-queue", passive=True) # Should not fail with mandatory=False thread.send("", "unroutable-missing-queue", "Another Message", mandatory=False).result() # But should fail with it declared pytest.xfail( "UnroutableError is not raised without publisher confirms, #96") with pytest.raises(pika.exceptions.UnroutableError): thread.send("", "unroutable-missing-queue", "Another Message", mandatory=True).result() finally: thread.join(stop=True)
def test_pikathread_broadcast_reconnection(connection_params, test_channel: pika.channel.Channel): thread = _PikaThread(connection_params) try: thread.start() thread.wait_for_connection() got_message = threading.Event() def _got_message(*args): got_message.set() exchange = test_channel.temporary_exchange_declare( exchange_type="fanout") thread.subscribe_broadcast(exchange, _got_message, reconnectable=True, subscription_id=1).result() # Force reconnection - normally we want this to be transparent, but # let's twiddle the internals so we can wait for reconnection as we # don't want to pick up the message before it resets print("Terminating connection") thread._connected.clear() thread._debug_close_connection() thread.wait_for_connection() print("Reconnected") # Now, make sure we still get this message test_channel.basic_publish(exchange, routing_key="", body="A Message") got_message.wait(2) assert got_message.is_set() # Add a non-resubscribable connection got_message_2 = threading.Event() def _got_message_2(*args): got_message_2.set() thread.subscribe_broadcast( exchange, _got_message_2, reconnectable=False, subscription_id=2, ).result() # Make sure that the thread ends instead of reconnect if we force a disconnection thread._debug_close_connection() thread.join() finally: thread.join(stop=True)