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_receive_chunking_fails_on_client_subsequent_chunk_id_is_unexpected(self, mock_standard):
core = self._get_client_core(receive_timeout_in_seconds=3, message_expiry_in_seconds=10)
message = {'request_id': 79, 'meta': {'yes': 'no'}, 'body': {'baz': 'qux'}}
message['body'].update({'key-{}'.format(i): 'value-{}'.format(i) for i in range(200)}) # type: ignore
serialized = MsgpackSerializer().dict_to_blob(message)
mock_standard.return_value.get_connection.return_value.blpop.side_effect = [
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:1;' + serialized[0:1000])],
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:2;' + serialized[1000:2000])],
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:4;' + serialized[2000:3000])],
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:5;' + serialized[3000:])],
]
with pytest.raises(InvalidMessageError) as error_context:
core.receive_message('test_receive_chunking_fails_on_client_subsequent_chunk_count_differs')
assert 'incorrect chunk ID' in error_context.value.args[0]
def test_standard_client_created_with_defaults(self, mock_standard, mock_sentinel):
# noinspection PyArgumentList
core = RedisTransportServerCore(backend_type=REDIS_BACKEND_TYPE_STANDARD)
core.backend_layer.anything() # type: ignore
assert core.message_expiry_in_seconds == 60
assert core.queue_capacity == 10000
assert core.queue_full_retries == 10
assert core.receive_timeout_in_seconds == 5
assert isinstance(core.default_serializer, MsgpackSerializer)
mock_standard.assert_called_once_with()
mock_standard.return_value.anything.assert_called_once_with()
assert not mock_sentinel.called
['pysoa:test_content_type_explicit_msgpack_with_version'],
timeout=core.receive_timeout_in_seconds,
)
core.send_message('test_content_type_explicit_msgpack_with_version:reply', 72, meta, {'nope': 'yep'})
call_kwargs = mock_standard.return_value.send_message_to_queue.call_args_list[0][1]
assert call_kwargs['queue_key'] == 'pysoa:test_content_type_explicit_msgpack_with_version:reply'
assert call_kwargs['expiry'] == core.message_expiry_in_seconds
assert call_kwargs['capacity'] == core.queue_capacity
assert call_kwargs['connection'] == mock_standard.return_value.get_connection.return_value
assert call_kwargs['message'].startswith(b'pysoa-redis/3//content-type:application/msgpack;')
message = MsgpackSerializer().blob_to_dict(
call_kwargs['message'][len(b'pysoa-redis/3//content-type:application/msgpack;'):],
)
assert message['request_id'] == 72
assert message['meta']['__expiry__'] <= time.time() + core.message_expiry_in_seconds
assert message['body'] == {'nope': 'yep'}
def test_receive_chunking_prohibited_on_server(self, mock_standard):
core = self._get_server_core(receive_timeout_in_seconds=3, message_expiry_in_seconds=10)
message = {'request_id': 79, 'meta': {'yes': 'no'}, 'body': {'baz': 'qux'}}
message['body'].update({'key-{}'.format(i): 'value-{}'.format(i) for i in range(200)}) # type: ignore
serialized = MsgpackSerializer().dict_to_blob(message)
mock_standard.return_value.get_connection.return_value.blpop.side_effect = [
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:1;' + serialized[0:1000])],
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:2;' + serialized[1000:2000])],
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:3;' + serialized[2000:3000])],
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:4;' + serialized[3000:])],
]
with pytest.raises(InvalidMessageError) as error_context:
core.receive_message('test_receive_chunking_prohibited_on_server')
assert 'Unsupported chunked request' in error_context.value.args[0]
def serializer():
return MsgpackSerializer()
def test_receive_chunking_successful_on_client(self, mock_standard):
core = self._get_client_core(receive_timeout_in_seconds=3, message_expiry_in_seconds=10)
message = {'request_id': 79, 'meta': {'yes': 'no'}, 'body': {'baz': 'qux'}}
message['body'].update({'key-{}'.format(i): 'value-{}'.format(i) for i in range(200)}) # type: ignore
serialized = MsgpackSerializer().dict_to_blob(message)
mock_standard.return_value.get_connection.return_value.blpop.side_effect = [
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:1;' + serialized[0:1000])],
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:2;' + serialized[1000:2000])],
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:3;' + serialized[2000:3000])],
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:4;' + serialized[3000:])],
]
request_id, meta, body = core.receive_message('test_receive_chunking_successful_on_client')
assert request_id == 79
assert 'serializer' in meta
assert 'protocol_version' in meta
assert meta['yes'] == 'no'
assert len(meta) == 3
assert meta['protocol_version'] == ProtocolVersion.VERSION_3
def test_content_type_explicit_msgpack_with_version(self, mock_standard):
core = self._get_server_core(receive_timeout_in_seconds=3, message_expiry_in_seconds=10)
mock_standard.return_value.get_connection.return_value.blpop.return_value = [
True,
(
b'pysoa-redis/3//content-type:application/msgpack;' +
MsgpackSerializer().dict_to_blob({'request_id': 72, 'meta': {}, 'body': {'baz': 'qux'}})
),
]
request_id, meta, body = core.receive_message('test_content_type_explicit_msgpack_with_version')
assert request_id == 72
assert 'serializer' in meta
assert 'protocol_version' in meta
assert len(meta) == 2
assert meta['protocol_version'] == ProtocolVersion.VERSION_3
assert isinstance(meta['serializer'], MsgpackSerializer)
assert body == {'baz': 'qux'}
mock_standard.return_value.get_connection.return_value.blpop.assert_called_once_with(
['pysoa:test_content_type_explicit_msgpack_with_version'],
timeout=core.receive_timeout_in_seconds,
mock_standard.return_value.get_connection.return_value.blpop.return_value = [
True,
(
b'content-type:application/msgpack;' +
MsgpackSerializer().dict_to_blob({'request_id': 71, 'meta': {}, 'body': {'baz': 'qux'}})
),
]
request_id, meta, body = core.receive_message('test_content_type_explicit_msgpack')
assert request_id == 71
assert 'serializer' in meta
assert 'protocol_version' in meta
assert len(meta) == 2
assert meta['protocol_version'] == ProtocolVersion.VERSION_2
assert isinstance(meta['serializer'], MsgpackSerializer)
assert body == {'baz': 'qux'}
mock_standard.return_value.get_connection.return_value.blpop.assert_called_once_with(
['pysoa:test_content_type_explicit_msgpack'],
timeout=core.receive_timeout_in_seconds,
)
core.send_message('test_content_type_explicit_msgpack:reply', 71, meta, {'nope': 'yep'})
call_kwargs = mock_standard.return_value.send_message_to_queue.call_args_list[0][1]
assert call_kwargs['queue_key'] == 'pysoa:test_content_type_explicit_msgpack:reply'
assert call_kwargs['expiry'] == core.message_expiry_in_seconds
assert call_kwargs['capacity'] == core.queue_capacity
assert call_kwargs['connection'] == mock_standard.return_value.get_connection.return_value
timeout=core.receive_timeout_in_seconds,
)
core.send_message('test_content_type_default:reply', 15, meta, {'yep': 'nope'})
call_kwargs = mock_standard.return_value.send_message_to_queue.call_args_list[0][1]
assert call_kwargs['queue_key'] == 'pysoa:test_content_type_default:reply'
assert call_kwargs['expiry'] == core.message_expiry_in_seconds
assert call_kwargs['capacity'] == core.queue_capacity
assert call_kwargs['connection'] == mock_standard.return_value.get_connection.return_value
assert not call_kwargs['message'].startswith(b'pysoa-redis/')
assert not call_kwargs['message'].startswith(b'content-type')
message = MsgpackSerializer().blob_to_dict(call_kwargs['message'])
assert message['request_id'] == 15
assert message['meta']['__expiry__'] <= time.time() + core.message_expiry_in_seconds
assert message['body'] == {'yep': 'nope'}
def test_receive_chunking_fails_on_client_subsequent_chunk_missing_headers(self, mock_standard):
core = self._get_client_core(receive_timeout_in_seconds=3, message_expiry_in_seconds=10)
message = {'request_id': 79, 'meta': {'yes': 'no'}, 'body': {'baz': 'qux'}}
message['body'].update({'key-{}'.format(i): 'value-{}'.format(i) for i in range(200)}) # type: ignore
serialized = MsgpackSerializer().dict_to_blob(message)
mock_standard.return_value.get_connection.return_value.blpop.side_effect = [
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:1;' + serialized[0:1000])],
[True, (b'pysoa-redis/3//chunk-id:2;' + serialized[1000:2000])],
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:3;' + serialized[2000:3000])],
[True, (b'pysoa-redis/3//chunk-count:4;chunk-id:4;' + serialized[3000:])],
]
with pytest.raises(InvalidMessageError) as error_context:
core.receive_message('test_receive_chunking_fails_on_client_subsequent_chunk_missing_headers')
assert 'missing chunk headers' in error_context.value.args[0]