def test_can_place_message_on_queue(self): # Roll a UUID to make sure we're getting the right message. secret = str(uuid.uuid4()) # Generate a transport and place a message on the queue. trans = AmqpTransport( host='localhost', username='******', password='******', queue=self._test_id, ) message = ProfileRequest(0, secret, secret) trans.send(message) # Use the raw client to fetch to message. # Placed in a while loop to prevent false test failures due to # occasional race conditions. raw_message = None while raw_message is None: raw_message = self._channel.basic_get(self._test_id) # Find the secret. self.assertTrue(secret in raw_message.body) # ACK the message to avoid breaking other tests. self._channel.basic_ack(raw_message.delivery_tag)
def test_can_ack_messages(self): raw_message = amqp.Message( ProfileRequest(0, '', '').json(), content_type='application/json', ) self._channel.basic_publish(raw_message, routing_key=self._test_id) trans = AmqpTransport( host='localhost', username='******', password='******', queue=self._test_id, ) message = trans.fetch() trans.complete(message) self.assertTrue(trans.fetch() is None)
def test_can_fetch_message_from_queue(self): secret = str(uuid.uuid4()) raw_message = amqp.Message( ProfileRequest(0, secret, secret).json(), content_type='application/json', ) self._channel.basic_publish(raw_message, routing_key=self._test_id) trans = AmqpTransport( host='localhost', username='******', password='******', queue=self._test_id, ) message = trans.fetch() self.assertTrue(message.code == secret) self._channel.basic_ack(trans._message_map[message].delivery_tag)
def test_raises_value_error_on_unknown_message(self): raw_message = amqp.Message( '{"fake": true}', content_type='application/json', ) self._channel.basic_publish(raw_message, routing_key=self._test_id) trans = AmqpTransport( host='localhost', username='******', password='******', queue=self._test_id, ) with self.assertRaises(ValueError): trans.fetch() # Close the connection and force requeue of the message. trans.close() raw_message = self._channel.basic_get(self._test_id) self._channel.basic_ack(raw_message.delivery_tag)
from wsgiref import simple_server from sqlalchemy import create_engine from pyperf.transport.amq import AmqpTransport from pyperf.wsgi.app import make_app engine = create_engine('sqlite:////tmp/perf.db') transport = AmqpTransport( host='localhost', username='******', password='******', queue='sampleq', ) app = make_app(engine, transport=transport) if __name__ == '__main__': httpd = simple_server.make_server('127.0.0.1', 8000, app) httpd.serve_forever()
from pyperf.transport.amq import AmqpTransport AMQP_HOST = 'localhost' AMQP_USER = '******' AMQP_PASSWORD = '******' AMQP_QUEUE = 'sampleq' LOG_FILE = '/tmp/host-executor.log' PID_FILE = '/tmp/host-executor.pid' engine = create_engine('sqlite:////tmp/perf.db') transport = AmqpTransport( host=AMQP_HOST, username=AMQP_USER, password=AMQP_PASSWORD, queue=AMQP_QUEUE, ) if __name__ == '__main__': logging.basicConfig(filename=LOG_FILE, level=logging.DEBUG) d = Executor( pidfile=PID_FILE, transport=transport, engine=engine, samples=1, Profiler=BasicProfile, )