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_send_unix(self):
with mock.patch.object(socket.socket, 'connect') as mock_connect:
handler = SyslogHandler(address='/path/to/unix.socket')
mock_connect.assert_called_once_with('/path/to/unix.socket')
with mock.patch.object(socket.socket, 'send') as mock_send:
handler._send(['this is the first part', 'here is another part', 'one more part'])
mock_send.assert_has_calls([
mock.call('this is the first part'),
mock.call('here is another part'),
mock.call('one more part'),
])
def test_main_some_args(self):
reloader = MockReloader('example_service.standalone', ['pysoa'])
mock_func = mock.MagicMock()
# noinspection PyUnresolvedReferences
with mock.patch.object(target=reloader, attribute='watch_and_reload') as mock_watch_and_reload:
reloader.main(mock_func, ('a', 'b'), {'c': 'd'})
mock_watch_and_reload.assert_called_once_with(mock_func, ('a', 'b'), {'c': 'd'})
self.assertFalse(mock_func.called)
assert not handler.unixsocket # type: ignore
else:
assert handler.unixsocket is False # type: ignore
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_FRAGMENT
assert handler.maximum_length >= 1252 # (1280 - 28)
handler = SyslogHandler(overflow=SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE)
assert handler.socktype == socket.SOCK_DGRAM # type: ignore
if six.PY2:
assert not handler.unixsocket # type: ignore
else:
assert handler.unixsocket is False # type: ignore
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length >= 1252 # (1280 - 28)
with mock.patch.object(socket.socket, 'connect'):
handler = SyslogHandler(socket_type=socket.SOCK_STREAM)
assert handler.socktype == socket.SOCK_STREAM # type: ignore
if six.PY2:
assert not handler.unixsocket # type: ignore
else:
assert handler.unixsocket is False # type: ignore
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length == 1024 * 1024
handler = SyslogHandler(address='/path/to/unix.socket')
assert handler.socktype == socket.SOCK_DGRAM # type: ignore
assert handler.unixsocket is True or handler.unixsocket == 1 # type: ignore # Python 2 compatibility
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length == 1024 * 1024
handler = SyslogHandler(address='/path/to/unix.socket', socket_type=socket.SOCK_STREAM)
def test_send_unix(self):
with mock.patch.object(socket.socket, 'connect') as mock_connect:
handler = SyslogHandler(address='/path/to/unix.socket')
mock_connect.assert_called_once_with('/path/to/unix.socket')
with mock.patch.object(socket.socket, 'send') as mock_send:
handler._send(['this is the first part', 'here is another part', 'one more part'])
mock_send.assert_has_calls([
mock.call('this is the first part'),
mock.call('here is another part'),
mock.call('one more part'),
])
def test_watch_and_reload_for_restart(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:
mock_os.environ.get.return_value = None
mock_restart_with_reloader.return_value = 15
reloader.watch_and_reload(mock_func, (), {})
mock_restart_with_reloader.assert_called_once_with()
mock_sys.exit.assert_called_once_with(15)
mock_os.environ.get.assert_called_once_with('PYSOA_RELOADER_RUN_MAIN')
self.assertFalse(mock_watch_files.called)
self.assertFalse(mock_stop_watching.called)
self.assertFalse(mock_os.getpid.called)
self.assertFalse(mock_os.kill.called)
mock_restart_with_reloader.reset_mock()
def test_watch_and_reload_for_watch_exception(self, mock_sys, mock_os):
reloader = MockReloader('example_service.standalone', ['pysoa'])
mock_func = mock.MagicMock()
class MockException(Exception):
pass
# 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)
raise MockException()
mock_os.environ.get.return_value = 'true'
mock_func.side_effect = _se
with self.assertRaises(MockException):
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_os.environ.get.assert_called_once_with('PYSOA_RELOADER_RUN_MAIN')