Skip to content

Instantly share code, notes, and snippets.

@saml
Last active April 16, 2019 18:11
Show Gist options
  • Save saml/5038bced407ce48dcff1b3018eca94fa to your computer and use it in GitHub Desktop.
Save saml/5038bced407ce48dcff1b3018eca94fa to your computer and use it in GitHub Desktop.

First, start toxiproxy (https://github.com/Shopify/toxiproxy):

toxiproxy-server
toxiproxy-cli create rabbitmq --listen localhost:5670 --upstream localhost:5672

RabbitMQ listens 5672. Toxiproxy listens 5670.

Prepare helper functions that connect to the proxy with short timeouts:

def connect():
    return pika.BlockingConnection(
        pika.ConnectionParameters(
            host='localhost',
            virtual_host='/',
            credentials=pika.PlainCredentials(
                username='guest',
                password='guest',
            ),
            socket_timeout=0.1,
            blocked_connection_timeout=0.1,
            connection_attempts=1,
            retry_delay=0.0,
            stack_timeout=0.2,
            port=5670,
        )
    )

def publish(channel, mandatory=False):
    properties = pika.BasicProperties(
        content_type='application/json',
        delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE,
    )
    channel.basic_publish(
        exchange='',
        routing_key='myqueue',
        properties=properties,
        mandatory=mandatory,
        body=json.dumps({'some': 'message'}),
    )

Test case:

c = connect()
# After connection is made, add 10 second latency to the proxy:
#  toxiproxy-cli toxic add rabbitmq -t latency -a latency=10000

ch = c.channel()      # blocks for 10 seconds.
publish(ch)           # does not block.
ch.close()            # blocks.

ch = c.channel()      # blocks.
ch.confirm_delivery() # blocks.
publish(ch)           # blocks.
ch.close()            # blocks.

There is no way to set timeout for .basic_publish() , .channel(), .confirm_delivery().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment