本文整理汇总了Python中amqpstorm.Connection类的典型用法代码示例。如果您正苦于以下问题:Python Connection类的具体用法?Python Connection怎么用?Python Connection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Connection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PublishLargeMessagesAndGetTest
class PublishLargeMessagesAndGetTest(unittest.TestCase):
def setUp(self):
self.connection = Connection(HOST, USERNAME, PASSWORD)
self.channel = self.connection.channel()
self.channel.confirm_deliveries()
self.channel.queue.declare('test.basic.large_messages')
self.channel.queue.purge('test.basic.large_messages')
def test_publish_5_large_messages(self):
body = str(uuid.uuid4()) * 8192
messages_to_publish = 5
# Publish 5 Messages.
for _ in range(messages_to_publish):
self.channel.basic.publish(body=body,
routing_key='test.basic.large_messages')
inbound_messages = []
for _ in range(messages_to_publish):
message = self.channel.basic.get('test.basic.large_messages',
no_ack=True, to_dict=False)
self.assertEqual(message.body, body)
inbound_messages.append(message)
self.assertEqual(len(inbound_messages), messages_to_publish)
def tearDown(self):
self.channel.queue.delete('test.basic.large_messages')
self.channel.close()
self.connection.close()
示例2: test_connection_cleanup_channel_does_not_exist
def test_connection_cleanup_channel_does_not_exist(self):
connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
connection._channels[1] = Channel(1, connection, 0.1)
connection._cleanup_channel(2)
self.assertEqual(len(connection._channels), 1)
示例3: Publish50kTest
class Publish50kTest(unittest.TestCase):
def setUp(self):
self.connection = Connection(HOST, USERNAME, PASSWORD)
self.channel = self.connection.channel()
self.channel.queue.declare('test.basic.50k')
self.channel.queue.purge('test.basic.50k')
def test_publish_50k_messages(self):
body = str(uuid.uuid4())
# Publish 50k Messages.
start_time = time.time()
for _ in range(50000):
self.channel.basic.publish(body=body,
routing_key='test.basic.50k')
end_time = time.time() - start_time
# Sleep for 2.5s to make sure RabbitMQ has time to catch up.
time.sleep(2.5)
result = self.channel.queue.declare(queue='test.basic.50k',
passive=True)
LOGGER.info('Published 50k messages in %d', round(end_time, 3))
self.assertEqual(result['message_count'], 50000)
def tearDown(self):
self.channel.queue.delete('test.basic.50k')
self.channel.close()
self.connection.close()
示例4: consumer
def consumer():
connection = Connection(HOST, USERNAME, PASSWORD)
channel = connection.channel()
# Declare a queue.
channel.queue.declare(QUEUE_NAME)
# Publish something we can get.
channel.basic.publish(body='Hello World!', routing_key=QUEUE_NAME,
properties={'content_type': 'text/plain'})
# Retrieve a single message.
result = channel.basic.get(queue=QUEUE_NAME, no_ack=False)
if result:
# If we got a message, handle it.
print('Message:', result['body'])
# Mark the message as handle.
channel.basic.ack(delivery_tag=result['method']['delivery_tag'])
else:
# The queue was empty.
print("Queue '{0}' Empty.".format(QUEUE_NAME))
channel.close()
connection.close()
示例5: GetAndRedeliverTest
class GetAndRedeliverTest(unittest.TestCase):
def setUp(self):
self.connection = Connection(HOST, USERNAME, PASSWORD)
self.channel = self.connection.channel()
self.channel.queue.declare('test.get.redeliver')
self.channel.queue.purge('test.get.redeliver')
self.channel.confirm_deliveries()
self.message = str(uuid.uuid4())
self.channel.basic.publish(body=self.message,
routing_key='test.get.redeliver')
message = self.channel.basic.get('test.get.redeliver', no_ack=False,
to_dict=False)
message.reject()
# Sleep for 0.5s to make sure RabbitMQ has time to catch up.
time.sleep(0.5)
def test_get_and_redeliver(self):
message = self.channel.basic.get('test.get.redeliver', no_ack=False,
to_dict=False)
self.assertEqual(message.body, self.message)
def tearDown(self):
self.channel.queue.delete('test.get.redeliver')
self.channel.close()
self.connection.close()
示例6: test_connection_handle_amqp_frame_none_returns_none
def test_connection_handle_amqp_frame_none_returns_none(self):
connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
result = connection._handle_amqp_frame('')
self.assertEqual(result[0], '')
self.assertIsNone(result[1])
self.assertIsNone(result[2])
示例7: test_connection_handle_value_error
def test_connection_handle_value_error(self):
"""This test covers an unlikely issue triggered by network corruption.
pamqp.decode._embedded_value raises:
ValueError: Unknown type: b'\x13'
The goal here is not to fix issues caused by network corruption,
but rather to make sure that the exceptions raised when
connections do fail are always predictable.
Fail fast and reliably!
:return:
"""
connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
def throw_error(_):
raise ValueError("Unknown type: b'\x13'")
restore_func = pamqp_frame.unmarshal
try:
pamqp_frame.unmarshal = throw_error
result = connection._handle_amqp_frame('travis-ci')
self.assertEqual(result[0], 'travis-ci')
self.assertIsNone(result[1])
self.assertIsNone(result[2])
finally:
pamqp_frame.unmarshal = restore_func
self.assertEqual(self.get_last_log(),
"Unknown type: b'\x13'")
示例8: test_api_connection_close
def test_api_connection_close(self):
reason = 'travis-ci'
api = ManagementApi(HTTP_URL, USERNAME, PASSWORD, timeout=1)
self.assertEqual(len(api.connection.list()), 0,
'found an open connection, test will fail')
connection = Connection(HOST, USERNAME, PASSWORD, timeout=1)
connections = retry_function_wrapper(api.connection.list)
self.assertIsNotNone(connections)
self.assertEqual(len(connections), 1)
for conn in api.connection.list():
self.assertEqual(api.connection.close(conn['name'],
reason=reason), None)
time.sleep(1)
self.assertRaisesRegexp(
AMQPConnectionError,
'Connection was closed by remote server: '
'CONNECTION_FORCED - %s' % reason,
connection.check_for_errors
)
self.assertEqual(len(api.connection.list()), 0)
connection.close()
示例9: test_connection_handle_unicode_error
def test_connection_handle_unicode_error(self):
"""This test covers an unlikely issue triggered by network corruption.
pamqp.decode._maybe_utf8 raises:
UnicodeDecodeError: 'utf8' codec can't
decode byte 0xc5 in position 1: invalid continuation byte
The goal here is not to fix issues caused by network corruption,
but rather to make sure that the exceptions raised when
connections do fail are always predictable.
Fail fast and reliably!
:return:
"""
connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
def throw_error(_):
raise UnicodeDecodeError(str(), bytes(), 1, 1, str())
restore_func = pamqp_frame.unmarshal
try:
pamqp_frame.unmarshal = throw_error
result = connection._handle_amqp_frame('travis-ci')
self.assertEqual(result[0], 'travis-ci')
self.assertIsNone(result[1])
self.assertIsNone(result[2])
finally:
pamqp_frame.unmarshal = restore_func
self.assertEqual(self.get_last_log(),
"'' codec can't decode bytes in position 1-0: ")
示例10: test_connection_get_first_channel_id
def test_connection_get_first_channel_id(self):
connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
self.assertEqual(connection._last_channel_id, None)
self.assertEqual(
connection._get_next_available_channel_id(), 1
)
self.assertEqual(connection._last_channel_id, 1)
示例11: PublishAndGetMessagesTest
class PublishAndGetMessagesTest(unittest.TestCase):
def setUp(self):
self.connection = Connection(HOST, USERNAME, PASSWORD)
self.channel = self.connection.channel()
self.channel.queue.declare('test.basic.get')
self.channel.queue.purge('test.basic.get')
def test_publish_and_get_five_messages(self):
# Publish 5 Messages.
for _ in range(5):
self.channel.basic.publish(body=str(uuid.uuid4()),
routing_key='test.basic.get')
# Sleep for 0.5s to make sure RabbitMQ has time to catch up.
time.sleep(0.5)
# Get 5 messages.
for _ in range(5):
payload = self.channel.basic.get('test.basic.get', to_dict=False)
self.assertIsInstance(payload, Message)
def tearDown(self):
self.channel.queue.delete('test.basic.get')
self.channel.close()
self.connection.close()
示例12: PublishAndGetLargeMessageTest
class PublishAndGetLargeMessageTest(unittest.TestCase):
def setUp(self):
self.connection = Connection(HOST, USERNAME, PASSWORD)
self.channel = self.connection.channel()
self.channel.confirm_deliveries()
self.channel.queue.declare('test.basic.get_large')
self.channel.queue.purge('test.basic.get_large')
def test_publish_and_get_large_message(self):
try:
body = str(uuid.uuid4()) * 65536
# Publish a single large message
self.channel.basic.publish(body=body,
routing_key='test.basic.get_large')
payload = self.channel.basic.get('test.basic.get_large',
to_dict=False)
self.assertEqual(body, payload.body)
finally:
self.channel.queue.purge('test.basic.get_large')
def tearDown(self):
self.channel.queue.delete('test.basic.get_large')
self.channel.close()
self.connection.close()
示例13: PublishFailAndFix
class PublishFailAndFix(unittest.TestCase):
def setUp(self):
self.connection = Connection(HOST, USERNAME, PASSWORD)
self.channel = self.connection.channel()
self.channel.confirm_deliveries()
def test_publish_and_confirm(self):
try:
self.channel.basic.publish(body=str(uuid.uuid4()),
routing_key='test.publish.fail.and.fix',
mandatory=True)
except AMQPChannelError as why:
self.assertTrue(self.channel.is_open)
self.assertEqual(why.error_code, 312)
if why.error_code == 312:
self.channel.queue.declare('test.publish.fail.and.fix')
result = \
self.channel.basic.publish(body=str(uuid.uuid4()),
routing_key='test.publish.fail.and.fix',
mandatory=True)
self.assertTrue(result)
payload = self.channel.queue.declare('test.publish.fail.and.fix',
passive=True)
self.assertEqual(payload['message_count'], 1)
def tearDown(self):
self.channel.queue.delete('test.publish.fail.and.fix')
self.channel.close()
self.connection.close()
示例14: test_connection_close_when_already_closed
def test_connection_close_when_already_closed(self):
connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
connection.set_state(connection.OPEN)
io = IO(connection.parameters, [])
io.socket = Mock(name='socket', spec=socket.socket)
connection._io = io
connection.set_state(connection.CLOSED)
# Create some fake channels.
for index in range(10):
connection._channels[index + 1] = Channel(
index + 1, connection, 360)
def state_set(state):
self.assertEqual(state, connection.CLOSED)
connection.set_state = state_set
self.assertTrue(connection.is_closed)
connection.close()
# Make sure all the fake channels were closed as well.
for index in range(10):
self.assertNotIn(index + 1, connection._channels)
self.assertFalse(connection._channels)
self.assertTrue(connection.is_closed)
示例15: GeneratorConsumeMessagesTest
class GeneratorConsumeMessagesTest(unittest.TestCase):
def setUp(self):
self.connection = Connection(HOST, USERNAME, PASSWORD)
self.channel = self.connection.channel()
self.channel.queue.declare('test.basic.generator')
self.channel.queue.purge('test.basic.generator')
self.channel.confirm_deliveries()
for _ in range(5):
self.channel.basic.publish(body=str(uuid.uuid4()),
routing_key='test.basic.generator')
self.channel.basic.consume(queue='test.basic.generator',
no_ack=True)
# Sleep for 0.5s to make sure RabbitMQ has time to catch up.
time.sleep(0.5)
def test_generator_consume(self):
# Store and inbound messages.
inbound_messages = []
for message in \
self.channel.build_inbound_messages(break_on_empty=True):
self.assertIsInstance(message, Message)
inbound_messages.append(message)
# Make sure all five messages were downloaded.
self.assertEqual(len(inbound_messages), 5)
def tearDown(self):
self.channel.queue.delete('test.basic.generator')
self.channel.close()
self.connection.close()