def test_channel0_unhandled_frame(self): channel = Channel0(self.connection) channel.on_frame(FakeFrame()) self.assertEqual(self.get_last_log(), "[Channel0] Unhandled Frame: FakeFrame")
def __init__(self, hostname, username, password, port=5672, **kwargs): """ :param str hostname: Hostname :param str username: Username :param str password: Password :param int port: Server port :param str virtual_host: Virtualhost :param int heartbeat: RabbitMQ Heartbeat interval :param int|float timeout: Socket timeout :param bool ssl: Enable SSL :param dict ssl_options: SSL kwargs (from ssl.wrap_socket) :param bool lazy: Lazy initialize the connection :return: """ super(Connection, self).__init__() self.parameters = { 'hostname': hostname, 'username': username, 'password': password, 'port': port, 'virtual_host': kwargs.get('virtual_host', '/'), 'heartbeat': kwargs.get('heartbeat', 60), 'timeout': kwargs.get('timeout', 30), 'ssl': kwargs.get('ssl', False), 'ssl_options': kwargs.get('ssl_options', {}) } self._validate_parameters() self.heartbeat = Heartbeat(self.parameters['heartbeat']) self._io = IO(self.parameters, on_read=self._read_buffer) self._channel0 = Channel0(self) self._channels = {} if not kwargs.get('lazy', False): self.open()
def test_channel0_heartbeat(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel0(connection) self.assertIsNone(channel.on_frame(Heartbeat()))
def test_channel0_on_hearbeat_registers_heartbeat(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) last_heartbeat = connection.heartbeat._last_heartbeat start_time = time.time() channel = Channel0(connection) time.sleep(0.1) def fake(*_): pass # Don't try to write to socket during test. channel._write_frame = fake # As the heartbeat timer was never started, it should be 0. self.assertEqual(connection.heartbeat._last_heartbeat, 0.0) channel.on_frame(Heartbeat()) self.assertNotEqual(connection.heartbeat._last_heartbeat, last_heartbeat) self.assertGreater(connection.heartbeat._last_heartbeat, start_time)
def __init__(self, hostname, username, password, port=5672, **kwargs): super(Connection, self).__init__() self.parameters = { 'hostname': hostname, 'username': username, 'password': password, 'port': port, 'virtual_host': kwargs.get('virtual_host', DEFAULT_VIRTUAL_HOST), 'heartbeat': kwargs.get('heartbeat', DEFAULT_HEARTBEAT_INTERVAL), 'timeout': kwargs.get('timeout', DEFAULT_SOCKET_TIMEOUT), 'ssl': kwargs.get('ssl', False), 'ssl_options': kwargs.get('ssl_options', {}), 'client_properties': kwargs.get('client_properties', {}) } self._validate_parameters() self._io = IO(self.parameters, exceptions=self._exceptions, on_read_impl=self._read_buffer) self._channel0 = Channel0(self, self.parameters['client_properties']) self._channels = {} self._last_channel_id = None self.heartbeat = Heartbeat(self.parameters['heartbeat'], self._channel0.send_heartbeat) if not kwargs.get('lazy', False): self.open()
def test_channel0_send_tune_ok_negotiate(self): channel = Channel0(FakeConnection()) channel._send_tune_ok( Connection.Tune(frame_max=MAX_FRAME_SIZE, channel_max=MAX_CHANNELS)) self.assertEqual(channel.max_frame_size, MAX_FRAME_SIZE) self.assertEqual(channel.max_allowed_channels, MAX_CHANNELS)
def test_channel0_credentials(self): connection = FakeConnection() connection.parameters['username'] = '******' connection.parameters['password'] = '******' channel = Channel0(connection) credentials = channel._plain_credentials() self.assertEqual(credentials, '\0guest\0password')
def test_channel0_open_ok_frame(self): channel = Channel0(self.connection) self.assertFalse(self.connection.is_open) channel.on_frame(Connection.OpenOk()) self.assertTrue(self.connection.is_open)
def test_channel0_send_tune_ok_negotiate_use_max(self): """Test to make sure that we use the highest acceptable value when the server returns zero. """ channel = Channel0(FakeConnection()) channel._send_tune_ok(Connection.Tune()) self.assertEqual(channel.max_frame_size, MAX_FRAME_SIZE) self.assertEqual(channel.max_allowed_channels, MAX_CHANNELS)
def test_channel0_invalid_authentication_mechanism(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel0(connection) channel._send_start_ok_frame( Connection.Start(mechanisms='CRAM-MD5 SCRAM-SHA-1 SCRAM-SHA-256')) self.assertRaises(AMQPConnectionError, connection.check_for_errors)
def test_channel0_send_close_connection_frame(self): connection = FakeConnection() channel = Channel0(connection) channel.send_close_connection_frame() self.assertNotEqual(connection.frames_out, []) channel_id, frame_out = connection.frames_out.pop() self.assertEqual(channel_id, 0) self.assertIsInstance(frame_out, Connection.Close)
def test_channel0_tune_frame(self): connection = FakeConnection() connection.parameters['virtual_host'] = '/' channel = Channel0(connection) channel.on_frame(Connection.Tune()) self.assertIsInstance(connection.get_last_frame(), Connection.TuneOk) self.assertIsInstance(connection.get_last_frame(), Connection.Open)
def test_channel0_on_close_ok_frame(self): self.connection.set_state(self.connection.OPEN) channel = Channel0(self.connection) self.assertFalse(self.connection.is_closed) channel.on_frame(Connection.CloseOk()) self.assertTrue(self.connection.is_closed)
def test_channel0_close_connection(self): connection = FakeConnection() connection.set_state(connection.OPEN) channel = Channel0(connection) self.assertTrue(connection.is_open) channel._close_connection( Connection.Close(reply_text=b'', reply_code=200)) self.assertEqual(connection.exceptions, []) self.assertTrue(connection.is_closed)
def test_channel0_send_tune_ok_negotiate_use_server(self): """Test to make sure that we use the highest acceptable value from the servers perspective. """ channel = Channel0(FakeConnection()) channel._send_tune_ok(Connection.Tune(frame_max=16384, channel_max=200)) self.assertEqual(channel.max_frame_size, 16384) self.assertEqual(channel.max_allowed_channels, 200)
def test_channel0_is_blocked(self): channel = Channel0(self.connection) self.assertFalse(channel.is_blocked) channel.on_frame(Connection.Blocked('travis-ci')) self.assertTrue(channel.is_blocked) self.assertEqual(self.get_last_log(), 'Connection is blocked by remote server: travis-ci')
def test_channel0_send_heartbeat(self): connection = FakeConnection() channel = Channel0(connection) channel.send_heartbeat() self.assertTrue(connection.frames_out) channel_id, frame_out = connection.frames_out.pop() self.assertEqual(channel_id, 0) self.assertIsInstance(frame_out, Heartbeat)
def test_channel0_forcefully_closed_connection(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) connection.set_state(connection.OPEN) channel = Channel0(connection) channel._close_connection( Connection.Close(reply_text=b'', reply_code=500)) self.assertTrue(connection.is_closed) self.assertRaises(AMQPConnectionError, connection.check_for_errors)
def test_channel0_unhandled_frame(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel0(connection) channel.on_frame(FakeFrame()) self.assertEqual(self.logging_handler.messages['error'][0], "[Channel0] Unhandled Frame: FakeFrame")
def test_channel0_send_tune_ok(self): connection = FakeConnection() channel = Channel0(connection) channel._send_tune_ok() self.assertNotEqual(connection.frames_out, []) channel_id, frame_out = connection.frames_out.pop() self.assertEqual(channel_id, 0) self.assertIsInstance(frame_out, Connection.TuneOk)
def test_channel0_start_invalid_auth_frame(self): connection = FakeConnection() connection.parameters['username'] = '******' connection.parameters['password'] = '******' channel = Channel0(connection) channel.on_frame(Connection.Start(mechanisms='invalid')) self.assertRaisesRegexp( AMQPConnectionError, 'Unsupported Security Mechanism\(s\): invalid', connection.check_for_errors)
def test_channel0_start_frame(self): connection = FakeConnection() connection.parameters['username'] = '******' connection.parameters['password'] = '******' channel = Channel0(connection) properties = {'version': 0} channel.on_frame(Connection.Start(server_properties=properties)) self.assertEqual(channel.server_properties, properties) self.assertIsInstance(connection.get_last_frame(), Connection.StartOk)
def test_channel0_open_ok_frame(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel0(connection) self.assertFalse(connection.is_open) channel.on_frame(Connection.OpenOk()) self.assertTrue(connection.is_open)
def test_channel0_on_close_frame(self): self.connection.set_state(self.connection.OPEN) channel = Channel0(self.connection) self.assertFalse(self.connection.exceptions) channel.on_frame(Connection.Close()) self.assertTrue(self.connection.exceptions) self.assertTrue(self.connection.is_closed) self.assertRaisesRegexp(AMQPConnectionError, 'Connection was closed by remote server: ', self.connection.check_for_errors)
def test_channel0_send_start_ok_external(self): connection = FakeConnection() channel = Channel0(connection) channel._send_start_ok(Connection.Start(mechanisms=b'EXTERNAL')) self.assertTrue(connection.frames_out) channel_id, frame_out = connection.frames_out.pop() self.assertEqual(channel_id, 0) self.assertIsInstance(frame_out, Connection.StartOk) self.assertNotEqual(frame_out.locale, '') self.assertIsNotNone(frame_out.locale)
def test_channel0_send_start_ok_frame(self): connection = FakeConnection() connection.parameters['username'] = '******' connection.parameters['password'] = '******' channel = Channel0(connection) channel._send_start_ok_frame(Connection.Start(mechanisms=b'PLAIN')) self.assertNotEqual(connection.frames_out, []) channel_id, frame_out = connection.frames_out.pop() self.assertEqual(channel_id, 0) self.assertIsInstance(frame_out, Connection.StartOk) self.assertNotEqual(frame_out.locale, '') self.assertIsNotNone(frame_out.locale)
def test_channel0_is_blocked(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel0(connection) self.assertFalse(channel.is_blocked) channel.on_frame(Connection.Blocked('unit-test')) self.assertTrue(channel.is_blocked) self.assertEqual(self.logging_handler.messages['warning'][0], 'Connection is blocked by remote server: unit-test')
def test_channel0_send_tune_ok(self): connection = FakeConnection() channel = Channel0(connection) channel._send_tune_ok(Connection.Tune()) self.assertTrue(connection.frames_out) channel_id, frame_out = connection.frames_out.pop() self.assertEqual(channel_id, 0) self.assertIsInstance(frame_out, Connection.TuneOk) self.assertEqual(frame_out.channel_max, MAX_CHANNELS) self.assertEqual(frame_out.frame_max, MAX_FRAME_SIZE)
def test_channel0_client_properties(self): channel = Channel0(FakeConnection()) result = channel._client_properties() information = 'See https://github.com/eandersson/amqpstorm' python_version = 'Python %s' % platform.python_version() self.assertIsInstance(result, dict) self.assertTrue(result['capabilities']['authentication_failure_close']) self.assertTrue(result['capabilities']['consumer_cancel_notify']) self.assertTrue(result['capabilities']['publisher_confirms']) self.assertTrue(result['capabilities']['connection.blocked']) self.assertTrue(result['capabilities']['basic.nack']) self.assertEqual(result['information'], information) self.assertEqual(result['platform'], python_version)
def test_channel0_unblocked(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel0(connection) channel.on_frame(Connection.Blocked()) self.assertTrue(channel.is_blocked) channel.on_frame(Connection.Unblocked()) self.assertFalse(channel.is_blocked) self.assertEqual(self.logging_handler.messages['info'][0], 'Connection is no longer blocked by remote server')