Skip to content

Commit

Permalink
bugfix: catch any exceptions raised during server creation
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniszewski committed Apr 13, 2020
1 parent e77bd60 commit 4514810
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
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

0 comments on commit 4514810

Please sign in to comment.