From b1a93d0e257ef812edcddf0b6e711b5e60505b19 Mon Sep 17 00:00:00 2001 From: Guilherme Diego Date: Wed, 18 Oct 2017 20:09:54 -0200 Subject: [PATCH] Add: bottery.app/.cli: close loop and session Fix: rougeth reviews Fix: lint Fix: tests: typo and mock order Add: Unit test to .stop Fix: test: isclosed to is_closed Change is_close to prop --- bottery/app.py | 7 +++++++ bottery/cli.py | 6 +++++- tests/test_app.py | 9 +++++++++ tests/test_cli.py | 11 +++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/bottery/app.py b/bottery/app.py index bac006d..339c5a8 100644 --- a/bottery/app.py +++ b/bottery/app.py @@ -63,3 +63,10 @@ def run(self): logger.debug('Tasks created') self.loop.run_forever() + + def stop(self): + self.session.close() + # Exit event loop at the next suitable opportunity... + self.loop.stop() + # ...and now close it. + self.loop.close() diff --git a/bottery/cli.py b/bottery/cli.py index a09d06f..64d5a8c 100644 --- a/bottery/cli.py +++ b/bottery/cli.py @@ -54,4 +54,8 @@ def startproject(name): @debug_option def run(port, debug): app = App() - app.run() + + try: + app.run() + except KeyboardInterrupt: + app.stop() diff --git a/tests/test_app.py b/tests/test_app.py index dcb7864..12f2b52 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -110,7 +110,16 @@ def test_app_run(mocked_asyncio): app = App() app.run() + mocked_event_loop = mocked_asyncio.get_event_loop.return_value mocked_asyncio.get_event_loop.assert_called_with() mocked_event_loop.run_forever.assert_called_with() + + +def test_app_stop(): + app = App() + app.stop() + + assert app.loop.is_closed() + assert app.session.closed diff --git a/tests/test_cli.py b/tests/test_cli.py index 0fdd608..fb91bef 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -16,6 +16,17 @@ def test_app_run_is_called(mocked_run): assert mocked_run.called +@mock.patch('bottery.cli.App.run') +@mock.patch('bottery.cli.App.stop') +def test_keyboard_interrupt_correctly_close_app(mocked_stop, mocked_run): + runner = CliRunner() + mocked_run.side_effect = KeyboardInterrupt() + runner.invoke(run) + + assert mocked_run.called + assert mocked_stop.called + + def test_debug_flag_enabled(): logger = logging.getLogger('bottery') logger.setLevel(logging.INFO)