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_watch_and_reload_for_watch_normal_exit(self, mock_sys, mock_os):
reloader = MockReloader('example_service.standalone', ['pysoa'])
mock_func = mock.MagicMock()
# noinspection PyUnresolvedReferences
with mock.patch.object(target=reloader, attribute='restart_with_reloader') as mock_restart_with_reloader, \
mock.patch.object(target=reloader, attribute='watch_files') as mock_watch_files, \
mock.patch.object(target=reloader, attribute='stop_watching') as mock_stop_watching:
def _se(*_, **__):
time.sleep(0.25)
mock_watch_files.assert_called_once_with()
self.assertFalse(mock_stop_watching.called)
mock_os.environ.get.return_value = 'true'
mock_func.side_effect = _se
reloader.watch_and_reload(mock_func, ('a', 'b'), {'c': 'd'})
mock_func.assert_called_once_with('a', 'b', c='d')
mock_stop_watching.assert_called_once_with()
mock_sys.exit.assert_called_once_with(0)
def test_watch_and_reload_for_watch_reload_exit(self, mock_sys, mock_os):
reloader = MockReloader('example_service.standalone', ['pysoa'])
mock_func = mock.MagicMock()
# noinspection PyUnresolvedReferences
with mock.patch.object(target=reloader, attribute='restart_with_reloader') as mock_restart_with_reloader, \
mock.patch.object(target=reloader, attribute='watch_files') as mock_watch_files, \
mock.patch.object(target=reloader, attribute='stop_watching') as mock_stop_watching:
def _se(*_, **__):
time.sleep(0.25)
mock_watch_files.assert_called_once_with()
self.assertFalse(mock_stop_watching.called)
reloader.shutting_down_for_reload = True
mock_os.environ.get.return_value = 'true'
mock_func.side_effect = _se
reloader.watch_and_reload(mock_func, ('foo', 'bar'), {'baz': 'qux'})
mock_func.assert_called_once_with('foo', 'bar', baz='qux')
mock_stop_watching.assert_called_once_with()
mock_sys.exit.assert_called_once_with(NEED_RELOAD_EXIT_CODE)
def test_watch_and_reload_for_watch_reload_exit(self, mock_sys, mock_os):
reloader = MockReloader('example_service.standalone', ['pysoa'])
mock_func = mock.MagicMock()
# noinspection PyUnresolvedReferences
with mock.patch.object(target=reloader, attribute='restart_with_reloader') as mock_restart_with_reloader, \
mock.patch.object(target=reloader, attribute='watch_files') as mock_watch_files, \
mock.patch.object(target=reloader, attribute='stop_watching') as mock_stop_watching:
def _se(*_, **__):
time.sleep(0.25)
mock_watch_files.assert_called_once_with()
self.assertFalse(mock_stop_watching.called)
reloader.shutting_down_for_reload = True
mock_os.environ.get.return_value = 'true'
mock_func.side_effect = _se
reloader.watch_and_reload(mock_func, ('foo', 'bar'), {'baz': 'qux'})
mock_func.assert_called_once_with('foo', 'bar', baz='qux')
mock_stop_watching.assert_called_once_with()
mock_sys.exit.assert_called_once_with(NEED_RELOAD_EXIT_CODE)
mock_os.environ.get.assert_called_once_with('PYSOA_RELOADER_RUN_MAIN')
def _test():
backend = _FakeBackend()
server_transport = RedisServerTransport(
'threaded',
NoOpMetricsRecorder(),
backend_type=REDIS_BACKEND_TYPE_STANDARD,
)
server_transport.core._backend_layer = backend # type: ignore
client_transport = RedisClientTransport(
'threaded',
NoOpMetricsRecorder(),
backend_type=REDIS_BACKEND_TYPE_STANDARD,
)
client_transport.core._backend_layer = backend # type: ignore
server = _FakeEchoingServer(server_transport)
client1 = _FakeClient('client-1', client_transport, {'key1': 'value1'}, 1.0)
client2 = _FakeClient('client-2', client_transport, {'key2': 'value2'}, 0.25)
server.start()
client1.start()
client2.start()
client1.join(timeout=2)
client2.join(timeout=2)
@mock.patch(target='pysoa.common.transport.redis_gateway.client.get_hex_thread_id')
def test_without_thread_id(self, mock_get_hex_thread_id):
mock_get_hex_thread_id.return_value = ''
server, client1, client2 = self._test()
if server.error:
raise server.error
self.assertIsNotNone(client1.error)
self.assertTrue(
client1.error.args[0].startswith('Expected payload to be'),
'Expected error message to start with "Expected payload to be," but instead got "{}"'.format(
client1.error.args[0],
),
)
def test_emit_longer_than_limit_fragment(self):
handler = SyslogHandler()
handler.maximum_length = 100
handler.overflow = SyslogHandler.OVERFLOW_BEHAVIOR_FRAGMENT
handler.formatter = Formatter('foo_file: %(name)s %(levelname)s %(message)s')
record = LogRecord(
name='bar_service',
level=WARNING,
pathname='/path/to/file.py',
lineno=122,
msg='This is a much longer message that is going to exceed the maximum byte count and will need truncating',
args=(),
exc_info=None,
)
with mock.patch.object(handler, '_send') as mock_send:
handler.emit(record)
def test_emit_shorter_than_limit(self):
handler = SyslogHandler()
handler.maximum_length = 500
handler.overflow = SyslogHandler.OVERFLOW_BEHAVIOR_FRAGMENT
handler.formatter = Formatter('foo_file: %(name)s %(levelname)s %(message)s')
record = LogRecord(
name='bar_service',
level=WARNING,
pathname='/path/to/file.py',
lineno=122,
msg='This is a fairly short message',
args=(),
exc_info=None,
)
with mock.patch.object(handler, '_send') as mock_send:
handler.emit(record)
@mock.patch('pysoa.common.transport.redis_gateway.core.SentinelRedisClient')
@mock.patch('pysoa.common.transport.redis_gateway.core.StandardRedisClient')
def test_chunking_supported_on_server(self, mock_standard, mock_sentinel):
# noinspection PyArgumentList
core = RedisTransportServerCore(
backend_type=REDIS_BACKEND_TYPE_SENTINEL,
backend_layer_kwargs={
'connection_kwargs': {'hello': 'world'},
'hosts': [('another_host', 6379)],
'redis_db': 5,
'redis_port': 1098,
'sentinel_refresh_interval': 13,
'sentinel_services': ['svc1', 'svc2', 'svc3'],
'sentinel_failover_retries': 5,
},
message_expiry_in_seconds=45,
queue_capacity=7500,
@mock.patch('pysoa.common.transport.redis_gateway.core.StandardRedisClient')
def test_receive_chunking_fails_on_client_missing_header(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;' + 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:
@mock.patch('pysoa.common.transport.redis_gateway.core.SentinelRedisClient')
@mock.patch('pysoa.common.transport.redis_gateway.core.StandardRedisClient')
def test_chunking_not_supported_on_client(self, mock_standard, mock_sentinel):
with pytest.raises(TypeError) as error_context:
# noinspection PyArgumentList
RedisTransportClientCore( # type: ignore
backend_type=REDIS_BACKEND_TYPE_SENTINEL,
backend_layer_kwargs={
'connection_kwargs': {'hello': 'world'},
'hosts': [('another_host', 6379)],
'redis_db': 5,
'redis_port': 1098,
'sentinel_refresh_interval': 13,
'sentinel_services': ['svc1', 'svc2', 'svc3'],
'sentinel_failover_retries': 5,
},
message_expiry_in_seconds=45,