mirror of
https://github.com/jupyter/notebook.git
synced 2025-01-24 12:05:22 +08:00
Update test for 'jupyter notebook stop'
This commit is contained in:
parent
c894b45fb6
commit
7248772f72
@ -410,6 +410,9 @@ class NbserverStopApp(JupyterApp):
|
|||||||
if self.extra_args:
|
if self.extra_args:
|
||||||
self.port=int(self.extra_args[0])
|
self.port=int(self.extra_args[0])
|
||||||
|
|
||||||
|
def shutdown_server(self, server):
|
||||||
|
return shutdown_server(server, log=self.log)
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
servers = list(list_running_servers(self.runtime_dir))
|
servers = list(list_running_servers(self.runtime_dir))
|
||||||
if not servers:
|
if not servers:
|
||||||
@ -417,7 +420,7 @@ class NbserverStopApp(JupyterApp):
|
|||||||
for server in servers:
|
for server in servers:
|
||||||
if server['port'] == self.port:
|
if server['port'] == self.port:
|
||||||
print("Shutting down server on port", self.port, "...")
|
print("Shutting down server on port", self.port, "...")
|
||||||
if not shutdown_server(server, log=self.log):
|
if not self.shutdown_server(server):
|
||||||
sys.exit("Could not stop server")
|
sys.exit("Could not stop server")
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
|
@ -140,6 +140,15 @@ def test_notebook_password():
|
|||||||
nt.assert_not_equal(nb.password, '')
|
nt.assert_not_equal(nb.password, '')
|
||||||
passwd_check(nb.password, password)
|
passwd_check(nb.password, password)
|
||||||
|
|
||||||
|
class TestingStopApp(notebookapp.NbserverStopApp):
|
||||||
|
"""For testing the logic of NbserverStopApp."""
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
super(TestingStopApp, self).__init__(**kwargs)
|
||||||
|
self.servers_shut_down = []
|
||||||
|
|
||||||
|
def shutdown_server(self, server):
|
||||||
|
self.servers_shut_down.append(server)
|
||||||
|
return True
|
||||||
|
|
||||||
def test_notebook_stop():
|
def test_notebook_stop():
|
||||||
def list_running_servers(runtime_dir):
|
def list_running_servers(runtime_dir):
|
||||||
@ -159,18 +168,18 @@ def test_notebook_stop():
|
|||||||
mock_servers = patch('notebook.notebookapp.list_running_servers', list_running_servers)
|
mock_servers = patch('notebook.notebookapp.list_running_servers', list_running_servers)
|
||||||
|
|
||||||
# test stop with a match
|
# test stop with a match
|
||||||
with mock_servers, patch('os.kill') as os_kill:
|
with mock_servers:
|
||||||
app = notebookapp.NbserverStopApp()
|
app = TestingStopApp()
|
||||||
app.initialize(['105'])
|
app.initialize(['105'])
|
||||||
app.start()
|
app.start()
|
||||||
nt.assert_equal(os_kill.call_count, 1)
|
nt.assert_equal(len(app.servers_shut_down), 1)
|
||||||
nt.assert_equal(os_kill.call_args, ((1105, signal.SIGTERM),))
|
nt.assert_equal(app.servers_shut_down[0]['port'], 105)
|
||||||
|
|
||||||
# test no match
|
# test no match
|
||||||
with mock_servers, patch('os.kill') as os_kill:
|
with mock_servers, patch('os.kill') as os_kill:
|
||||||
app = notebookapp.NbserverStopApp()
|
app = TestingStopApp()
|
||||||
app.initialize(['999'])
|
app.initialize(['999'])
|
||||||
with nt.assert_raises(SystemExit) as exc:
|
with nt.assert_raises(SystemExit) as exc:
|
||||||
app.start()
|
app.start()
|
||||||
nt.assert_equal(exc.exception.code, 1)
|
nt.assert_equal(exc.exception.exception_code, 1)
|
||||||
nt.assert_equal(os_kill.call_count, 0)
|
nt.assert_equal(len(app.servers_shut_down), 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user