def test_connection_avoid_conflicts_with_channel_ids(self): connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True) connection.set_state(connection.OPEN) for index in compatibility.RANGE(1, 301): connection._channels[index] = None for index in compatibility.RANGE(302, 65535): connection._channels[index] = None last_channel_id = int(connection.channel(lazy=True)) self.assertEqual(last_channel_id, 301) self.assertEqual(connection._last_channel_id, 301) last_channel_id = int(connection.channel(lazy=True)) self.assertEqual(last_channel_id, 65535) self.assertEqual(connection._last_channel_id, 65535)
def test_connection_maximum_channels_reached(self): connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True) connection.set_state(connection.OPEN) for index in compatibility.RANGE(MAX_CHANNELS): channel_id = connection._get_next_available_channel_id() connection._channels[channel_id] = None self.assertEqual(connection._last_channel_id, index + 1) self.assertRaisesRegexp( AMQPConnectionError, 'reached the maximum number of channels %d' % MAX_CHANNELS, connection.channel, lazy=True)
def _create_content_body(body): """Split body based on the maximum frame size. This function is based on code from Rabbitpy. https://github.com/gmr/rabbitpy :param str body: :rtype: collections.Iterable """ frames = int(math.ceil(len(body) / float(FRAME_MAX))) for offset in compatibility.RANGE(0, frames): start_frame = FRAME_MAX * offset end_frame = start_frame + FRAME_MAX if end_frame > len(body): end_frame = len(body) yield pamqp_body.ContentBody(body[start_frame:end_frame])
def _create_content_body(self, body): """Split body based on the maximum frame size. This function is based on code from Rabbitpy. https://github.com/gmr/rabbitpy :param bytes,str,unicode body: Message payload :rtype: collections.Iterable """ frames = int(math.ceil(len(body) / float(self._max_frame_size))) for offset in compatibility.RANGE(0, frames): start_frame = self._max_frame_size * offset end_frame = start_frame + self._max_frame_size body_len = len(body) if end_frame > body_len: end_frame = body_len yield pamqp_body.ContentBody(body[start_frame:end_frame])
def _get_next_available_channel_id(self): """Returns the next available available channel id. :raises AMQPConnectionError: Raises if there is no available channel. :rtype: int """ for index in compatibility.RANGE(self._last_channel_id or 1, self.max_allowed_channels + 1): if index in self._channels: continue self._last_channel_id = index return index if self._last_channel_id: self._last_channel_id = None return self._get_next_available_channel_id() raise AMQPConnectionError('reached the maximum number of channels %d' % self.max_allowed_channels)
def test_connection_close_old_ids_with_channel(self): max_channels = 1024 connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True) connection._channel0.max_allowed_channels = max_channels connection.set_state(connection.OPEN) for _ in compatibility.RANGE(max_channels): connection.channel(lazy=True) ids_to_close = [2, 8, 16, 32, 64, 128, 256, 512, 768, 1024] for _ in range(100): for index in ids_to_close: del connection._channels[index] self.assertEqual(len(connection._channels), max_channels - len(ids_to_close)) for _ in ids_to_close: self.assertIn(int(connection.channel(lazy=True)), ids_to_close) self.assertEqual(len(connection._channels), max_channels)
def test_connection_open_many_channels(self): connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True) connection.set_state(connection.OPEN) for index in compatibility.RANGE(MAX_CHANNELS - 1): self.assertEqual(int(connection.channel(lazy=True)), index + 1)