Skip to content

Commit

Permalink
Merge pull request #104 from guidiego/add-keyinterrupt-handle
Browse files Browse the repository at this point in the history
Add KeyInterrupt handle to app.run
  • Loading branch information
rougeth authored Oct 20, 2017
2 parents 1c4a4a9 + b1a93d0 commit d7b4f3e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions bottery/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
6 changes: 5 additions & 1 deletion bottery/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ def startproject(name):
@debug_option
def run(port, debug):
app = App()
app.run()

try:
app.run()
except KeyboardInterrupt:
app.stop()
9 changes: 9 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d7b4f3e

Please sign in to comment.