Show More
@@ -1,62 +1,69 b'' | |||||
1 | """Base class for notebook tests.""" |
|
1 | """Base class for notebook tests.""" | |
2 |
|
2 | |||
3 | import sys |
|
3 | import sys | |
4 | import time |
|
4 | import time | |
5 | import requests |
|
5 | import requests | |
6 | from subprocess import Popen, PIPE |
|
6 | from subprocess import Popen, PIPE | |
7 | from unittest import TestCase |
|
7 | from unittest import TestCase | |
8 |
|
8 | |||
9 | from IPython.utils.tempdir import TemporaryDirectory |
|
9 | from IPython.utils.tempdir import TemporaryDirectory | |
10 |
|
10 | |||
11 |
|
11 | |||
12 | class NotebookTestBase(TestCase): |
|
12 | class NotebookTestBase(TestCase): | |
13 | """A base class for tests that need a running notebook. |
|
13 | """A base class for tests that need a running notebook. | |
14 |
|
14 | |||
15 | This creates an empty profile in a temp ipython_dir |
|
15 | This creates an empty profile in a temp ipython_dir | |
16 | and then starts the notebook server with a separate temp notebook_dir. |
|
16 | and then starts the notebook server with a separate temp notebook_dir. | |
17 | """ |
|
17 | """ | |
18 |
|
18 | |||
19 | port = 12341 |
|
19 | port = 12341 | |
20 |
|
20 | |||
21 | def wait_till_alive(self): |
|
21 | @classmethod | |
22 | url = 'http://localhost:%i/' % self.port |
|
22 | def wait_until_alive(cls): | |
|
23 | """Wait for the server to be alive""" | |||
|
24 | url = 'http://localhost:%i/api/notebooks' % cls.port | |||
23 | while True: |
|
25 | while True: | |
24 | time.sleep(.1) |
|
|||
25 | try: |
|
26 | try: | |
26 |
|
|
27 | requests.get(url) | |
27 | break |
|
|||
28 | except requests.exceptions.ConnectionError: |
|
28 | except requests.exceptions.ConnectionError: | |
29 |
|
|
29 | time.sleep(.1) | |
|
30 | else: | |||
|
31 | break | |||
30 |
|
|
32 | ||
31 | def wait_till_dead(self): |
|
33 | @classmethod | |
32 | url = 'http://localhost:%i/' % self.port |
|
34 | def wait_until_dead(cls): | |
|
35 | """Wait for the server to stop getting requests after shutdown""" | |||
|
36 | url = 'http://localhost:%i/api/notebooks' % cls.port | |||
33 | while True: |
|
37 | while True: | |
34 | time.sleep(.1) |
|
|||
35 | try: |
|
38 | try: | |
36 |
|
|
39 | requests.get(url) | |
37 | continue |
|
|||
38 | except requests.exceptions.ConnectionError: |
|
40 | except requests.exceptions.ConnectionError: | |
39 | break |
|
41 | break | |
|
42 | else: | |||
|
43 | time.sleep(.1) | |||
40 |
|
44 | |||
41 | def setUp(self): |
|
45 | @classmethod | |
42 | self.ipython_dir = TemporaryDirectory() |
|
46 | def setup_class(cls): | |
43 |
s |
|
47 | cls.ipython_dir = TemporaryDirectory() | |
|
48 | cls.notebook_dir = TemporaryDirectory() | |||
44 | notebook_args = [ |
|
49 | notebook_args = [ | |
45 | sys.executable, '-c', |
|
50 | sys.executable, '-c', | |
46 | 'from IPython.html.notebookapp import launch_new_instance; launch_new_instance()', |
|
51 | 'from IPython.html.notebookapp import launch_new_instance; launch_new_instance()', | |
47 |
'--port=%d' % s |
|
52 | '--port=%d' % cls.port, | |
48 | '--no-browser', |
|
53 | '--no-browser', | |
49 |
'--ipython-dir=%s' % s |
|
54 | '--ipython-dir=%s' % cls.ipython_dir.name, | |
50 |
'--notebook-dir=%s' % s |
|
55 | '--notebook-dir=%s' % cls.notebook_dir.name | |
51 | ] |
|
56 | ] | |
52 |
s |
|
57 | cls.notebook = Popen(notebook_args, stdout=PIPE, stderr=PIPE) | |
53 |
s |
|
58 | cls.wait_until_alive() | |
54 |
|
59 | |||
55 | def tearDown(self): |
|
60 | @classmethod | |
56 | self.notebook.terminate() |
|
61 | def teardown_class(cls): | |
57 | self.ipython_dir.cleanup() |
|
62 | cls.notebook.terminate() | |
58 |
s |
|
63 | cls.ipython_dir.cleanup() | |
59 | self.wait_till_dead() |
|
64 | cls.notebook_dir.cleanup() | |
|
65 | cls.wait_until_dead() | |||
60 |
|
66 | |||
61 | def base_url(self): |
|
67 | @classmethod | |
62 | return 'http://localhost:%i/' % self.port |
|
68 | def base_url(cls): | |
|
69 | return 'http://localhost:%i/' % cls.port |
General Comments 0
You need to be logged in to leave comments.
Login now