test notebook stop

This commit is contained in:
Min RK 2017-04-19 16:27:10 +02:00
parent 0d200ce9ab
commit 9e84fe332c
2 changed files with 37 additions and 0 deletions

View File

@ -366,6 +366,7 @@ class NbserverStopApp(JupyterApp):
if server['port'] == self.port:
self.log.debug("Shutting down notebook server with PID: %i", server['pid'])
os.kill(server['pid'], signal.SIGTERM)
return
else:
print("There is currently no server running on port {}".format(self.port), file=sys.stderr)
print("Ports currently in use:", file=sys.stderr)

View File

@ -4,6 +4,7 @@ import getpass
import logging
import os
import re
import signal
from subprocess import Popen, PIPE, STDOUT
import sys
from tempfile import NamedTemporaryFile
@ -138,3 +139,38 @@ def test_notebook_password():
nb.load_config_file()
nt.assert_not_equal(nb.password, '')
passwd_check(nb.password, password)
def test_notebook_stop():
def list_running_servers(runtime_dir):
for port in range(100, 110):
yield {
'pid': 1000 + port,
'port': port,
'base_url': '/',
'hostname': 'localhost',
'notebook_dir': '/',
'secure': False,
'token': '',
'password': False,
'url': 'http://localhost:%i' % port,
}
mock_servers = patch('notebook.notebookapp.list_running_servers', list_running_servers)
# test stop with a match
with mock_servers, patch('os.kill') as os_kill:
app = notebookapp.NbserverStopApp()
app.initialize(['105'])
app.start()
nt.assert_equal(os_kill.call_count, 1)
nt.assert_equal(os_kill.call_args, ((1105, signal.SIGTERM),))
# test no match
with mock_servers, patch('os.kill') as os_kill:
app = notebookapp.NbserverStopApp()
app.initialize(['999'])
with nt.assert_raises(SystemExit) as exc:
app.start()
nt.assert_equal(exc.exception.code, 1)
nt.assert_equal(os_kill.call_count, 0)