Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: catch any exceptions raised during server creation #392

Merged
merged 1 commit into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion synse_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,13 @@ def run(self) -> None:
return_asyncio_server=True,
access_log=False,
)
asyncio.ensure_future(self.server, loop=loop.synse_loop)
t = asyncio.ensure_future(self.server, loop=loop.synse_loop)

# Wait until the server has been created and run. If there was an error
# creating the server, this will raise an exception.
loop.synse_loop.run_until_complete(t)

# The server is now running on the loop, so run the loop forever.
loop.synse_loop.run_forever()

def reload_config(self) -> None:
Expand Down
25 changes: 22 additions & 3 deletions tests/unit/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def test_initialize(self, mock_reload, mock_logger, mock_mkdirs):
@mock.patch.dict('synse_server.config.options._full_config', {'metrics': {'enabled': True}})
@mock.patch('synse_server.server.Synse._initialize')
@mock.patch('synse_server.loop.synse_loop.run_forever')
@mock.patch('synse_server.loop.synse_loop.run_until_complete')
@mock.patch('sys.stdout.write')
def test_run_ok_with_metrics(self, mock_write, mock_run, mock_init):
def test_run_ok_with_metrics(self, mock_write, mock_ruc, mock_run, mock_init):
synse = server.Synse()
assert 'metrics' not in synse.app.router.routes_names.keys()

Expand All @@ -47,6 +48,7 @@ def test_run_ok_with_metrics(self, mock_write, mock_run, mock_init):

mock_write.assert_called_once()
mock_init.assert_called_once()
mock_ruc.assert_called_once()
mock_run.assert_called_once()

@mock.patch.dict(
Expand All @@ -55,13 +57,15 @@ def test_run_ok_with_metrics(self, mock_write, mock_run, mock_init):
)
@mock.patch('synse_server.server.Synse._initialize')
@mock.patch('synse_server.loop.synse_loop.run_forever')
@mock.patch('synse_server.loop.synse_loop.run_until_complete')
@mock.patch('sys.stdout.write')
def test_run_ok_with_ssl(self, mock_write, mock_run, mock_init):
def test_run_ok_with_ssl(self, mock_write, mock_ruc, mock_run, mock_init):
synse = server.Synse(log_header=False)
synse.run()

mock_write.assert_not_called()
mock_init.assert_called_once()
mock_ruc.assert_called_once()
mock_run.assert_called_once()

@mock.patch.dict(
Expand All @@ -70,15 +74,17 @@ def test_run_ok_with_ssl(self, mock_write, mock_run, mock_init):
)
@mock.patch('synse_server.server.Synse._initialize')
@mock.patch('synse_server.loop.synse_loop.run_forever')
@mock.patch('synse_server.loop.synse_loop.run_until_complete')
@mock.patch('sys.stdout.write')
def test_run_grpc_cert_error(self, mock_write, mock_run, mock_init):
def test_run_grpc_cert_error(self, mock_write, mock_ruc, mock_run, mock_init):
synse = server.Synse(log_header=False)

with pytest.raises(FileNotFoundError):
synse.run()

mock_write.assert_not_called()
mock_init.assert_called_once()
mock_ruc.assert_not_called()
mock_run.assert_not_called()

@mock.patch('synse_server.server.Synse._initialize')
Expand All @@ -92,6 +98,19 @@ def test_run_error(self, mock_run, mock_init):
mock_init.assert_called_once()
mock_run.assert_called_once()

@mock.patch('synse_server.server.Synse._initialize')
@mock.patch('synse_server.loop.synse_loop.run_until_complete', side_effect=ValueError)
@mock.patch('synse_server.loop.synse_loop.run_forever')
def test_run_error2(self, mock_run, mock_ruc, mock_init):
synse = server.Synse(log_header=False)

with pytest.raises(ValueError):
synse.run()

mock_init.assert_called_once()
mock_ruc.assert_called_once()
mock_run.assert_not_called()

@mock.patch('bison.Bison.parse')
@mock.patch('bison.Bison.validate')
@mock.patch('synse_server.server.Synse._initialize')
Expand Down