当前位置: 首页>>代码示例>>Python>>正文


Python Connection.close方法代码示例

本文整理汇总了Python中amqpstorm.Connection.close方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.close方法的具体用法?Python Connection.close怎么用?Python Connection.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在amqpstorm.Connection的用法示例。


在下文中一共展示了Connection.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: GeneratorConsumeMessagesTest

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
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()
开发者ID:exg77,项目名称:amqpstorm,代码行数:32,代码来源:generic_tests.py

示例2: PublishFailAndFix

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
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()
开发者ID:exg77,项目名称:amqpstorm,代码行数:33,代码来源:generic_tests.py

示例3: PublishLargeMessagesAndGetTest

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
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()
开发者ID:exg77,项目名称:amqpstorm,代码行数:31,代码来源:generic_tests.py

示例4: PublishAndGetMessagesTest

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
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()
开发者ID:exg77,项目名称:amqpstorm,代码行数:27,代码来源:generic_tests.py

示例5: PublishAndGetLargeMessageTest

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
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()
开发者ID:exg77,项目名称:amqpstorm,代码行数:28,代码来源:generic_tests.py

示例6: Publish50kTest

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
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()
开发者ID:exg77,项目名称:amqpstorm,代码行数:30,代码来源:generic_tests.py

示例7: test_connection_close

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
    def test_connection_close(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

        # Create some fake channels.
        for index in range(10):
            connection._channels[index + 1] = Channel(
                index + 1, connection, 360)

        def on_write(frame_out):
            self.assertIsInstance(frame_out, specification.Connection.Close)
            connection._channel0._close_connection_ok()

        connection._channel0._write_frame = on_write

        self.assertFalse(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.assertTrue(connection.is_closed)
开发者ID:eandersson,项目名称:amqpstorm,代码行数:29,代码来源:connection_tests.py

示例8: test_api_connection_close

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
    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()
开发者ID:eandersson,项目名称:amqpstorm,代码行数:32,代码来源:connection_tests.py

示例9: consumer

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
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()
开发者ID:gaochunzy,项目名称:amqp-storm,代码行数:27,代码来源:basic_get.py

示例10: test_connection_close_when_already_closed

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
    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)
开发者ID:eandersson,项目名称:amqpstorm,代码行数:31,代码来源:connection_tests.py

示例11: test_connection_close_handles_raise_on_write

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
    def test_connection_close_handles_raise_on_write(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

        # Create some fake channels.
        for index in range(10):
            connection._channels[index + 1] = Channel(
                index + 1, connection, 360)

        def raise_on_write(_):
            raise AMQPConnectionError('travis-ci')

        connection._channel0._write_frame = raise_on_write

        self.assertFalse(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)
开发者ID:eandersson,项目名称:amqpstorm,代码行数:29,代码来源:connection_tests.py

示例12: GetAndRedeliverTest

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
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()
开发者ID:exg77,项目名称:amqpstorm,代码行数:27,代码来源:generic_tests.py

示例13: PublishWithPropertiesAndGetTest

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
class PublishWithPropertiesAndGetTest(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.queue.declare('test.basic.properties')
        self.channel.queue.purge('test.basic.properties')
        self.channel.confirm_deliveries()

    def test_publish_with_properties_and_get(self):
        app_id = 'travis-ci'.encode('utf-8')
        properties = {
            'headers': {
                'key': 1234567890,
                'alpha': 'omega'
            }
        }

        message = Message.create(self.channel,
                                 body=str(uuid.uuid4()),
                                 properties=properties)
        # Assign Property app_id
        message.app_id = app_id

        # Check that it was set correctly.
        self.assertEqual(message.properties['app_id'], app_id)

        # Get Property Correlation Id
        correlation_id = message.correlation_id

        # Publish Message
        message.publish(routing_key='test.basic.properties')

        # Sleep for 0.5s to make sure RabbitMQ has time to catch up.
        time.sleep(0.5)

        # New way
        payload = self.channel.basic.get('test.basic.properties',
                                         to_dict=False)
        self.assertEqual(payload.properties['headers']['key'], 1234567890)
        self.assertEqual(payload.properties['headers']['alpha'], 'omega')
        self.assertEqual(payload.app_id, app_id.decode('utf-8'))
        self.assertEqual(payload.correlation_id, correlation_id)
        self.assertIsInstance(payload.properties['app_id'], str)
        self.assertIsInstance(payload.properties['correlation_id'], str)

        # Old way
        result = payload.to_dict()
        self.assertEqual(result['properties']['headers'][b'key'], 1234567890)
        self.assertEqual(result['properties']['headers'][b'alpha'], b'omega')
        self.assertIsInstance(result['properties']['app_id'], bytes)
        self.assertIsInstance(result['properties']['correlation_id'], bytes)
        self.assertEqual(result['properties']['app_id'], app_id)
        self.assertEqual(result['properties']['correlation_id'],
                         correlation_id.encode('utf-8'))

    def tearDown(self):
        self.channel.queue.delete('test.basic.properties')
        self.channel.close()
        self.connection.close()
开发者ID:exg77,项目名称:amqpstorm,代码行数:61,代码来源:generic_tests.py

示例14: consumer

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
def consumer():
    connection = Connection('127.0.0.1', 'guest', 'guest',
                            ssl=True, port=5671)
    channel = connection.channel()
    channel.queue.declare('simple_queue')
    channel.basic.publish(body='Hello World!', routing_key='simple_queue')
    channel.close()
    connection.close()
开发者ID:timothy-hanson,项目名称:amqp-storm,代码行数:10,代码来源:basic_ssl_publisher.py

示例15: PublishMessageAndResend

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import close [as 别名]
class PublishMessageAndResend(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.queue.declare('test.basic.resend')
        self.channel.queue.purge('test.basic.resend')
        self.channel.confirm_deliveries()
        message = Message.create(self.channel,
                                 body=str(uuid.uuid4()))
        message.app_id = 'travis-ci'
        message.publish('test.basic.resend')

    def test_publish_with_properties_and_get(self):
        message = self.channel.basic.get('test.basic.resend',
                                         to_dict=False, no_ack=True)

        # Check original app_id
        self.assertEqual(message.app_id, 'travis-ci')

        # Assign Property app_id
        app_id = 'travis-ci-2'.encode('utf-8')
        message.app_id = app_id

        # Check that it was set correctly.
        self.assertEqual(message.properties['app_id'], app_id)

        # Get Property Correlation Id
        correlation_id = message.correlation_id

        # Publish Message
        message.publish(routing_key='test.basic.resend')

        # Sleep for 0.5s to make sure RabbitMQ has time to catch up.
        time.sleep(0.5)

        # New way
        payload = self.channel.basic.get('test.basic.resend',
                                         to_dict=False, no_ack=True)
        self.assertEqual(payload.app_id, app_id.decode('utf-8'))
        self.assertEqual(payload.correlation_id, correlation_id)
        self.assertIsInstance(payload.properties['app_id'], str)
        self.assertIsInstance(payload.properties['correlation_id'], str)

        # Old way
        result = payload.to_dict()
        self.assertIsInstance(result['properties']['app_id'], bytes)
        self.assertIsInstance(result['properties']['correlation_id'], bytes)
        self.assertEqual(result['properties']['app_id'], app_id)
        self.assertEqual(result['properties']['correlation_id'],
                         correlation_id.encode('utf-8'))

    def tearDown(self):
        self.channel.queue.delete('test.basic.resend')
        self.channel.close()
        self.connection.close()
开发者ID:exg77,项目名称:amqpstorm,代码行数:57,代码来源:generic_tests.py


注:本文中的amqpstorm.Connection.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。