##// END OF EJS Templates
Missing class def for TimeoutError.
Brian E. Granger -
Show More
@@ -1,97 +1,100 b''
1 """Base class for notebook tests."""
1 """Base class for notebook tests."""
2
2
3 from __future__ import print_function
3 from __future__ import print_function
4
4
5 import sys
5 import sys
6 import time
6 import time
7 import requests
7 import requests
8 from contextlib import contextmanager
8 from contextlib import contextmanager
9 from subprocess import Popen, STDOUT
9 from subprocess import Popen, STDOUT
10 from unittest import TestCase
10 from unittest import TestCase
11
11
12 import nose
12 import nose
13
13
14 from IPython.utils.tempdir import TemporaryDirectory
14 from IPython.utils.tempdir import TemporaryDirectory
15
15
16 MAX_WAITTIME = 30 # seconds to wait for notebook server to start
16 MAX_WAITTIME = 30 # seconds to wait for notebook server to start
17 POLL_INTERVAL = 0.1 # time between attempts
17 POLL_INTERVAL = 0.1 # time between attempts
18
18
19 class TimeoutError(Exception):
20 pass
21
19 class NotebookTestBase(TestCase):
22 class NotebookTestBase(TestCase):
20 """A base class for tests that need a running notebook.
23 """A base class for tests that need a running notebook.
21
24
22 This creates an empty profile in a temp ipython_dir
25 This creates an empty profile in a temp ipython_dir
23 and then starts the notebook server with a separate temp notebook_dir.
26 and then starts the notebook server with a separate temp notebook_dir.
24 """
27 """
25
28
26 port = 12341
29 port = 12341
27
30
28 @classmethod
31 @classmethod
29 def wait_until_alive(cls):
32 def wait_until_alive(cls):
30 """Wait for the server to be alive"""
33 """Wait for the server to be alive"""
31 url = 'http://localhost:%i/api/notebooks' % cls.port
34 url = 'http://localhost:%i/api/notebooks' % cls.port
32 for _ in range(int(MAX_WAITTIME/POLL_INTERVAL)):
35 for _ in range(int(MAX_WAITTIME/POLL_INTERVAL)):
33 try:
36 try:
34 requests.get(url)
37 requests.get(url)
35 except requests.exceptions.ConnectionError:
38 except requests.exceptions.ConnectionError:
36 if cls.notebook.poll() is not None:
39 if cls.notebook.poll() is not None:
37 raise RuntimeError("The notebook server exited with status %s" \
40 raise RuntimeError("The notebook server exited with status %s" \
38 % cls.notebook.poll())
41 % cls.notebook.poll())
39 time.sleep(POLL_INTERVAL)
42 time.sleep(POLL_INTERVAL)
40 else:
43 else:
41 return
44 return
42
45
43 raise TimeoutError("The notebook server didn't start up correctly.")
46 raise TimeoutError("The notebook server didn't start up correctly.")
44
47
45 @classmethod
48 @classmethod
46 def wait_until_dead(cls):
49 def wait_until_dead(cls):
47 """Wait for the server process to terminate after shutdown"""
50 """Wait for the server process to terminate after shutdown"""
48 for _ in range(int(MAX_WAITTIME/POLL_INTERVAL)):
51 for _ in range(int(MAX_WAITTIME/POLL_INTERVAL)):
49 if cls.notebook.poll() is not None:
52 if cls.notebook.poll() is not None:
50 return
53 return
51 time.sleep(POLL_INTERVAL)
54 time.sleep(POLL_INTERVAL)
52
55
53 raise TimeoutError("Undead notebook server")
56 raise TimeoutError("Undead notebook server")
54
57
55 @classmethod
58 @classmethod
56 def setup_class(cls):
59 def setup_class(cls):
57 cls.ipython_dir = TemporaryDirectory()
60 cls.ipython_dir = TemporaryDirectory()
58 cls.notebook_dir = TemporaryDirectory()
61 cls.notebook_dir = TemporaryDirectory()
59 notebook_args = [
62 notebook_args = [
60 sys.executable, '-c',
63 sys.executable, '-c',
61 'from IPython.html.notebookapp import launch_new_instance; launch_new_instance()',
64 'from IPython.html.notebookapp import launch_new_instance; launch_new_instance()',
62 '--port=%d' % cls.port,
65 '--port=%d' % cls.port,
63 '--port-retries=0', # Don't try any other ports
66 '--port-retries=0', # Don't try any other ports
64 '--no-browser',
67 '--no-browser',
65 '--ipython-dir=%s' % cls.ipython_dir.name,
68 '--ipython-dir=%s' % cls.ipython_dir.name,
66 '--notebook-dir=%s' % cls.notebook_dir.name,
69 '--notebook-dir=%s' % cls.notebook_dir.name,
67 ]
70 ]
68 cls.notebook = Popen(notebook_args,
71 cls.notebook = Popen(notebook_args,
69 stdout=nose.iptest_stdstreams_fileno(),
72 stdout=nose.iptest_stdstreams_fileno(),
70 stderr=STDOUT,
73 stderr=STDOUT,
71 )
74 )
72 cls.wait_until_alive()
75 cls.wait_until_alive()
73
76
74 @classmethod
77 @classmethod
75 def teardown_class(cls):
78 def teardown_class(cls):
76 cls.notebook.terminate()
79 cls.notebook.terminate()
77 cls.ipython_dir.cleanup()
80 cls.ipython_dir.cleanup()
78 cls.notebook_dir.cleanup()
81 cls.notebook_dir.cleanup()
79 cls.wait_until_dead()
82 cls.wait_until_dead()
80
83
81 @classmethod
84 @classmethod
82 def base_url(cls):
85 def base_url(cls):
83 return 'http://localhost:%i/' % cls.port
86 return 'http://localhost:%i/' % cls.port
84
87
85
88
86 @contextmanager
89 @contextmanager
87 def assert_http_error(status, msg=None):
90 def assert_http_error(status, msg=None):
88 try:
91 try:
89 yield
92 yield
90 except requests.HTTPError as e:
93 except requests.HTTPError as e:
91 real_status = e.response.status_code
94 real_status = e.response.status_code
92 assert real_status == status, \
95 assert real_status == status, \
93 "Expected status %d, got %d" % (real_status, status)
96 "Expected status %d, got %d" % (real_status, status)
94 if msg:
97 if msg:
95 assert msg in str(e), e
98 assert msg in str(e), e
96 else:
99 else:
97 assert False, "Expected HTTP error status" No newline at end of file
100 assert False, "Expected HTTP error status"
General Comments 0
You need to be logged in to leave comments. Login now