Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_heartbeat_raise_when_check_for_life_when_exceptions_not_set(self):
heartbeat = Heartbeat(1)
heartbeat._beats_since_check = 0
heartbeat._last_heartbeat = time.time() - 100
# Normally the exception should be passed down to the list of
# exceptions in the connection, but if that list for some obscure
# reason is None, we should raise directly in _check_for_life_signs.
self.assertRaises(AMQPConnectionError, heartbeat._check_for_life_signs)
def _send_start_ok(self, frame_in):
"""Send Start OK frame.
:param pamqp_spec.Connection.Start frame_in: Amqp frame.
:return:
"""
if 'PLAIN' not in try_utf8_decode(frame_in.mechanisms):
exception = AMQPConnectionError(
'Unsupported Security Mechanism(s): %s' %
frame_in.mechanisms
)
self._connection.exceptions.append(exception)
return
credentials = self._plain_credentials()
start_ok_frame = pamqp_spec.Connection.StartOk(
mechanism=AUTH_MECHANISM,
client_properties=self._client_properties(),
response=credentials,
locale=LOCALE)
self._write_frame(start_ok_frame)
:return:
"""
ssl_options = ssl_options or {}
kwargs = urlparse.parse_qs(parsed_uri.query)
vhost = urlparse.unquote(parsed_uri.path[1:]) or DEFAULT_VIRTUAL_HOST
options = {
'ssl': use_ssl,
'virtual_host': vhost,
'heartbeat': int(kwargs.pop('heartbeat',
[DEFAULT_HEARTBEAT_INTERVAL])[0]),
'timeout': int(kwargs.pop('timeout',
[DEFAULT_SOCKET_TIMEOUT])[0])
}
if use_ssl:
if not compatibility.SSL_SUPPORTED:
raise AMQPConnectionError(
'Python not compiled with support '
'for TLSv1 or higher'
)
ssl_options.update(self._parse_ssl_options(kwargs))
options['ssl_options'] = ssl_options
return options
"""
last_message_built_at = time()
self.check_for_errors()
while not self.is_closed:
if self._die.value != 0:
return
if self.is_closed:
return
message = self._build_message(auto_decode=auto_decode)
if not message:
if time() - last_message_built_at > self.message_build_timeout:
raise AMQPConnectionError("Timeout while attempting to build message!")
if break_on_empty:
break
self.check_for_errors()
sleep(IDLE_WAIT)
continue
last_message_built_at = time()
if to_tuple:
yield message.to_tuple()
continue
yield message
"""Close connection.
:raises AMQPConnectionError: Raises if the connection
encountered an error.
:return:
"""
LOGGER.debug('Connection Closing')
if not self.is_closed:
self.set_state(self.CLOSING)
self.heartbeat.stop()
try:
self._close_remaining_channels()
if not self.is_closed and self.socket:
self._channel0.send_close_connection()
self._wait_for_connection_state(state=Stateful.CLOSED)
except AMQPConnectionError:
pass
finally:
self._io.close()
self.set_state(self.CLOSED)
LOGGER.debug('Connection Closed')
def is_ready(self):
"""Is Socket Ready.
:rtype: tuple
"""
try:
ready, _, _ = self.select.select([self.fileno], [], [],
self.timeout)
return bool(ready)
except self.select.error as why:
if why.args[0] != EINTR:
self._exceptions.append(AMQPConnectionError(why))
return False
def _close_connection(self, frame_in):
"""Connection Close.
:param specification.Connection.Close frame_in: Amqp frame.
:return:
"""
self._set_connection_state(Stateful.CLOSED)
if frame_in.reply_code != 200:
reply_text = try_utf8_decode(frame_in.reply_text)
message = (
'Connection was closed by remote server: %s' % reply_text
)
exception = AMQPConnectionError(message,
reply_code=frame_in.reply_code)
self._connection.exceptions.append(exception)
def _create_socket(self, socket_family):
"""Create Socket.
:param int socket_family:
:rtype: socket.socket
"""
sock = socket.socket(socket_family, socket.SOCK_STREAM, 0)
sock.settimeout(self._parameters['timeout'] or None)
if self.use_ssl:
if not compatibility.SSL_SUPPORTED:
raise AMQPConnectionError(
'Python not compiled with support for TLSv1 or higher'
)
sock = self._ssl_wrap_socket(sock)
return sock
If an error is thrown, handle it and return an empty string.
:return: data_in
:rtype: bytes
"""
data_in = EMPTY_BUFFER
try:
if not self.socket:
raise socket.error('connection/socket error')
data_in = self._read_from_socket()
except socket.timeout:
pass
except (IOError, OSError) as why:
if why.args[0] not in (EWOULDBLOCK, EAGAIN):
self._exceptions.append(AMQPConnectionError(why))
self._running.clear()
return data_in
def _wait_for_connection_state(self, state=Stateful.OPEN):
"""Wait for a Connection state.
:param int state: State that we expect
:raises AMQPConnectionError: Raises if we reach the connection timeout.
:return:
"""
start_time = time.time()
timeout = (self.parameters['timeout'] or 10) * 3
while self.current_state != state:
self.check_for_errors()
if time.time() - start_time > timeout:
raise AMQPConnectionError('Connection timed out')
sleep(IDLE_WAIT)